From 5ecab9e831c6d39e649430013087a3e2a85fb949 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Tue, 24 Feb 2015 19:43:29 -0800 Subject: [PATCH] Support windows style dockerfile paths for build cmd Currently TestBuildRenamedDockerfile fails since passing custom dockerfile paths like: docker build -f dir/file . fails on windows because those are unix paths. Instead, on windows accept windows style paths like: docker build -f dir\file . and convert them to unix style paths using the helper we have in `pkg/archive` so that daemon can correctly locate the path in the context. Signed-off-by: Ahmet Alp Balkan --- api/client/commands.go | 8 ++++++-- pkg/archive/archive.go | 2 +- pkg/archive/archive_unix.go | 2 +- pkg/archive/archive_unix_test.go | 2 +- pkg/archive/archive_windows.go | 2 +- pkg/archive/archive_windows_test.go | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/api/client/commands.go b/api/client/commands.go index 241913291..64801fabe 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -155,11 +155,10 @@ func (cli *DockerCli) CmdBuild(args ...string) error { if *dockerfileName == "" { // No -f/--file was specified so use the default *dockerfileName = api.DefaultDockerfileName - filename = path.Join(absRoot, *dockerfileName) + filename = filepath.Join(absRoot, *dockerfileName) } origDockerfile := *dockerfileName // used for error msg - if filename, err = filepath.Abs(filename); err != nil { return err } @@ -175,6 +174,11 @@ func (cli *DockerCli) CmdBuild(args ...string) error { if err != nil { return err } + // And canonicalize dockerfile name to a platform-independent one + *dockerfileName, err = archive.CanonicalTarNameForPath(*dockerfileName) + if err != nil { + return fmt.Errorf("Cannot canonicalize dockerfile path %s: %v", dockerfileName, err) + } if _, err = os.Lstat(filename); os.IsNotExist(err) { return fmt.Errorf("Cannot locate Dockerfile: %s", origDockerfile) diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 40b1a406f..bce66a505 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -175,7 +175,7 @@ type tarAppender struct { // canonicalTarName provides a platform-independent and consistent posix-style //path for files and directories to be archived regardless of the platform. func canonicalTarName(name string, isDir bool) (string, error) { - name, err := canonicalTarNameForPath(name) + name, err := CanonicalTarNameForPath(name) if err != nil { return "", err } diff --git a/pkg/archive/archive_unix.go b/pkg/archive/archive_unix.go index 19590ec95..8c7079f85 100644 --- a/pkg/archive/archive_unix.go +++ b/pkg/archive/archive_unix.go @@ -12,7 +12,7 @@ import ( // canonicalTarNameForPath returns platform-specific filepath // to canonical posix-style path for tar archival. p is relative // path. -func canonicalTarNameForPath(p string) (string, error) { +func CanonicalTarNameForPath(p string) (string, error) { return p, nil // already unix-style } diff --git a/pkg/archive/archive_unix_test.go b/pkg/archive/archive_unix_test.go index 27cfd772d..52f28e20f 100644 --- a/pkg/archive/archive_unix_test.go +++ b/pkg/archive/archive_unix_test.go @@ -13,7 +13,7 @@ func TestCanonicalTarNameForPath(t *testing.T) { {"foo/dir/", "foo/dir/"}, } for _, v := range cases { - if out, err := canonicalTarNameForPath(v.in); err != nil { + if out, err := CanonicalTarNameForPath(v.in); err != nil { t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err) } else if out != v.expected { t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out) diff --git a/pkg/archive/archive_windows.go b/pkg/archive/archive_windows.go index b76384429..b95aa178d 100644 --- a/pkg/archive/archive_windows.go +++ b/pkg/archive/archive_windows.go @@ -12,7 +12,7 @@ import ( // canonicalTarNameForPath returns platform-specific filepath // to canonical posix-style path for tar archival. p is relative // path. -func canonicalTarNameForPath(p string) (string, error) { +func CanonicalTarNameForPath(p string) (string, error) { // windows: convert windows style relative path with backslashes // into forward slashes. since windows does not allow '/' or '\' // in file names, it is mostly safe to replace however we must diff --git a/pkg/archive/archive_windows_test.go b/pkg/archive/archive_windows_test.go index d79f33315..2b7993c29 100644 --- a/pkg/archive/archive_windows_test.go +++ b/pkg/archive/archive_windows_test.go @@ -17,7 +17,7 @@ func TestCanonicalTarNameForPath(t *testing.T) { {`foo\bar`, "foo/bar/", false}, } for _, v := range cases { - if out, err := canonicalTarNameForPath(v.in); err != nil && !v.shouldFail { + if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail { t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err) } else if v.shouldFail && err == nil { t.Fatalf("canonical path call should have pailed with error. in=%s out=%s", v.in, out)