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