github.com/olljanat/moby@v1.13.1/builder/dockerfile/dispatchers_windows.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/pkg/system"
    11  )
    12  
    13  var pattern = regexp.MustCompile(`^[a-zA-Z]:\.$`)
    14  
    15  // normaliseWorkdir normalises a user requested working directory in a
    16  // platform sematically consistent way.
    17  func normaliseWorkdir(current string, requested string) (string, error) {
    18  	if requested == "" {
    19  		return "", fmt.Errorf("cannot normalise nothing")
    20  	}
    21  
    22  	// `filepath.Clean` will replace "" with "." so skip in that case
    23  	if current != "" {
    24  		current = filepath.Clean(current)
    25  	}
    26  	if requested != "" {
    27  		requested = filepath.Clean(requested)
    28  	}
    29  
    30  	// If either current or requested in Windows is:
    31  	// C:
    32  	// C:.
    33  	// then an error will be thrown as the definition for the above
    34  	// refers to `current directory on drive C:`
    35  	// Since filepath.Clean() will automatically normalize the above
    36  	// to `C:.`, we only need to check the last format
    37  	if pattern.MatchString(current) {
    38  		return "", fmt.Errorf("%s is not a directory. If you are specifying a drive letter, please add a trailing '\\'", current)
    39  	}
    40  	if pattern.MatchString(requested) {
    41  		return "", fmt.Errorf("%s is not a directory. If you are specifying a drive letter, please add a trailing '\\'", requested)
    42  	}
    43  
    44  	// Target semantics is C:\somefolder, specifically in the format:
    45  	// UPPERCASEDriveLetter-Colon-Backslash-FolderName. We are already
    46  	// guaranteed that `current`, if set, is consistent. This allows us to
    47  	// cope correctly with any of the following in a Dockerfile:
    48  	//	WORKDIR a                       --> C:\a
    49  	//	WORKDIR c:\\foo                 --> C:\foo
    50  	//	WORKDIR \\foo                   --> C:\foo
    51  	//	WORKDIR /foo                    --> C:\foo
    52  	//	WORKDIR c:\\foo \ WORKDIR bar   --> C:\foo --> C:\foo\bar
    53  	//	WORKDIR C:/foo \ WORKDIR bar    --> C:\foo --> C:\foo\bar
    54  	//	WORKDIR C:/foo \ WORKDIR \\bar  --> C:\foo --> C:\bar
    55  	//	WORKDIR /foo \ WORKDIR c:/bar   --> C:\foo --> C:\bar
    56  	if len(current) == 0 || system.IsAbs(requested) {
    57  		if (requested[0] == os.PathSeparator) ||
    58  			(len(requested) > 1 && string(requested[1]) != ":") ||
    59  			(len(requested) == 1) {
    60  			requested = filepath.Join(`C:\`, requested)
    61  		}
    62  	} else {
    63  		requested = filepath.Join(current, requested)
    64  	}
    65  	// Upper-case drive letter
    66  	return (strings.ToUpper(string(requested[0])) + requested[1:]), nil
    67  }
    68  
    69  func errNotJSON(command, original string) error {
    70  	// For Windows users, give a hint if it looks like it might contain
    71  	// a path which hasn't been escaped such as ["c:\windows\system32\prog.exe", "-param"],
    72  	// as JSON must be escaped. Unfortunate...
    73  	//
    74  	// Specifically looking for quote-driveletter-colon-backslash, there's no
    75  	// double backslash and a [] pair. No, this is not perfect, but it doesn't
    76  	// have to be. It's simply a hint to make life a little easier.
    77  	extra := ""
    78  	original = filepath.FromSlash(strings.ToLower(strings.Replace(strings.ToLower(original), strings.ToLower(command)+" ", "", -1)))
    79  	if len(regexp.MustCompile(`"[a-z]:\\.*`).FindStringSubmatch(original)) > 0 &&
    80  		!strings.Contains(original, `\\`) &&
    81  		strings.Contains(original, "[") &&
    82  		strings.Contains(original, "]") {
    83  		extra = fmt.Sprintf(`. It looks like '%s' includes a file path without an escaped back-slash. JSON requires back-slashes to be escaped such as ["c:\\path\\to\\file.exe", "/parameter"]`, original)
    84  	}
    85  	return fmt.Errorf("%s requires the arguments to be in JSON form%s", command, extra)
    86  }