github.com/jingleWang/moby@v1.13.1/pkg/system/path_windows.go (about)

     1  // +build windows
     2  
     3  package system
     4  
     5  import (
     6  	"fmt"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // DefaultPathEnv is deliberately empty on Windows as the default path will be set by
    12  // the container. Docker has no context of what the default path should be.
    13  const DefaultPathEnv = ""
    14  
    15  // CheckSystemDriveAndRemoveDriveLetter verifies and manipulates a Windows path.
    16  // This is used, for example, when validating a user provided path in docker cp.
    17  // If a drive letter is supplied, it must be the system drive. The drive letter
    18  // is always removed. Also, it translates it to OS semantics (IOW / to \). We
    19  // need the path in this syntax so that it can ultimately be contatenated with
    20  // a Windows long-path which doesn't support drive-letters. Examples:
    21  // C:			--> Fail
    22  // C:\			--> \
    23  // a			--> a
    24  // /a			--> \a
    25  // d:\			--> Fail
    26  func CheckSystemDriveAndRemoveDriveLetter(path string) (string, error) {
    27  	if len(path) == 2 && string(path[1]) == ":" {
    28  		return "", fmt.Errorf("No relative path specified in %q", path)
    29  	}
    30  	if !filepath.IsAbs(path) || len(path) < 2 {
    31  		return filepath.FromSlash(path), nil
    32  	}
    33  	if string(path[1]) == ":" && !strings.EqualFold(string(path[0]), "c") {
    34  		return "", fmt.Errorf("The specified path is not on the system drive (C:)")
    35  	}
    36  	return filepath.FromSlash(path[2:]), nil
    37  }