github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/system/path.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  
     9  	"github.com/containerd/continuity/pathdriver"
    10  )
    11  
    12  const defaultUnixPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    13  
    14  // DefaultPathEnv is unix style list of directories to search for
    15  // executables. Each directory is separated from the next by a colon
    16  // ':' character .
    17  func DefaultPathEnv(os string) string {
    18  	if runtime.GOOS == "windows" {
    19  		if os != runtime.GOOS {
    20  			return defaultUnixPathEnv
    21  		}
    22  		// Deliberately empty on Windows containers on Windows as the default path will be set by
    23  		// the container. Docker has no context of what the default path should be.
    24  		return ""
    25  	}
    26  	return defaultUnixPathEnv
    27  
    28  }
    29  
    30  // CheckSystemDriveAndRemoveDriveLetter verifies that a path, if it includes a drive letter,
    31  // is the system drive.
    32  // On Linux: this is a no-op.
    33  // On Windows: this does the following>
    34  // CheckSystemDriveAndRemoveDriveLetter verifies and manipulates a Windows path.
    35  // This is used, for example, when validating a user provided path in docker cp.
    36  // If a drive letter is supplied, it must be the system drive. The drive letter
    37  // is always removed. Also, it translates it to OS semantics (IOW / to \). We
    38  // need the path in this syntax so that it can ultimately be contatenated with
    39  // a Windows long-path which doesn't support drive-letters. Examples:
    40  // C:			--> Fail
    41  // C:\			--> \
    42  // a			--> a
    43  // /a			--> \a
    44  // d:\			--> Fail
    45  func CheckSystemDriveAndRemoveDriveLetter(path string, driver pathdriver.PathDriver) (string, error) {
    46  	if runtime.GOOS != "windows" || LCOWSupported() {
    47  		return path, nil
    48  	}
    49  
    50  	if len(path) == 2 && string(path[1]) == ":" {
    51  		return "", fmt.Errorf("No relative path specified in %q", path)
    52  	}
    53  	if !driver.IsAbs(path) || len(path) < 2 {
    54  		return filepath.FromSlash(path), nil
    55  	}
    56  	if string(path[1]) == ":" && !strings.EqualFold(string(path[0]), "c") {
    57  		return "", fmt.Errorf("The specified path is not on the system drive (C:)")
    58  	}
    59  	return filepath.FromSlash(path[2:]), nil
    60  }