symlink_windows.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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. // Needs administrator priviledges.
  41. // Let's check that everything works.
  42. // This could be done more officially:
  43. // http://stackoverflow.com/questions/2094663/determine-if-windows-process-has-privilege-to-create-symbolic-link
  44. // But I don't want to define 10 more structs just to look this up.
  45. base := os.TempDir()
  46. path := filepath.Join(base, "symlinktest")
  47. defer os.Remove(path)
  48. err := Create(path, base, protocol.FlagDirectory)
  49. if err != nil {
  50. return
  51. }
  52. isLink, err := IsSymlink(path)
  53. if err != nil || !isLink {
  54. return
  55. }
  56. target, flags, err := Read(path)
  57. if err != nil || osutil.NativeFilename(target) != base || flags&protocol.FlagDirectory == 0 {
  58. return
  59. }
  60. Supported = true
  61. }
  62. type reparseData struct {
  63. reparseTag uint32
  64. reparseDataLength uint16
  65. reserved uint16
  66. substitueNameOffset uint16
  67. substitueNameLength uint16
  68. printNameOffset uint16
  69. printNameLength uint16
  70. flags uint32
  71. // substituteName - 264 widechars max = 528 bytes
  72. // printName - 260 widechars max = 520 bytes
  73. // = 1048 bytes total
  74. buffer [1048]uint16
  75. }
  76. func (r *reparseData) PrintName() string {
  77. // No clue why the offset and length is doubled...
  78. offset := r.printNameOffset / 2
  79. length := r.printNameLength / 2
  80. return string(utf16.Decode(r.buffer[offset : offset+length]))
  81. }
  82. func (r *reparseData) SubstituteName() string {
  83. // No clue why the offset and length is doubled...
  84. offset := r.substitueNameOffset / 2
  85. length := r.substitueNameLength / 2
  86. return string(utf16.Decode(r.buffer[offset : offset+length]))
  87. }
  88. func Read(path string) (string, uint32, error) {
  89. ptr, err := syscall.UTF16PtrFromString(path)
  90. if err != nil {
  91. return "", protocol.FlagSymlinkMissingTarget, err
  92. }
  93. 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)
  94. if err != nil || handle == syscall.InvalidHandle {
  95. return "", protocol.FlagSymlinkMissingTarget, err
  96. }
  97. defer syscall.Close(handle)
  98. var ret uint16
  99. var data reparseData
  100. 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)
  101. if r1 == 0 {
  102. return "", protocol.FlagSymlinkMissingTarget, err
  103. }
  104. var flags uint32 = 0
  105. attr, err := syscall.GetFileAttributes(ptr)
  106. if err != nil {
  107. flags = protocol.FlagSymlinkMissingTarget
  108. } else if attr&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
  109. flags = protocol.FlagDirectory
  110. }
  111. return osutil.NormalizedFilename(data.PrintName()), flags, nil
  112. }
  113. func IsSymlink(path string) (bool, error) {
  114. ptr, err := syscall.UTF16PtrFromString(path)
  115. if err != nil {
  116. return false, err
  117. }
  118. attr, err := syscall.GetFileAttributes(ptr)
  119. if err != nil {
  120. return false, err
  121. }
  122. return attr&FILE_ATTRIBUTE_REPARSE_POINT != 0, nil
  123. }
  124. func Create(source, target string, flags uint32) error {
  125. srcp, err := syscall.UTF16PtrFromString(source)
  126. if err != nil {
  127. return err
  128. }
  129. trgp, err := syscall.UTF16PtrFromString(osutil.NativeFilename(target))
  130. if err != nil {
  131. return err
  132. }
  133. // Sadly for Windows we need to specify the type of the symlink,
  134. // whether it's a directory symlink or a file symlink.
  135. // If the flags doesn't reveal the target type, try to evaluate it
  136. // ourselves, and worst case default to the symlink pointing to a file.
  137. mode := 0
  138. if flags&protocol.FlagSymlinkMissingTarget != 0 {
  139. path := target
  140. if !filepath.IsAbs(target) {
  141. path = filepath.Join(filepath.Dir(source), target)
  142. }
  143. stat, err := os.Stat(path)
  144. if err == nil && stat.IsDir() {
  145. mode = SYMBOLIC_LINK_FLAG_DIRECTORY
  146. }
  147. } else if flags&protocol.FlagDirectory != 0 {
  148. mode = SYMBOLIC_LINK_FLAG_DIRECTORY
  149. }
  150. r0, _, err := syscall.Syscall(procCreateSymbolicLink.Addr(), 3, uintptr(unsafe.Pointer(srcp)), uintptr(unsafe.Pointer(trgp)), uintptr(mode))
  151. if r0 == 1 {
  152. return nil
  153. }
  154. return err
  155. }
  156. func ChangeType(path string, flags uint32) error {
  157. target, cflags, err := Read(path)
  158. if err != nil {
  159. return err
  160. }
  161. // If it's the same type, nothing to do.
  162. if cflags&protocol.SymlinkTypeMask == flags&protocol.SymlinkTypeMask {
  163. return nil
  164. }
  165. // If the actual type is unknown, but the new type is file, nothing to do
  166. if cflags&protocol.FlagSymlinkMissingTarget != 0 && flags&protocol.FlagDirectory == 0 {
  167. return nil
  168. }
  169. return osutil.InWritableDir(func(path string) error {
  170. // It should be a symlink as well hence no need to change permissions on
  171. // the file.
  172. os.Remove(path)
  173. return Create(path, target, flags)
  174. }, path)
  175. }