symlink_windows.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build windows
  16. package symlinks
  17. import (
  18. "os"
  19. "path/filepath"
  20. "github.com/syncthing/syncthing/internal/osutil"
  21. "github.com/syncthing/syncthing/internal/protocol"
  22. "syscall"
  23. "unicode/utf16"
  24. "unsafe"
  25. )
  26. const (
  27. FSCTL_GET_REPARSE_POINT = 0x900a8
  28. FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
  29. FILE_ATTRIBUTE_REPARSE_POINT = 0x400
  30. IO_REPARSE_TAG_SYMLINK = 0xA000000C
  31. SYMBOLIC_LINK_FLAG_DIRECTORY = 0x1
  32. )
  33. var (
  34. modkernel32 = syscall.NewLazyDLL("kernel32.dll")
  35. procDeviceIoControl = modkernel32.NewProc("DeviceIoControl")
  36. procCreateSymbolicLink = modkernel32.NewProc("CreateSymbolicLinkW")
  37. Supported = false
  38. )
  39. func init() {
  40. defer func() {
  41. if err := recover(); err != nil {
  42. // Ensure that the supported flag is disabled when we hit an
  43. // error, even though it should already be. Also, silently swallow
  44. // the error since it's fine for a system not to support symlinks.
  45. Supported = false
  46. }
  47. }()
  48. // Needs administrator priviledges.
  49. // Let's check that everything works.
  50. // This could be done more officially:
  51. // http://stackoverflow.com/questions/2094663/determine-if-windows-process-has-privilege-to-create-symbolic-link
  52. // But I don't want to define 10 more structs just to look this up.
  53. base := os.TempDir()
  54. path := filepath.Join(base, "symlinktest")
  55. defer os.Remove(path)
  56. err := Create(path, base, protocol.FlagDirectory)
  57. if err != nil {
  58. return
  59. }
  60. isLink, err := IsSymlink(path)
  61. if err != nil || !isLink {
  62. return
  63. }
  64. target, flags, err := Read(path)
  65. if err != nil || osutil.NativeFilename(target) != base || flags&protocol.FlagDirectory == 0 {
  66. return
  67. }
  68. Supported = true
  69. }
  70. type reparseData struct {
  71. reparseTag uint32
  72. reparseDataLength uint16
  73. reserved uint16
  74. substitueNameOffset uint16
  75. substitueNameLength uint16
  76. printNameOffset uint16
  77. printNameLength uint16
  78. flags uint32
  79. // substituteName - 264 widechars max = 528 bytes
  80. // printName - 260 widechars max = 520 bytes
  81. // = 1048 bytes total
  82. buffer [1048]uint16
  83. }
  84. func (r *reparseData) PrintName() string {
  85. // No clue why the offset and length is doubled...
  86. offset := r.printNameOffset / 2
  87. length := r.printNameLength / 2
  88. return string(utf16.Decode(r.buffer[offset : offset+length]))
  89. }
  90. func (r *reparseData) SubstituteName() string {
  91. // No clue why the offset and length is doubled...
  92. offset := r.substitueNameOffset / 2
  93. length := r.substitueNameLength / 2
  94. return string(utf16.Decode(r.buffer[offset : offset+length]))
  95. }
  96. func Read(path string) (string, uint32, error) {
  97. ptr, err := syscall.UTF16PtrFromString(path)
  98. if err != nil {
  99. return "", protocol.FlagSymlinkMissingTarget, err
  100. }
  101. handle, err := syscall.CreateFile(ptr, 0, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, 0)
  102. if err != nil || handle == syscall.InvalidHandle {
  103. return "", protocol.FlagSymlinkMissingTarget, err
  104. }
  105. defer syscall.Close(handle)
  106. var ret uint16
  107. var data reparseData
  108. r1, _, err := syscall.Syscall9(procDeviceIoControl.Addr(), 8, uintptr(handle), FSCTL_GET_REPARSE_POINT, 0, 0, uintptr(unsafe.Pointer(&data)), unsafe.Sizeof(data), uintptr(unsafe.Pointer(&ret)), 0, 0)
  109. if r1 == 0 {
  110. return "", protocol.FlagSymlinkMissingTarget, err
  111. }
  112. var flags uint32 = 0
  113. attr, err := syscall.GetFileAttributes(ptr)
  114. if err != nil {
  115. flags = protocol.FlagSymlinkMissingTarget
  116. } else if attr&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
  117. flags = protocol.FlagDirectory
  118. }
  119. return osutil.NormalizedFilename(data.PrintName()), flags, nil
  120. }
  121. func IsSymlink(path string) (bool, error) {
  122. ptr, err := syscall.UTF16PtrFromString(path)
  123. if err != nil {
  124. return false, err
  125. }
  126. attr, err := syscall.GetFileAttributes(ptr)
  127. if err != nil {
  128. return false, err
  129. }
  130. return attr&FILE_ATTRIBUTE_REPARSE_POINT != 0, nil
  131. }
  132. func Create(source, target string, flags uint32) error {
  133. srcp, err := syscall.UTF16PtrFromString(source)
  134. if err != nil {
  135. return err
  136. }
  137. trgp, err := syscall.UTF16PtrFromString(osutil.NativeFilename(target))
  138. if err != nil {
  139. return err
  140. }
  141. // Sadly for Windows we need to specify the type of the symlink,
  142. // whether it's a directory symlink or a file symlink.
  143. // If the flags doesn't reveal the target type, try to evaluate it
  144. // ourselves, and worst case default to the symlink pointing to a file.
  145. mode := 0
  146. if flags&protocol.FlagSymlinkMissingTarget != 0 {
  147. path := target
  148. if !filepath.IsAbs(target) {
  149. path = filepath.Join(filepath.Dir(source), target)
  150. }
  151. stat, err := os.Stat(path)
  152. if err == nil && stat.IsDir() {
  153. mode = SYMBOLIC_LINK_FLAG_DIRECTORY
  154. }
  155. } else if flags&protocol.FlagDirectory != 0 {
  156. mode = SYMBOLIC_LINK_FLAG_DIRECTORY
  157. }
  158. r0, _, err := syscall.Syscall(procCreateSymbolicLink.Addr(), 3, uintptr(unsafe.Pointer(srcp)), uintptr(unsafe.Pointer(trgp)), uintptr(mode))
  159. if r0 == 1 {
  160. return nil
  161. }
  162. return err
  163. }
  164. func ChangeType(path string, flags uint32) error {
  165. target, cflags, err := Read(path)
  166. if err != nil {
  167. return err
  168. }
  169. // If it's the same type, nothing to do.
  170. if cflags&protocol.SymlinkTypeMask == flags&protocol.SymlinkTypeMask {
  171. return nil
  172. }
  173. // If the actual type is unknown, but the new type is file, nothing to do
  174. if cflags&protocol.FlagSymlinkMissingTarget != 0 && flags&protocol.FlagDirectory == 0 {
  175. return nil
  176. }
  177. return osutil.InWritableDir(func(path string) error {
  178. // It should be a symlink as well hence no need to change permissions on
  179. // the file.
  180. os.Remove(path)
  181. return Create(path, target, flags)
  182. }, path)
  183. }