From 49c1b51ae22e54edc2e13f3195583e47b987fc15 Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 26 Aug 2015 16:39:16 -0700 Subject: [PATCH] Windows: Fix absolute checks Signed-off-by: John Howard --- api/client/cp.go | 5 +++-- builder/dispatchers.go | 39 ++++++++------------------------------- builder/internals.go | 2 +- pkg/archive/copy.go | 3 ++- pkg/symlink/fs.go | 4 +++- 5 files changed, 17 insertions(+), 36 deletions(-) diff --git a/api/client/cp.go b/api/client/cp.go index a36212a79a..4876ae2cdc 100644 --- a/api/client/cp.go +++ b/api/client/cp.go @@ -15,6 +15,7 @@ import ( Cli "github.com/docker/docker/cli" "github.com/docker/docker/pkg/archive" flag "github.com/docker/docker/pkg/mflag" + "github.com/docker/docker/pkg/system" ) type copyDirection int @@ -101,7 +102,7 @@ func (cli *DockerCli) CmdCp(args ...string) error { // client, a `:` could be part of an absolute Windows path, in which case it // is immediately proceeded by a backslash. func splitCpArg(arg string) (container, path string) { - if filepath.IsAbs(arg) { + if system.IsAbs(arg) { // Explicit local absolute path, e.g., `C:\foo` or `/foo`. return "", arg } @@ -236,7 +237,7 @@ func (cli *DockerCli) copyToContainer(srcPath, dstContainer, dstPath string) (er // If the destination is a symbolic link, we should evaluate it. if err == nil && dstStat.Mode&os.ModeSymlink != 0 { linkTarget := dstStat.LinkTarget - if !filepath.IsAbs(linkTarget) { + if !system.IsAbs(linkTarget) { // Join with the parent directory. dstParent, _ := archive.SplitPathDirEntry(dstPath) linkTarget = filepath.Join(dstParent, linkTarget) diff --git a/builder/dispatchers.go b/builder/dispatchers.go index 00e85b9236..78e3f87229 100644 --- a/builder/dispatchers.go +++ b/builder/dispatchers.go @@ -10,7 +10,7 @@ package builder import ( "fmt" "io/ioutil" - "path" + "os" "path/filepath" "regexp" "runtime" @@ -21,6 +21,7 @@ import ( flag "github.com/docker/docker/pkg/mflag" "github.com/docker/docker/pkg/nat" "github.com/docker/docker/pkg/stringutils" + "github.com/docker/docker/pkg/system" "github.com/docker/docker/runconfig" ) @@ -272,39 +273,15 @@ func workdir(b *builder, args []string, attributes map[string]bool, original str return err } - // Note that workdir passed comes from the Dockerfile. Hence it is in - // Linux format using forward-slashes, even on Windows. However, - // b.Config.WorkingDir is in platform-specific notation (in other words - // on Windows will use `\` - workdir := args[0] + // This is from the Dockerfile and will not necessarily be in platform + // specific semantics, hence ensure it is converted. + workdir := filepath.FromSlash(args[0]) - isAbs := false - if runtime.GOOS == "windows" { - // Alternate processing for Windows here is necessary as we can't call - // filepath.IsAbs(workDir) as that would verify Windows style paths, - // along with drive-letters (eg c:\pathto\file.txt). We (arguably - // correctly or not) check for both forward and back slashes as this - // is what the 1.4.2 GoLang implementation of IsAbs() does in the - // isSlash() function. - isAbs = workdir[0] == '\\' || workdir[0] == '/' - } else { - isAbs = filepath.IsAbs(workdir) + if !system.IsAbs(workdir) { + current := filepath.FromSlash(b.Config.WorkingDir) + workdir = filepath.Join(string(os.PathSeparator), current, workdir) } - if !isAbs { - current := b.Config.WorkingDir - if runtime.GOOS == "windows" { - // Convert to Linux format before join - current = strings.Replace(current, "\\", "/", -1) - } - // Must use path.Join so works correctly on Windows, not filepath - workdir = path.Join("/", current, workdir) - } - - // Convert to platform specific format - if runtime.GOOS == "windows" { - workdir = strings.Replace(workdir, "/", "\\", -1) - } b.Config.WorkingDir = workdir return b.commit("", b.Config.Cmd, fmt.Sprintf("WORKDIR %v", workdir)) diff --git a/builder/internals.go b/builder/internals.go index e7ce1b450a..dbd661c28f 100644 --- a/builder/internals.go +++ b/builder/internals.go @@ -270,7 +270,7 @@ func calcCopyInfo(b *builder, cmdName string, cInfos *[]*copyInfo, origPath stri // Twiddle the destPath when its a relative path - meaning, make it // relative to the WORKINGDIR - if !filepath.IsAbs(destPath) { + if !system.IsAbs(destPath) { hasSlash := strings.HasSuffix(destPath, string(os.PathSeparator)) destPath = filepath.Join(string(os.PathSeparator), filepath.FromSlash(b.Config.WorkingDir), destPath) diff --git a/pkg/archive/copy.go b/pkg/archive/copy.go index a7ed12c1b3..ecfd0a9c69 100644 --- a/pkg/archive/copy.go +++ b/pkg/archive/copy.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/Sirupsen/logrus" + "github.com/docker/docker/pkg/system" ) // Errors used or returned by this file. @@ -210,7 +211,7 @@ func CopyInfoDestinationPath(path string) (info CopyInfo, err error) { return CopyInfo{}, err } - if !filepath.IsAbs(linkTarget) { + if !system.IsAbs(linkTarget) { // Join with the parent directory. dstParent, _ := SplitPathDirEntry(path) linkTarget = filepath.Join(dstParent, linkTarget) diff --git a/pkg/symlink/fs.go b/pkg/symlink/fs.go index ef12f06bae..d305500453 100644 --- a/pkg/symlink/fs.go +++ b/pkg/symlink/fs.go @@ -12,6 +12,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/docker/docker/pkg/system" ) // FollowSymlinkInScope is a wrapper around evalSymlinksInScope that returns an @@ -120,7 +122,7 @@ func evalSymlinksInScope(path, root string) (string, error) { if err != nil { return "", err } - if filepath.IsAbs(dest) { + if system.IsAbs(dest) { b.Reset() } path = dest + string(filepath.Separator) + path