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