filesys_windows.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // +build windows
  2. package system
  3. import (
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "syscall"
  11. "time"
  12. "unsafe"
  13. winio "github.com/Microsoft/go-winio"
  14. )
  15. // MkdirAllWithACL is a wrapper for MkdirAll that creates a directory
  16. // ACL'd for Builtin Administrators and Local System.
  17. func MkdirAllWithACL(path string, perm os.FileMode) error {
  18. return mkdirall(path, true)
  19. }
  20. // MkdirAll implementation that is volume path aware for Windows.
  21. func MkdirAll(path string, _ os.FileMode) error {
  22. return mkdirall(path, false)
  23. }
  24. // mkdirall is a custom version of os.MkdirAll modified for use on Windows
  25. // so that it is both volume path aware, and can create a directory with
  26. // a DACL.
  27. func mkdirall(path string, adminAndLocalSystem bool) error {
  28. if re := regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}$`); re.MatchString(path) {
  29. return nil
  30. }
  31. // The rest of this method is largely copied from os.MkdirAll and should be kept
  32. // as-is to ensure compatibility.
  33. // Fast path: if we can tell whether path is a directory or file, stop with success or error.
  34. dir, err := os.Stat(path)
  35. if err == nil {
  36. if dir.IsDir() {
  37. return nil
  38. }
  39. return &os.PathError{
  40. Op: "mkdir",
  41. Path: path,
  42. Err: syscall.ENOTDIR,
  43. }
  44. }
  45. // Slow path: make sure parent exists and then call Mkdir for path.
  46. i := len(path)
  47. for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
  48. i--
  49. }
  50. j := i
  51. for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
  52. j--
  53. }
  54. if j > 1 {
  55. // Create parent
  56. err = mkdirall(path[0:j-1], false)
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. // Parent now exists; invoke os.Mkdir or mkdirWithACL and use its result.
  62. if adminAndLocalSystem {
  63. err = mkdirWithACL(path)
  64. } else {
  65. err = os.Mkdir(path, 0)
  66. }
  67. if err != nil {
  68. // Handle arguments like "foo/." by
  69. // double-checking that directory doesn't exist.
  70. dir, err1 := os.Lstat(path)
  71. if err1 == nil && dir.IsDir() {
  72. return nil
  73. }
  74. return err
  75. }
  76. return nil
  77. }
  78. // mkdirWithACL creates a new directory. If there is an error, it will be of
  79. // type *PathError. .
  80. //
  81. // This is a modified and combined version of os.Mkdir and syscall.Mkdir
  82. // in golang to cater for creating a directory am ACL permitting full
  83. // access, with inheritance, to any subfolder/file for Built-in Administrators
  84. // and Local System.
  85. func mkdirWithACL(name string) error {
  86. sa := syscall.SecurityAttributes{Length: 0}
  87. sddl := "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
  88. sd, err := winio.SddlToSecurityDescriptor(sddl)
  89. if err != nil {
  90. return &os.PathError{Op: "mkdir", Path: name, Err: err}
  91. }
  92. sa.Length = uint32(unsafe.Sizeof(sa))
  93. sa.InheritHandle = 1
  94. sa.SecurityDescriptor = uintptr(unsafe.Pointer(&sd[0]))
  95. namep, err := syscall.UTF16PtrFromString(name)
  96. if err != nil {
  97. return &os.PathError{Op: "mkdir", Path: name, Err: err}
  98. }
  99. e := syscall.CreateDirectory(namep, &sa)
  100. if e != nil {
  101. return &os.PathError{Op: "mkdir", Path: name, Err: e}
  102. }
  103. return nil
  104. }
  105. // IsAbs is a platform-specific wrapper for filepath.IsAbs. On Windows,
  106. // golang filepath.IsAbs does not consider a path \windows\system32 as absolute
  107. // as it doesn't start with a drive-letter/colon combination. However, in
  108. // docker we need to verify things such as WORKDIR /windows/system32 in
  109. // a Dockerfile (which gets translated to \windows\system32 when being processed
  110. // by the daemon. This SHOULD be treated as absolute from a docker processing
  111. // perspective.
  112. func IsAbs(path string) bool {
  113. if !filepath.IsAbs(path) {
  114. if !strings.HasPrefix(path, string(os.PathSeparator)) {
  115. return false
  116. }
  117. }
  118. return true
  119. }
  120. // The origin of the functions below here are the golang OS and syscall packages,
  121. // slightly modified to only cope with files, not directories due to the
  122. // specific use case.
  123. //
  124. // The alteration is to allow a file on Windows to be opened with
  125. // FILE_FLAG_SEQUENTIAL_SCAN (particular for docker load), to avoid eating
  126. // the standby list, particularly when accessing large files such as layer.tar.
  127. // CreateSequential creates the named file with mode 0666 (before umask), truncating
  128. // it if it already exists. If successful, methods on the returned
  129. // File can be used for I/O; the associated file descriptor has mode
  130. // O_RDWR.
  131. // If there is an error, it will be of type *PathError.
  132. func CreateSequential(name string) (*os.File, error) {
  133. return OpenFileSequential(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0)
  134. }
  135. // OpenSequential opens the named file for reading. If successful, methods on
  136. // the returned file can be used for reading; the associated file
  137. // descriptor has mode O_RDONLY.
  138. // If there is an error, it will be of type *PathError.
  139. func OpenSequential(name string) (*os.File, error) {
  140. return OpenFileSequential(name, os.O_RDONLY, 0)
  141. }
  142. // OpenFileSequential is the generalized open call; most users will use Open
  143. // or Create instead.
  144. // If there is an error, it will be of type *PathError.
  145. func OpenFileSequential(name string, flag int, _ os.FileMode) (*os.File, error) {
  146. if name == "" {
  147. return nil, &os.PathError{Op: "open", Path: name, Err: syscall.ENOENT}
  148. }
  149. r, errf := syscallOpenFileSequential(name, flag, 0)
  150. if errf == nil {
  151. return r, nil
  152. }
  153. return nil, &os.PathError{Op: "open", Path: name, Err: errf}
  154. }
  155. func syscallOpenFileSequential(name string, flag int, _ os.FileMode) (file *os.File, err error) {
  156. r, e := syscallOpenSequential(name, flag|syscall.O_CLOEXEC, 0)
  157. if e != nil {
  158. return nil, e
  159. }
  160. return os.NewFile(uintptr(r), name), nil
  161. }
  162. func makeInheritSa() *syscall.SecurityAttributes {
  163. var sa syscall.SecurityAttributes
  164. sa.Length = uint32(unsafe.Sizeof(sa))
  165. sa.InheritHandle = 1
  166. return &sa
  167. }
  168. func syscallOpenSequential(path string, mode int, _ uint32) (fd syscall.Handle, err error) {
  169. if len(path) == 0 {
  170. return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
  171. }
  172. pathp, err := syscall.UTF16PtrFromString(path)
  173. if err != nil {
  174. return syscall.InvalidHandle, err
  175. }
  176. var access uint32
  177. switch mode & (syscall.O_RDONLY | syscall.O_WRONLY | syscall.O_RDWR) {
  178. case syscall.O_RDONLY:
  179. access = syscall.GENERIC_READ
  180. case syscall.O_WRONLY:
  181. access = syscall.GENERIC_WRITE
  182. case syscall.O_RDWR:
  183. access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
  184. }
  185. if mode&syscall.O_CREAT != 0 {
  186. access |= syscall.GENERIC_WRITE
  187. }
  188. if mode&syscall.O_APPEND != 0 {
  189. access &^= syscall.GENERIC_WRITE
  190. access |= syscall.FILE_APPEND_DATA
  191. }
  192. sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE)
  193. var sa *syscall.SecurityAttributes
  194. if mode&syscall.O_CLOEXEC == 0 {
  195. sa = makeInheritSa()
  196. }
  197. var createmode uint32
  198. switch {
  199. case mode&(syscall.O_CREAT|syscall.O_EXCL) == (syscall.O_CREAT | syscall.O_EXCL):
  200. createmode = syscall.CREATE_NEW
  201. case mode&(syscall.O_CREAT|syscall.O_TRUNC) == (syscall.O_CREAT | syscall.O_TRUNC):
  202. createmode = syscall.CREATE_ALWAYS
  203. case mode&syscall.O_CREAT == syscall.O_CREAT:
  204. createmode = syscall.OPEN_ALWAYS
  205. case mode&syscall.O_TRUNC == syscall.O_TRUNC:
  206. createmode = syscall.TRUNCATE_EXISTING
  207. default:
  208. createmode = syscall.OPEN_EXISTING
  209. }
  210. // Use FILE_FLAG_SEQUENTIAL_SCAN rather than FILE_ATTRIBUTE_NORMAL as implemented in golang.
  211. //https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
  212. const fileFlagSequentialScan = 0x08000000 // FILE_FLAG_SEQUENTIAL_SCAN
  213. h, e := syscall.CreateFile(pathp, access, sharemode, sa, createmode, fileFlagSequentialScan, 0)
  214. return h, e
  215. }
  216. // Helpers for TempFileSequential
  217. var rand uint32
  218. var randmu sync.Mutex
  219. func reseed() uint32 {
  220. return uint32(time.Now().UnixNano() + int64(os.Getpid()))
  221. }
  222. func nextSuffix() string {
  223. randmu.Lock()
  224. r := rand
  225. if r == 0 {
  226. r = reseed()
  227. }
  228. r = r*1664525 + 1013904223 // constants from Numerical Recipes
  229. rand = r
  230. randmu.Unlock()
  231. return strconv.Itoa(int(1e9 + r%1e9))[1:]
  232. }
  233. // TempFileSequential is a copy of ioutil.TempFile, modified to use sequential
  234. // file access. Below is the original comment from golang:
  235. // TempFile creates a new temporary file in the directory dir
  236. // with a name beginning with prefix, opens the file for reading
  237. // and writing, and returns the resulting *os.File.
  238. // If dir is the empty string, TempFile uses the default directory
  239. // for temporary files (see os.TempDir).
  240. // Multiple programs calling TempFile simultaneously
  241. // will not choose the same file. The caller can use f.Name()
  242. // to find the pathname of the file. It is the caller's responsibility
  243. // to remove the file when no longer needed.
  244. func TempFileSequential(dir, prefix string) (f *os.File, err error) {
  245. if dir == "" {
  246. dir = os.TempDir()
  247. }
  248. nconflict := 0
  249. for i := 0; i < 10000; i++ {
  250. name := filepath.Join(dir, prefix+nextSuffix())
  251. f, err = OpenFileSequential(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
  252. if os.IsExist(err) {
  253. if nconflict++; nconflict > 10 {
  254. randmu.Lock()
  255. rand = reseed()
  256. randmu.Unlock()
  257. }
  258. continue
  259. }
  260. break
  261. }
  262. return
  263. }