Skip to content

Commit

Permalink
Merge pull request moby#15871 from Microsoft/10662-absolute
Browse files Browse the repository at this point in the history
Windows: Fix absolute checks
  • Loading branch information
calavera committed Sep 1, 2015
2 parents 7f58fb5 + 49c1b51 commit 2093616
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
5 changes: 3 additions & 2 deletions api/client/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 8 additions & 31 deletions builder/dispatchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package builder
import (
"fmt"
"io/ioutil"
"path"
"os"
"path/filepath"
"regexp"
"runtime"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 !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)
if !system.IsAbs(workdir) {
current := filepath.FromSlash(b.Config.WorkingDir)
workdir = filepath.Join(string(os.PathSeparator), 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))
Expand Down
2 changes: 1 addition & 1 deletion builder/internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion pkg/archive/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/system"
)

// Errors used or returned by this file.
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/symlink/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/docker/docker/pkg/system"
)

// FollowSymlinkInScope is a wrapper around evalSymlinksInScope that returns an
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2093616

Please sign in to comment.