longpath.go 737 B

1234567891011121314151617181920212223242526
  1. // longpath introduces some constants and helper functions for handling long paths
  2. // in Windows, which are expected to be prepended with `\\?\` and followed by either
  3. // a drive letter, a UNC server\share, or a volume identifier.
  4. package longpath
  5. import (
  6. "strings"
  7. )
  8. // Prefix is the longpath prefix for Windows file paths.
  9. const Prefix = `\\?\`
  10. // AddPrefix will add the Windows long path prefix to the path provided if
  11. // it does not already have it.
  12. func AddPrefix(path string) string {
  13. if !strings.HasPrefix(path, Prefix) {
  14. if strings.HasPrefix(path, `\\`) {
  15. // This is a UNC path, so we need to add 'UNC' to the path as well.
  16. path = Prefix + `UNC` + path[1:]
  17. } else {
  18. path = Prefix + path
  19. }
  20. }
  21. return path
  22. }