file_windows.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package loggerutils
  2. import (
  3. "os"
  4. "syscall"
  5. "unsafe"
  6. )
  7. func open(name string) (*os.File, error) {
  8. return openFile(name, os.O_RDONLY, 0)
  9. }
  10. func openFile(name string, flag int, perm os.FileMode) (*os.File, error) {
  11. if name == "" {
  12. return nil, &os.PathError{Op: "open", Path: name, Err: syscall.ENOENT}
  13. }
  14. h, err := syscallOpen(fixLongPath(name), flag|syscall.O_CLOEXEC, syscallMode(perm))
  15. if err != nil {
  16. return nil, &os.PathError{Op: "open", Path: name, Err: err}
  17. }
  18. return os.NewFile(uintptr(h), name), nil
  19. }
  20. // syscallOpen is copied from syscall.Open but is modified to
  21. // always open a file with FILE_SHARE_DELETE
  22. func syscallOpen(path string, mode int, perm uint32) (fd syscall.Handle, err error) {
  23. if len(path) == 0 {
  24. return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
  25. }
  26. pathp, err := syscall.UTF16PtrFromString(path)
  27. if err != nil {
  28. return syscall.InvalidHandle, err
  29. }
  30. var access uint32
  31. switch mode & (syscall.O_RDONLY | syscall.O_WRONLY | syscall.O_RDWR) {
  32. case syscall.O_RDONLY:
  33. access = syscall.GENERIC_READ
  34. case syscall.O_WRONLY:
  35. access = syscall.GENERIC_WRITE
  36. case syscall.O_RDWR:
  37. access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
  38. }
  39. if mode&syscall.O_CREAT != 0 {
  40. access |= syscall.GENERIC_WRITE
  41. }
  42. if mode&syscall.O_APPEND != 0 {
  43. access &^= syscall.GENERIC_WRITE
  44. access |= syscall.FILE_APPEND_DATA
  45. }
  46. sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE | syscall.FILE_SHARE_DELETE)
  47. var sa *syscall.SecurityAttributes
  48. if mode&syscall.O_CLOEXEC == 0 {
  49. sa = makeInheritSa()
  50. }
  51. var createmode uint32
  52. switch {
  53. case mode&(syscall.O_CREAT|syscall.O_EXCL) == (syscall.O_CREAT | syscall.O_EXCL):
  54. createmode = syscall.CREATE_NEW
  55. case mode&(syscall.O_CREAT|syscall.O_TRUNC) == (syscall.O_CREAT | syscall.O_TRUNC):
  56. createmode = syscall.CREATE_ALWAYS
  57. case mode&syscall.O_CREAT == syscall.O_CREAT:
  58. createmode = syscall.OPEN_ALWAYS
  59. case mode&syscall.O_TRUNC == syscall.O_TRUNC:
  60. createmode = syscall.TRUNCATE_EXISTING
  61. default:
  62. createmode = syscall.OPEN_EXISTING
  63. }
  64. h, e := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
  65. return h, e
  66. }
  67. func makeInheritSa() *syscall.SecurityAttributes {
  68. var sa syscall.SecurityAttributes
  69. sa.Length = uint32(unsafe.Sizeof(sa))
  70. sa.InheritHandle = 1
  71. return &sa
  72. }
  73. // fixLongPath returns the extended-length (\\?\-prefixed) form of
  74. // path when needed, in order to avoid the default 260 character file
  75. // path limit imposed by Windows. If path is not easily converted to
  76. // the extended-length form (for example, if path is a relative path
  77. // or contains .. elements), or is short enough, fixLongPath returns
  78. // path unmodified.
  79. //
  80. // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
  81. //
  82. // Copied from os.OpenFile
  83. func fixLongPath(path string) string {
  84. // Do nothing (and don't allocate) if the path is "short".
  85. // Empirically (at least on the Windows Server 2013 builder),
  86. // the kernel is arbitrarily okay with < 248 bytes. That
  87. // matches what the docs above say:
  88. // "When using an API to create a directory, the specified
  89. // path cannot be so long that you cannot append an 8.3 file
  90. // name (that is, the directory name cannot exceed MAX_PATH
  91. // minus 12)." Since MAX_PATH is 260, 260 - 12 = 248.
  92. //
  93. // The MSDN docs appear to say that a normal path that is 248 bytes long
  94. // will work; empirically the path must be less then 248 bytes long.
  95. if len(path) < 248 {
  96. // Don't fix. (This is how Go 1.7 and earlier worked,
  97. // not automatically generating the \\?\ form)
  98. return path
  99. }
  100. // The extended form begins with \\?\, as in
  101. // \\?\c:\windows\foo.txt or \\?\UNC\server\share\foo.txt.
  102. // The extended form disables evaluation of . and .. path
  103. // elements and disables the interpretation of / as equivalent
  104. // to \. The conversion here rewrites / to \ and elides
  105. // . elements as well as trailing or duplicate separators. For
  106. // simplicity it avoids the conversion entirely for relative
  107. // paths or paths containing .. elements. For now,
  108. // \\server\share paths are not converted to
  109. // \\?\UNC\server\share paths because the rules for doing so
  110. // are less well-specified.
  111. if len(path) >= 2 && path[:2] == `\\` {
  112. // Don't canonicalize UNC paths.
  113. return path
  114. }
  115. if !isAbs(path) {
  116. // Relative path
  117. return path
  118. }
  119. const prefix = `\\?`
  120. pathbuf := make([]byte, len(prefix)+len(path)+len(`\`))
  121. copy(pathbuf, prefix)
  122. n := len(path)
  123. r, w := 0, len(prefix)
  124. for r < n {
  125. switch {
  126. case os.IsPathSeparator(path[r]):
  127. // empty block
  128. r++
  129. case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
  130. // /./
  131. r++
  132. case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
  133. // /../ is currently unhandled
  134. return path
  135. default:
  136. pathbuf[w] = '\\'
  137. w++
  138. for ; r < n && !os.IsPathSeparator(path[r]); r++ {
  139. pathbuf[w] = path[r]
  140. w++
  141. }
  142. }
  143. }
  144. // A drive's root directory needs a trailing \
  145. if w == len(`\\?\c:`) {
  146. pathbuf[w] = '\\'
  147. w++
  148. }
  149. return string(pathbuf[:w])
  150. }
  151. // copied from os package for os.OpenFile
  152. func syscallMode(i os.FileMode) (o uint32) {
  153. o |= uint32(i.Perm())
  154. if i&os.ModeSetuid != 0 {
  155. o |= syscall.S_ISUID
  156. }
  157. if i&os.ModeSetgid != 0 {
  158. o |= syscall.S_ISGID
  159. }
  160. if i&os.ModeSticky != 0 {
  161. o |= syscall.S_ISVTX
  162. }
  163. // No mapping for Go's ModeTemporary (plan9 only).
  164. return
  165. }
  166. func isAbs(path string) (b bool) {
  167. v := volumeName(path)
  168. if v == "" {
  169. return false
  170. }
  171. path = path[len(v):]
  172. if path == "" {
  173. return false
  174. }
  175. return os.IsPathSeparator(path[0])
  176. }
  177. func volumeName(path string) (v string) {
  178. if len(path) < 2 {
  179. return ""
  180. }
  181. // with drive letter
  182. c := path[0]
  183. if path[1] == ':' &&
  184. ('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
  185. 'A' <= c && c <= 'Z') {
  186. return path[:2]
  187. }
  188. // is it UNC
  189. if l := len(path); l >= 5 && os.IsPathSeparator(path[0]) && os.IsPathSeparator(path[1]) &&
  190. !os.IsPathSeparator(path[2]) && path[2] != '.' {
  191. // first, leading `\\` and next shouldn't be `\`. its server name.
  192. for n := 3; n < l-1; n++ {
  193. // second, next '\' shouldn't be repeated.
  194. if os.IsPathSeparator(path[n]) {
  195. n++
  196. // third, following something characters. its share name.
  197. if !os.IsPathSeparator(path[n]) {
  198. if path[n] == '.' {
  199. break
  200. }
  201. for ; n < l; n++ {
  202. if os.IsPathSeparator(path[n]) {
  203. break
  204. }
  205. }
  206. return path[:n]
  207. }
  208. break
  209. }
  210. }
  211. }
  212. return ""
  213. }