utils_windows_test.go 774 B

123456789101112131415161718192021222324252627
  1. package main
  2. import "golang.org/x/sys/windows"
  3. // getLongPathName converts Windows short pathnames to full pathnames.
  4. // For example C:\Users\ADMIN~1 --> C:\Users\Administrator.
  5. // It is a no-op on non-Windows platforms
  6. func getLongPathName(path string) (string, error) {
  7. // See https://groups.google.com/forum/#!topic/golang-dev/1tufzkruoTg
  8. p, err := windows.UTF16FromString(path)
  9. if err != nil {
  10. return "", err
  11. }
  12. b := p // GetLongPathName says we can reuse buffer
  13. n, err := windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
  14. if err != nil {
  15. return "", err
  16. }
  17. if n > uint32(len(b)) {
  18. b = make([]uint16, n)
  19. _, err = windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
  20. if err != nil {
  21. return "", err
  22. }
  23. }
  24. return windows.UTF16ToString(b), nil
  25. }