basicfs_realcaser_windows.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // +build windows
  7. package fs
  8. import (
  9. "path/filepath"
  10. "strings"
  11. "syscall"
  12. )
  13. type basicRealCaserWindows struct {
  14. uri string
  15. }
  16. func newBasicRealCaser(fs Filesystem) realCaser {
  17. return &basicRealCaserWindows{fs.URI()}
  18. }
  19. // RealCase returns the correct case for the given name, which is a relative
  20. // path below root, as it exists on disk.
  21. func (r *basicRealCaserWindows) realCase(name string) (string, error) {
  22. if name == "." {
  23. return ".", nil
  24. }
  25. path := r.uri
  26. comps := strings.Split(name, string(PathSeparator))
  27. var err error
  28. for i, comp := range comps {
  29. path = filepath.Join(path, comp)
  30. comps[i], err = r.realCaseBase(path)
  31. if err != nil {
  32. return "", err
  33. }
  34. }
  35. return filepath.Join(comps...), nil
  36. }
  37. func (*basicRealCaserWindows) realCaseBase(path string) (string, error) {
  38. p, err := syscall.UTF16PtrFromString(fixLongPath(path))
  39. if err != nil {
  40. return "", err
  41. }
  42. var fd syscall.Win32finddata
  43. h, err := syscall.FindFirstFile(p, &fd)
  44. if err != nil {
  45. return "", err
  46. }
  47. syscall.FindClose(h)
  48. return syscall.UTF16ToString(fd.FileName[:]), nil
  49. }
  50. func (r *basicRealCaserWindows) dropCache() {}