file_windows.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 os_overloads
  5. import (
  6. "os"
  7. "syscall"
  8. "golang.org/x/sys/windows"
  9. )
  10. func isAbs(path string) (b bool) {
  11. v := volumeName(path)
  12. if v == "" {
  13. return false
  14. }
  15. path = path[len(v):]
  16. if path == "" {
  17. return false
  18. }
  19. return os.IsPathSeparator(path[0])
  20. }
  21. func volumeName(path string) (v string) {
  22. if len(path) < 2 {
  23. return ""
  24. }
  25. // with drive letter
  26. c := path[0]
  27. if path[1] == ':' &&
  28. ('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
  29. 'A' <= c && c <= 'Z') {
  30. return path[:2]
  31. }
  32. // is it UNC
  33. if l := len(path); l >= 5 && os.IsPathSeparator(path[0]) && os.IsPathSeparator(path[1]) &&
  34. !os.IsPathSeparator(path[2]) && path[2] != '.' {
  35. // first, leading `\\` and next shouldn't be `\`. its server name.
  36. for n := 3; n < l-1; n++ {
  37. // second, next '\' shouldn't be repeated.
  38. if os.IsPathSeparator(path[n]) {
  39. n++
  40. // third, following something characters. its share name.
  41. if !os.IsPathSeparator(path[n]) {
  42. if path[n] == '.' {
  43. break
  44. }
  45. for ; n < l; n++ {
  46. if os.IsPathSeparator(path[n]) {
  47. break
  48. }
  49. }
  50. return path[:n]
  51. }
  52. break
  53. }
  54. }
  55. }
  56. return ""
  57. }
  58. // fixLongPath returns the extended-length (\\?\-prefixed) form of
  59. // path when needed, in order to avoid the default 260 character file
  60. // path limit imposed by Windows. If path is not easily converted to
  61. // the extended-length form (for example, if path is a relative path
  62. // or contains .. elements), or is short enough, fixLongPath returns
  63. // path unmodified.
  64. //
  65. // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
  66. func fixLongPath(path string) string {
  67. // Do nothing (and don't allocate) if the path is "short".
  68. // Empirically (at least on the Windows Server 2013 builder),
  69. // the kernel is arbitrarily okay with < 248 bytes. That
  70. // matches what the docs above say:
  71. // "When using an API to create a directory, the specified
  72. // path cannot be so long that you cannot append an 8.3 file
  73. // name (that is, the directory name cannot exceed MAX_PATH
  74. // minus 12)." Since MAX_PATH is 260, 260 - 12 = 248.
  75. //
  76. // The MSDN docs appear to say that a normal path that is 248 bytes long
  77. // will work; empirically the path must be less then 248 bytes long.
  78. if len(path) < 248 {
  79. // Don't fix. (This is how Go 1.7 and earlier worked,
  80. // not automatically generating the \\?\ form)
  81. return path
  82. }
  83. // The extended form begins with \\?\, as in
  84. // \\?\c:\windows\foo.txt or \\?\UNC\server\share\foo.txt.
  85. // The extended form disables evaluation of . and .. path
  86. // elements and disables the interpretation of / as equivalent
  87. // to \. The conversion here rewrites / to \ and elides
  88. // . elements as well as trailing or duplicate separators. For
  89. // simplicity it avoids the conversion entirely for relative
  90. // paths or paths containing .. elements. For now,
  91. // \\server\share paths are not converted to
  92. // \\?\UNC\server\share paths because the rules for doing so
  93. // are less well-specified.
  94. if len(path) >= 2 && path[:2] == `\\` {
  95. // Don't canonicalize UNC paths.
  96. return path
  97. }
  98. if !isAbs(path) {
  99. // Relative path
  100. return path
  101. }
  102. const prefix = `\\?`
  103. pathbuf := make([]byte, len(prefix)+len(path)+len(`\`))
  104. copy(pathbuf, prefix)
  105. n := len(path)
  106. r, w := 0, len(prefix)
  107. for r < n {
  108. switch {
  109. case os.IsPathSeparator(path[r]):
  110. // empty block
  111. r++
  112. case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
  113. // /./
  114. r++
  115. case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
  116. // /../ is currently unhandled
  117. return path
  118. default:
  119. pathbuf[w] = '\\'
  120. w++
  121. for ; r < n && !os.IsPathSeparator(path[r]); r++ {
  122. pathbuf[w] = path[r]
  123. w++
  124. }
  125. }
  126. }
  127. // A drive's root directory needs a trailing \
  128. if w == len(`\\?\c:`) {
  129. pathbuf[w] = '\\'
  130. w++
  131. }
  132. return string(pathbuf[:w])
  133. }
  134. // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
  135. func syscallMode(i os.FileMode) (o uint32) {
  136. o |= uint32(i.Perm())
  137. if i&os.ModeSetuid != 0 {
  138. o |= syscall.S_ISUID
  139. }
  140. if i&os.ModeSetgid != 0 {
  141. o |= syscall.S_ISGID
  142. }
  143. if i&os.ModeSticky != 0 {
  144. o |= syscall.S_ISVTX
  145. }
  146. // No mapping for Go's ModeTemporary (plan9 only).
  147. return
  148. }
  149. //If the bool is set to true then the file is opened with the parameters FILE_ATTRIBUTE_TEMPORARY and
  150. // FILE_FLAG_DELETE_ON_CLOSE
  151. func OpenFile(name string, flag int, perm os.FileMode, setToTempAndDelete bool) (file *os.File, err error) {
  152. r, e := Open(fixLongPath(name), flag|windows.O_CLOEXEC, syscallMode(perm), setToTempAndDelete)
  153. if e != nil {
  154. return nil, e
  155. }
  156. return os.NewFile(uintptr(r), name), nil
  157. }