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