Browse code

LCOW: lazycontext: Use correct lstat, fix archive check

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2018/06/28 04:07:17
Showing 6 changed files
... ...
@@ -45,7 +45,7 @@ func (c *lazySource) Hash(path string) (string, error) {
45 45
 		return "", errors.WithStack(convertPathError(err, cleanPath))
46 46
 	}
47 47
 
48
-	fi, err := os.Lstat(fullPath)
48
+	fi, err := c.root.Lstat(fullPath)
49 49
 	if err != nil {
50 50
 		// Backwards compatibility: a missing file returns a path as hash.
51 51
 		// This is reached in the case of a broken symlink.
... ...
@@ -367,11 +367,7 @@ func FileInfoHeader(name string, fi os.FileInfo, link string) (*tar.Header, erro
367 367
 	hdr.AccessTime = time.Time{}
368 368
 	hdr.ChangeTime = time.Time{}
369 369
 	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())
375 371
 	if err := setHeaderForSpecialDevice(hdr, name, fi.Sys()); err != nil {
376 372
 		return nil, err
377 373
 	}
... ...
@@ -447,17 +443,14 @@ func newTarAppender(idMapping *idtools.IDMappings, writer io.Writer, chownOpts *
447 447
 
448 448
 // canonicalTarName provides a platform-independent and consistent posix-style
449 449
 //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
-	}
450
+func canonicalTarName(name string, isDir bool) string {
451
+	name = CanonicalTarNameForPath(name)
455 452
 
456 453
 	// suffix with '/' for directories
457 454
 	if isDir && !strings.HasSuffix(name, "/") {
458 455
 		name += "/"
459 456
 	}
460
-	return name, nil
457
+	return name
461 458
 }
462 459
 
463 460
 // addTarFile adds to the tar archive a file from `path` as `name`
... ...
@@ -32,8 +32,8 @@ func getWalkRoot(srcPath string, include string) string {
32 32
 // CanonicalTarNameForPath returns platform-specific filepath
33 33
 // to canonical posix-style path for tar archival. p is relative
34 34
 // 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
37 37
 }
38 38
 
39 39
 // chmodTarEntry is used to adjust the file permissions used in tar header based
... ...
@@ -26,10 +26,8 @@ func TestCanonicalTarNameForPath(t *testing.T) {
26 26
 		{"foo/dir/", "foo/dir/"},
27 27
 	}
28 28
 	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))
33 31
 		}
34 32
 	}
35 33
 }
... ...
@@ -46,10 +44,8 @@ func TestCanonicalTarName(t *testing.T) {
46 46
 		{"foo/bar", true, "foo/bar/"},
47 47
 	}
48 48
 	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)
49
+		if canonicalTarName(v.in, v.isDir) != v.expected {
50
+			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir))
53 51
 		}
54 52
 	}
55 53
 }
... ...
@@ -2,10 +2,8 @@ package archive // import "github.com/docker/docker/pkg/archive"
2 2
 
3 3
 import (
4 4
 	"archive/tar"
5
-	"fmt"
6 5
 	"os"
7 6
 	"path/filepath"
8
-	"strings"
9 7
 
10 8
 	"github.com/docker/docker/pkg/idtools"
11 9
 	"github.com/docker/docker/pkg/longpath"
... ...
@@ -26,16 +24,8 @@ func getWalkRoot(srcPath string, include string) string {
26 26
 // CanonicalTarNameForPath returns platform-specific filepath
27 27
 // to canonical posix-style path for tar archival. p is relative
28 28
 // 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
-
29
+func CanonicalTarNameForPath(p string) string {
30
+	return filepath.ToSlash(p)
39 31
 }
40 32
 
41 33
 // chmodTarEntry is used to adjust the file permissions used in tar header based
... ...
@@ -36,19 +36,14 @@ func TestCopyFileWithInvalidDest(t *testing.T) {
36 36
 func TestCanonicalTarNameForPath(t *testing.T) {
37 37
 	cases := []struct {
38 38
 		in, expected string
39
-		shouldFail   bool
40 39
 	}{
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"},
44 43
 	}
45 44
 	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))
52 47
 		}
53 48
 	}
54 49
 }
... ...
@@ -65,10 +60,8 @@ func TestCanonicalTarName(t *testing.T) {
65 65
 		{`foo\bar`, true, "foo/bar/"},
66 66
 	}
67 67
 	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)
68
+		if canonicalTarName(v.in, v.isDir) != v.expected {
69
+			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir))
72 70
 		}
73 71
 	}
74 72
 }