basicfs_lstat_windows.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // +build windows
  7. package fs
  8. import (
  9. "fmt"
  10. "os"
  11. "syscall"
  12. "unsafe"
  13. "golang.org/x/sys/windows"
  14. )
  15. func isDirectoryJunction(path string) (bool, error) {
  16. namep, err := syscall.UTF16PtrFromString(path)
  17. if err != nil {
  18. return false, fmt.Errorf("syscall.UTF16PtrFromString failed with: %s", err)
  19. }
  20. attrs := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS | syscall.FILE_FLAG_OPEN_REPARSE_POINT)
  21. h, err := syscall.CreateFile(namep, 0, 0, nil, syscall.OPEN_EXISTING, attrs, 0)
  22. if err != nil {
  23. return false, fmt.Errorf("syscall.CreateFile failed with: %s", err)
  24. }
  25. defer syscall.CloseHandle(h)
  26. //https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_attribute_tag_info
  27. const fileAttributeTagInfo = 9
  28. type FILE_ATTRIBUTE_TAG_INFO struct {
  29. FileAttributes uint32
  30. ReparseTag uint32
  31. }
  32. var ti FILE_ATTRIBUTE_TAG_INFO
  33. err = windows.GetFileInformationByHandleEx(windows.Handle(h), fileAttributeTagInfo, (*byte)(unsafe.Pointer(&ti)), uint32(unsafe.Sizeof(ti)))
  34. if err != nil {
  35. if errno, ok := err.(syscall.Errno); ok && errno == windows.ERROR_INVALID_PARAMETER {
  36. // It appears calling GetFileInformationByHandleEx with
  37. // FILE_ATTRIBUTE_TAG_INFO fails on FAT file system with
  38. // ERROR_INVALID_PARAMETER. Clear ti.ReparseTag in that
  39. // instance to indicate no symlinks are possible.
  40. ti.ReparseTag = 0
  41. } else {
  42. return false, fmt.Errorf("windows.GetFileInformationByHandleEx failed with: %s", err)
  43. }
  44. }
  45. return ti.ReparseTag == windows.IO_REPARSE_TAG_MOUNT_POINT, nil
  46. }
  47. type dirJunctFileInfo struct {
  48. os.FileInfo
  49. }
  50. func (fi *dirJunctFileInfo) Mode() os.FileMode {
  51. // Simulate a directory and not a symlink; also set the execute
  52. // bits so the directory can be traversed Unix-side.
  53. return fi.FileInfo.Mode() ^ os.ModeSymlink | os.ModeDir | 0111
  54. }
  55. func (fi *dirJunctFileInfo) IsDir() bool {
  56. return true
  57. }
  58. func (f *BasicFilesystem) underlyingLstat(name string) (os.FileInfo, error) {
  59. var fi, err = os.Lstat(name)
  60. // NTFS directory junctions can be treated as ordinary directories,
  61. // see https://forum.syncthing.net/t/option-to-follow-directory-junctions-symbolic-links/14750
  62. if err == nil && f.junctionsAsDirs && fi.Mode()&os.ModeSymlink != 0 {
  63. var isJunct bool
  64. isJunct, err = isDirectoryJunction(name)
  65. if err == nil && isJunct {
  66. return &dirJunctFileInfo{fi}, nil
  67. }
  68. }
  69. return fi, err
  70. }