debug_symlink_windows.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package fs
  5. import (
  6. "os"
  7. "path/filepath"
  8. "syscall"
  9. )
  10. // DebugSymlinkForTestsOnly is os.Symlink taken from the 1.9.2 stdlib,
  11. // hacked with the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE flag to
  12. // create symlinks when not elevated.
  13. //
  14. // This is not and should not be used in Syncthing code, hence the
  15. // cumbersome name to make it obvious if this ever leaks. Nonetheless it's
  16. // useful in tests.
  17. func DebugSymlinkForTestsOnly(oldFs, newFS Filesystem, oldname, newname string) error {
  18. oldname = filepath.Join(oldFs.URI(), oldname)
  19. newname = filepath.Join(newFS.URI(), newname)
  20. // CreateSymbolicLink is not supported before Windows Vista
  21. if syscall.LoadCreateSymbolicLink() != nil {
  22. return &os.LinkError{"symlink", oldname, newname, syscall.EWINDOWS}
  23. }
  24. // '/' does not work in link's content
  25. oldname = filepath.FromSlash(oldname)
  26. // need the exact location of the oldname when it's relative to determine if it's a directory
  27. destpath := oldname
  28. if !filepath.IsAbs(oldname) {
  29. destpath = filepath.Dir(newname) + `\` + oldname
  30. }
  31. fi, err := os.Lstat(destpath)
  32. isdir := err == nil && fi.IsDir()
  33. n, err := syscall.UTF16PtrFromString(fixLongPath(newname))
  34. if err != nil {
  35. return &os.LinkError{"symlink", oldname, newname, err}
  36. }
  37. o, err := syscall.UTF16PtrFromString(fixLongPath(oldname))
  38. if err != nil {
  39. return &os.LinkError{"symlink", oldname, newname, err}
  40. }
  41. var flags uint32
  42. if isdir {
  43. flags |= syscall.SYMBOLIC_LINK_FLAG_DIRECTORY
  44. }
  45. flags |= 0x02 // SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
  46. err = syscall.CreateSymbolicLink(n, o, flags)
  47. if err != nil {
  48. return &os.LinkError{"symlink", oldname, newname, err}
  49. }
  50. return nil
  51. }
  52. // fixLongPath returns the extended-length (\\?\-prefixed) form of
  53. // path when needed, in order to avoid the default 260 character file
  54. // path limit imposed by Windows. If path is not easily converted to
  55. // the extended-length form (for example, if path is a relative path
  56. // or contains .. elements), or is short enough, fixLongPath returns
  57. // path unmodified.
  58. //
  59. // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
  60. func fixLongPath(path string) string {
  61. // Do nothing (and don't allocate) if the path is "short".
  62. // Empirically (at least on the Windows Server 2013 builder),
  63. // the kernel is arbitrarily okay with < 248 bytes. That
  64. // matches what the docs above say:
  65. // "When using an API to create a directory, the specified
  66. // path cannot be so long that you cannot append an 8.3 file
  67. // name (that is, the directory name cannot exceed MAX_PATH
  68. // minus 12)." Since MAX_PATH is 260, 260 - 12 = 248.
  69. //
  70. // The MSDN docs appear to say that a normal path that is 248 bytes long
  71. // will work; empirically the path must be less than 248 bytes long.
  72. if len(path) < 248 {
  73. // Don't fix. (This is how Go 1.7 and earlier worked,
  74. // not automatically generating the \\?\ form)
  75. return path
  76. }
  77. // The extended form begins with \\?\, as in
  78. // \\?\c:\windows\foo.txt or \\?\UNC\server\share\foo.txt.
  79. // The extended form disables evaluation of . and .. path
  80. // elements and disables the interpretation of / as equivalent
  81. // to \. The conversion here rewrites / to \ and elides
  82. // . elements as well as trailing or duplicate separators. For
  83. // simplicity it avoids the conversion entirely for relative
  84. // paths or paths containing .. elements. For now,
  85. // \\server\share paths are not converted to
  86. // \\?\UNC\server\share paths because the rules for doing so
  87. // are less well-specified.
  88. if len(path) >= 2 && path[:2] == `\\` {
  89. // Don't canonicalize UNC paths.
  90. return path
  91. }
  92. if !filepath.IsAbs(path) {
  93. // Relative path
  94. return path
  95. }
  96. const prefix = `\\?`
  97. pathbuf := make([]byte, len(prefix)+len(path)+len(`\`))
  98. copy(pathbuf, prefix)
  99. n := len(path)
  100. r, w := 0, len(prefix)
  101. for r < n {
  102. switch {
  103. case os.IsPathSeparator(path[r]):
  104. // empty block
  105. r++
  106. case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
  107. // /./
  108. r++
  109. case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
  110. // /../ is currently unhandled
  111. return path
  112. default:
  113. pathbuf[w] = '\\'
  114. w++
  115. for ; r < n && !os.IsPathSeparator(path[r]); r++ {
  116. pathbuf[w] = path[r]
  117. w++
  118. }
  119. }
  120. }
  121. // A drive's root directory needs a trailing \
  122. if w == len(`\\?\c:`) {
  123. pathbuf[w] = '\\'
  124. w++
  125. }
  126. return string(pathbuf[:w])
  127. }