symlink_windows.go 4.4 KB

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