github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/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  	"golang.org/x/sys/windows"
    15  )
    16  
    17  const (
    18  	// SddlAdministratorsLocalSystem is local administrators plus NT AUTHORITY\System
    19  	SddlAdministratorsLocalSystem = "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
    20  )
    21  
    22  // MkdirAllWithACL is a wrapper for MkdirAll that creates a directory
    23  // with an appropriate SDDL defined ACL.
    24  func MkdirAllWithACL(path string, perm os.FileMode, sddl string) error {
    25  	return mkdirall(path, true, sddl)
    26  }
    27  
    28  // MkdirAll implementation that is volume path aware for Windows. It can be used
    29  // as a drop-in replacement for os.MkdirAll()
    30  func MkdirAll(path string, _ os.FileMode) error {
    31  	return mkdirall(path, false, "")
    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 := windows.SecurityDescriptorFromString(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 = sd
   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) || strings.HasPrefix(path, string(os.PathSeparator)) {
   134  		return true
   135  	}
   136  	return false
   137  }
   138  
   139  // The origin of the functions below here are the golang OS and windows packages,
   140  // slightly modified to only cope with files, not directories due to the
   141  // specific use case.
   142  //
   143  // The alteration is to allow a file on Windows to be opened with
   144  // FILE_FLAG_SEQUENTIAL_SCAN (particular for docker load), to avoid eating
   145  // the standby list, particularly when accessing large files such as layer.tar.
   146  
   147  // CreateSequential creates the named file with mode 0666 (before umask), truncating
   148  // it if it already exists. If successful, methods on the returned
   149  // File can be used for I/O; the associated file descriptor has mode
   150  // O_RDWR.
   151  // If there is an error, it will be of type *PathError.
   152  func CreateSequential(name string) (*os.File, error) {
   153  	return OpenFileSequential(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0)
   154  }
   155  
   156  // OpenSequential opens the named file for reading. If successful, methods on
   157  // the returned file can be used for reading; the associated file
   158  // descriptor has mode O_RDONLY.
   159  // If there is an error, it will be of type *PathError.
   160  func OpenSequential(name string) (*os.File, error) {
   161  	return OpenFileSequential(name, os.O_RDONLY, 0)
   162  }
   163  
   164  // OpenFileSequential is the generalized open call; most users will use Open
   165  // or Create instead.
   166  // If there is an error, it will be of type *PathError.
   167  func OpenFileSequential(name string, flag int, _ os.FileMode) (*os.File, error) {
   168  	if name == "" {
   169  		return nil, &os.PathError{Op: "open", Path: name, Err: syscall.ENOENT}
   170  	}
   171  	r, errf := windowsOpenFileSequential(name, flag, 0)
   172  	if errf == nil {
   173  		return r, nil
   174  	}
   175  	return nil, &os.PathError{Op: "open", Path: name, Err: errf}
   176  }
   177  
   178  func windowsOpenFileSequential(name string, flag int, _ os.FileMode) (file *os.File, err error) {
   179  	r, e := windowsOpenSequential(name, flag|windows.O_CLOEXEC, 0)
   180  	if e != nil {
   181  		return nil, e
   182  	}
   183  	return os.NewFile(uintptr(r), name), nil
   184  }
   185  
   186  func makeInheritSa() *windows.SecurityAttributes {
   187  	var sa windows.SecurityAttributes
   188  	sa.Length = uint32(unsafe.Sizeof(sa))
   189  	sa.InheritHandle = 1
   190  	return &sa
   191  }
   192  
   193  func windowsOpenSequential(path string, mode int, _ uint32) (fd windows.Handle, err error) {
   194  	if len(path) == 0 {
   195  		return windows.InvalidHandle, windows.ERROR_FILE_NOT_FOUND
   196  	}
   197  	pathp, err := windows.UTF16PtrFromString(path)
   198  	if err != nil {
   199  		return windows.InvalidHandle, err
   200  	}
   201  	var access uint32
   202  	switch mode & (windows.O_RDONLY | windows.O_WRONLY | windows.O_RDWR) {
   203  	case windows.O_RDONLY:
   204  		access = windows.GENERIC_READ
   205  	case windows.O_WRONLY:
   206  		access = windows.GENERIC_WRITE
   207  	case windows.O_RDWR:
   208  		access = windows.GENERIC_READ | windows.GENERIC_WRITE
   209  	}
   210  	if mode&windows.O_CREAT != 0 {
   211  		access |= windows.GENERIC_WRITE
   212  	}
   213  	if mode&windows.O_APPEND != 0 {
   214  		access &^= windows.GENERIC_WRITE
   215  		access |= windows.FILE_APPEND_DATA
   216  	}
   217  	sharemode := uint32(windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE)
   218  	var sa *windows.SecurityAttributes
   219  	if mode&windows.O_CLOEXEC == 0 {
   220  		sa = makeInheritSa()
   221  	}
   222  	var createmode uint32
   223  	switch {
   224  	case mode&(windows.O_CREAT|windows.O_EXCL) == (windows.O_CREAT | windows.O_EXCL):
   225  		createmode = windows.CREATE_NEW
   226  	case mode&(windows.O_CREAT|windows.O_TRUNC) == (windows.O_CREAT | windows.O_TRUNC):
   227  		createmode = windows.CREATE_ALWAYS
   228  	case mode&windows.O_CREAT == windows.O_CREAT:
   229  		createmode = windows.OPEN_ALWAYS
   230  	case mode&windows.O_TRUNC == windows.O_TRUNC:
   231  		createmode = windows.TRUNCATE_EXISTING
   232  	default:
   233  		createmode = windows.OPEN_EXISTING
   234  	}
   235  	// Use FILE_FLAG_SEQUENTIAL_SCAN rather than FILE_ATTRIBUTE_NORMAL as implemented in golang.
   236  	// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
   237  	const fileFlagSequentialScan = 0x08000000 // FILE_FLAG_SEQUENTIAL_SCAN
   238  	h, e := windows.CreateFile(pathp, access, sharemode, sa, createmode, fileFlagSequentialScan, 0)
   239  	return h, e
   240  }
   241  
   242  // Helpers for TempFileSequential
   243  var rand uint32
   244  var randmu sync.Mutex
   245  
   246  func reseed() uint32 {
   247  	return uint32(time.Now().UnixNano() + int64(os.Getpid()))
   248  }
   249  func nextSuffix() string {
   250  	randmu.Lock()
   251  	r := rand
   252  	if r == 0 {
   253  		r = reseed()
   254  	}
   255  	r = r*1664525 + 1013904223 // constants from Numerical Recipes
   256  	rand = r
   257  	randmu.Unlock()
   258  	return strconv.Itoa(int(1e9 + r%1e9))[1:]
   259  }
   260  
   261  // TempFileSequential is a copy of ioutil.TempFile, modified to use sequential
   262  // file access. Below is the original comment from golang:
   263  // TempFile creates a new temporary file in the directory dir
   264  // with a name beginning with prefix, opens the file for reading
   265  // and writing, and returns the resulting *os.File.
   266  // If dir is the empty string, TempFile uses the default directory
   267  // for temporary files (see os.TempDir).
   268  // Multiple programs calling TempFile simultaneously
   269  // will not choose the same file. The caller can use f.Name()
   270  // to find the pathname of the file. It is the caller's responsibility
   271  // to remove the file when no longer needed.
   272  func TempFileSequential(dir, prefix string) (f *os.File, err error) {
   273  	if dir == "" {
   274  		dir = os.TempDir()
   275  	}
   276  
   277  	nconflict := 0
   278  	for i := 0; i < 10000; i++ {
   279  		name := filepath.Join(dir, prefix+nextSuffix())
   280  		f, err = OpenFileSequential(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
   281  		if os.IsExist(err) {
   282  			if nconflict++; nconflict > 10 {
   283  				randmu.Lock()
   284  				rand = reseed()
   285  				randmu.Unlock()
   286  			}
   287  			continue
   288  		}
   289  		break
   290  	}
   291  	return
   292  }