github.com/hauerwu/docker@v1.8.0-rc1/pkg/system/filesys_windows.go (about)

     1  // +build windows
     2  
     3  package system
     4  
     5  import (
     6  	"os"
     7  	"regexp"
     8  	"syscall"
     9  )
    10  
    11  // MkdirAll implementation that is volume path aware for Windows.
    12  func MkdirAll(path string, perm os.FileMode) error {
    13  	if re := regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}$`); re.MatchString(path) {
    14  		return nil
    15  	}
    16  
    17  	// The rest of this method is copied from os.MkdirAll and should be kept
    18  	// as-is to ensure compatibility.
    19  
    20  	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
    21  	dir, err := os.Stat(path)
    22  	if err == nil {
    23  		if dir.IsDir() {
    24  			return nil
    25  		}
    26  		return &os.PathError{
    27  			Op:   "mkdir",
    28  			Path: path,
    29  			Err:  syscall.ENOTDIR,
    30  		}
    31  	}
    32  
    33  	// Slow path: make sure parent exists and then call Mkdir for path.
    34  	i := len(path)
    35  	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
    36  		i--
    37  	}
    38  
    39  	j := i
    40  	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
    41  		j--
    42  	}
    43  
    44  	if j > 1 {
    45  		// Create parent
    46  		err = MkdirAll(path[0:j-1], perm)
    47  		if err != nil {
    48  			return err
    49  		}
    50  	}
    51  
    52  	// Parent now exists; invoke Mkdir and use its result.
    53  	err = os.Mkdir(path, perm)
    54  	if err != nil {
    55  		// Handle arguments like "foo/." by
    56  		// double-checking that directory doesn't exist.
    57  		dir, err1 := os.Lstat(path)
    58  		if err1 == nil && dir.IsDir() {
    59  			return nil
    60  		}
    61  		return err
    62  	}
    63  	return nil
    64  }