github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/dockerfile/internals_windows.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/pkg/system"
    10  )
    11  
    12  // normaliseDest normalises the destination of a COPY/ADD command in a
    13  // platform semantically consistent way.
    14  func normaliseDest(cmdName, workingDir, requested string) (string, error) {
    15  	dest := filepath.FromSlash(requested)
    16  	endsInSlash := strings.HasSuffix(dest, string(os.PathSeparator))
    17  
    18  	// We are guaranteed that the working directory is already consistent,
    19  	// However, Windows also has, for now, the limitation that ADD/COPY can
    20  	// only be done to the system drive, not any drives that might be present
    21  	// as a result of a bind mount.
    22  	//
    23  	// So... if the path requested is Linux-style absolute (/foo or \\foo),
    24  	// we assume it is the system drive. If it is a Windows-style absolute
    25  	// (DRIVE:\\foo), error if DRIVE is not C. And finally, ensure we
    26  	// strip any configured working directories drive letter so that it
    27  	// can be subsequently legitimately converted to a Windows volume-style
    28  	// pathname.
    29  
    30  	// Not a typo - filepath.IsAbs, not system.IsAbs on this next check as
    31  	// we only want to validate where the DriveColon part has been supplied.
    32  	if filepath.IsAbs(dest) {
    33  		if strings.ToUpper(string(dest[0])) != "C" {
    34  			return "", fmt.Errorf("Windows does not support %s with a destinations not on the system drive (C:)", cmdName)
    35  		}
    36  		dest = dest[2:] // Strip the drive letter
    37  	}
    38  
    39  	// Cannot handle relative where WorkingDir is not the system drive.
    40  	if len(workingDir) > 0 {
    41  		if ((len(workingDir) > 1) && !system.IsAbs(workingDir[2:])) || (len(workingDir) == 1) {
    42  			return "", fmt.Errorf("Current WorkingDir %s is not platform consistent", workingDir)
    43  		}
    44  		if !system.IsAbs(dest) {
    45  			if string(workingDir[0]) != "C" {
    46  				return "", fmt.Errorf("Windows does not support %s with relative paths when WORKDIR is not the system drive", cmdName)
    47  			}
    48  			dest = filepath.Join(string(os.PathSeparator), workingDir[2:], dest)
    49  			// Make sure we preserve any trailing slash
    50  			if endsInSlash {
    51  				dest += string(os.PathSeparator)
    52  			}
    53  		}
    54  	}
    55  	return dest, nil
    56  }
    57  
    58  func containsWildcards(name string) bool {
    59  	for i := 0; i < len(name); i++ {
    60  		ch := name[i]
    61  		if ch == '*' || ch == '?' || ch == '[' {
    62  			return true
    63  		}
    64  	}
    65  	return false
    66  }