basicfs_lstat_windows.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2015 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. //go:build windows
  7. // +build windows
  8. package fs
  9. import (
  10. "fmt"
  11. "go/version"
  12. "os"
  13. "runtime"
  14. "syscall"
  15. "unsafe"
  16. "golang.org/x/sys/windows"
  17. )
  18. func readReparseTag(path string) (uint32, error) {
  19. namep, err := syscall.UTF16PtrFromString(path)
  20. if err != nil {
  21. return 0, fmt.Errorf("syscall.UTF16PtrFromString failed with: %s", err)
  22. }
  23. attrs := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS | syscall.FILE_FLAG_OPEN_REPARSE_POINT)
  24. h, err := syscall.CreateFile(namep, 0, 0, nil, syscall.OPEN_EXISTING, attrs, 0)
  25. if err != nil {
  26. return 0, fmt.Errorf("syscall.CreateFile failed with: %s", err)
  27. }
  28. defer syscall.CloseHandle(h)
  29. // https://docs.microsoft.com/windows/win32/api/winbase/ns-winbase-file_attribute_tag_info
  30. const fileAttributeTagInfo = 9
  31. type FILE_ATTRIBUTE_TAG_INFO struct {
  32. FileAttributes uint32
  33. ReparseTag uint32
  34. }
  35. var ti FILE_ATTRIBUTE_TAG_INFO
  36. err = windows.GetFileInformationByHandleEx(windows.Handle(h), fileAttributeTagInfo, (*byte)(unsafe.Pointer(&ti)), uint32(unsafe.Sizeof(ti)))
  37. if err != nil {
  38. if errno, ok := err.(syscall.Errno); ok && errno == windows.ERROR_INVALID_PARAMETER {
  39. // It appears calling GetFileInformationByHandleEx with
  40. // FILE_ATTRIBUTE_TAG_INFO fails on FAT file system with
  41. // ERROR_INVALID_PARAMETER. Clear ti.ReparseTag in that
  42. // instance to indicate no symlinks are possible.
  43. ti.ReparseTag = 0
  44. } else {
  45. return 0, fmt.Errorf("windows.GetFileInformationByHandleEx failed with: %s", err)
  46. }
  47. }
  48. return ti.ReparseTag, nil
  49. }
  50. func isDirectoryJunction(reparseTag uint32) bool {
  51. return reparseTag == windows.IO_REPARSE_TAG_MOUNT_POINT
  52. }
  53. type dirJunctFileInfo struct {
  54. os.FileInfo
  55. }
  56. func (fi *dirJunctFileInfo) Mode() os.FileMode {
  57. // Simulate a directory and not a symlink; also set the execute
  58. // bits so the directory can be traversed Unix-side.
  59. return fi.FileInfo.Mode()&^junctionPointModeMask | os.ModeDir | 0o111
  60. }
  61. func (fi *dirJunctFileInfo) IsDir() bool {
  62. return true
  63. }
  64. var junctionPointModeMask os.FileMode
  65. func init() {
  66. // Per https://tip.golang.org/doc/go1.23#minor_library_changes
  67. // In go1.22 (and go1.23 when GODEBUG=winsymlink=0) Windows' directory
  68. // junctions (aka "mount points") always have ModeSymlink set.
  69. junctionPointModeMask = os.ModeSymlink
  70. if version.Compare(runtime.Version(), "go1.23") >= 0 {
  71. // In go1.23 Windows' directory junctions always have ModeIrregular set
  72. // (unless GODEBUG=winsymlink=0).
  73. junctionPointModeMask |= os.ModeIrregular
  74. }
  75. }
  76. func (f *BasicFilesystem) underlyingLstat(name string) (os.FileInfo, error) {
  77. fi, err := os.Lstat(name)
  78. // There are cases where files are tagged as symlink, but they end up being
  79. // something else. Make sure we properly handle those types.
  80. if err == nil {
  81. // NTFS directory junctions can be treated as ordinary directories,
  82. // see https://forum.syncthing.net/t/option-to-follow-directory-junctions-symbolic-links/14750
  83. if fi.Mode()&junctionPointModeMask != 0 && f.junctionsAsDirs {
  84. if reparseTag, reparseErr := readReparseTag(name); reparseErr == nil && isDirectoryJunction(reparseTag) {
  85. return &dirJunctFileInfo{fi}, nil
  86. }
  87. }
  88. }
  89. return fi, err
  90. }