Skip to content

Commit a5aed69

Browse files
author
John Howard
committed
LCOW: lazycontext: Use correct lstat, fix archive check
Signed-off-by: John Howard <[email protected]>
1 parent fda9397 commit a5aed69

6 files changed

Lines changed: 20 additions & 48 deletions

File tree

builder/remotecontext/lazycontext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *lazySource) Hash(path string) (string, error) {
4545
return "", errors.WithStack(convertPathError(err, cleanPath))
4646
}
4747

48-
fi, err := os.Lstat(fullPath)
48+
fi, err := c.root.Lstat(fullPath)
4949
if err != nil {
5050
// Backwards compatibility: a missing file returns a path as hash.
5151
// This is reached in the case of a broken symlink.

pkg/archive/archive.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,7 @@ func FileInfoHeader(name string, fi os.FileInfo, link string) (*tar.Header, erro
367367
hdr.AccessTime = time.Time{}
368368
hdr.ChangeTime = time.Time{}
369369
hdr.Mode = fillGo18FileTypeBits(int64(chmodTarEntry(os.FileMode(hdr.Mode))), fi)
370-
name, err = canonicalTarName(name, fi.IsDir())
371-
if err != nil {
372-
return nil, fmt.Errorf("tar: cannot canonicalize path: %v", err)
373-
}
374-
hdr.Name = name
370+
hdr.Name = canonicalTarName(name, fi.IsDir())
375371
if err := setHeaderForSpecialDevice(hdr, name, fi.Sys()); err != nil {
376372
return nil, err
377373
}
@@ -447,17 +443,14 @@ func newTarAppender(idMapping *idtools.IDMappings, writer io.Writer, chownOpts *
447443

448444
// canonicalTarName provides a platform-independent and consistent posix-style
449445
//path for files and directories to be archived regardless of the platform.
450-
func canonicalTarName(name string, isDir bool) (string, error) {
451-
name, err := CanonicalTarNameForPath(name)
452-
if err != nil {
453-
return "", err
454-
}
446+
func canonicalTarName(name string, isDir bool) string {
447+
name = CanonicalTarNameForPath(name)
455448

456449
// suffix with '/' for directories
457450
if isDir && !strings.HasSuffix(name, "/") {
458451
name += "/"
459452
}
460-
return name, nil
453+
return name
461454
}
462455

463456
// addTarFile adds to the tar archive a file from `path` as `name`

pkg/archive/archive_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func getWalkRoot(srcPath string, include string) string {
3232
// CanonicalTarNameForPath returns platform-specific filepath
3333
// to canonical posix-style path for tar archival. p is relative
3434
// path.
35-
func CanonicalTarNameForPath(p string) (string, error) {
36-
return p, nil // already unix-style
35+
func CanonicalTarNameForPath(p string) string {
36+
return p // already unix-style
3737
}
3838

3939
// chmodTarEntry is used to adjust the file permissions used in tar header based

pkg/archive/archive_unix_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ func TestCanonicalTarNameForPath(t *testing.T) {
2626
{"foo/dir/", "foo/dir/"},
2727
}
2828
for _, v := range cases {
29-
if out, err := CanonicalTarNameForPath(v.in); err != nil {
30-
t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
31-
} else if out != v.expected {
32-
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
29+
if CanonicalTarNameForPath(v.in) != v.expected {
30+
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, CanonicalTarNameForPath(v.in))
3331
}
3432
}
3533
}
@@ -46,10 +44,8 @@ func TestCanonicalTarName(t *testing.T) {
4644
{"foo/bar", true, "foo/bar/"},
4745
}
4846
for _, v := range cases {
49-
if out, err := canonicalTarName(v.in, v.isDir); err != nil {
50-
t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
51-
} else if out != v.expected {
52-
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
47+
if canonicalTarName(v.in, v.isDir) != v.expected {
48+
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir))
5349
}
5450
}
5551
}

pkg/archive/archive_windows.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package archive // import "github.com/docker/docker/pkg/archive"
22

33
import (
44
"archive/tar"
5-
"fmt"
65
"os"
76
"path/filepath"
8-
"strings"
97

108
"github.com/docker/docker/pkg/idtools"
119
"github.com/docker/docker/pkg/longpath"
@@ -26,16 +24,8 @@ func getWalkRoot(srcPath string, include string) string {
2624
// CanonicalTarNameForPath returns platform-specific filepath
2725
// to canonical posix-style path for tar archival. p is relative
2826
// path.
29-
func CanonicalTarNameForPath(p string) (string, error) {
30-
// windows: convert windows style relative path with backslashes
31-
// into forward slashes. Since windows does not allow '/' or '\'
32-
// in file names, it is mostly safe to replace however we must
33-
// check just in case
34-
if strings.Contains(p, "/") {
35-
return "", fmt.Errorf("Windows path contains forward slash: %s", p)
36-
}
37-
return strings.Replace(p, string(os.PathSeparator), "/", -1), nil
38-
27+
func CanonicalTarNameForPath(p string) string {
28+
return filepath.ToSlash(p)
3929
}
4030

4131
// chmodTarEntry is used to adjust the file permissions used in tar header based

pkg/archive/archive_windows_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,14 @@ func TestCopyFileWithInvalidDest(t *testing.T) {
3636
func TestCanonicalTarNameForPath(t *testing.T) {
3737
cases := []struct {
3838
in, expected string
39-
shouldFail bool
4039
}{
41-
{"foo", "foo", false},
42-
{"foo/bar", "___", true}, // unix-styled windows path must fail
43-
{`foo\bar`, "foo/bar", false},
40+
{"foo", "foo"},
41+
{"foo/bar", "foo/bar"},
42+
{`foo\bar`, "foo/bar"},
4443
}
4544
for _, v := range cases {
46-
if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
47-
t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
48-
} else if v.shouldFail && err == nil {
49-
t.Fatalf("canonical path call should have failed with error. in=%s out=%s", v.in, out)
50-
} else if !v.shouldFail && out != v.expected {
51-
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
45+
if CanonicalTarNameForPath(v.in) != v.expected {
46+
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, CanonicalTarNameForPath(v.in))
5247
}
5348
}
5449
}
@@ -65,10 +60,8 @@ func TestCanonicalTarName(t *testing.T) {
6560
{`foo\bar`, true, "foo/bar/"},
6661
}
6762
for _, v := range cases {
68-
if out, err := canonicalTarName(v.in, v.isDir); err != nil {
69-
t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
70-
} else if out != v.expected {
71-
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
63+
if canonicalTarName(v.in, v.isDir) != v.expected {
64+
t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir))
7265
}
7366
}
7467
}

0 commit comments

Comments
 (0)