mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Merge pull request #37356 from Microsoft/jjh/new37316
LCOW: lazycontext: Use correct lstat, fix archive check
This commit is contained in:
		
						commit
						1d7132e3d2
					
				
					 6 changed files with 20 additions and 48 deletions
				
			
		| 
						 | 
					@ -45,7 +45,7 @@ func (c *lazySource) Hash(path string) (string, error) {
 | 
				
			||||||
		return "", errors.WithStack(convertPathError(err, cleanPath))
 | 
							return "", errors.WithStack(convertPathError(err, cleanPath))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fi, err := os.Lstat(fullPath)
 | 
						fi, err := c.root.Lstat(fullPath)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		// Backwards compatibility: a missing file returns a path as hash.
 | 
							// Backwards compatibility: a missing file returns a path as hash.
 | 
				
			||||||
		// This is reached in the case of a broken symlink.
 | 
							// 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
 | 
				
			||||||
	hdr.AccessTime = time.Time{}
 | 
						hdr.AccessTime = time.Time{}
 | 
				
			||||||
	hdr.ChangeTime = time.Time{}
 | 
						hdr.ChangeTime = time.Time{}
 | 
				
			||||||
	hdr.Mode = fillGo18FileTypeBits(int64(chmodTarEntry(os.FileMode(hdr.Mode))), fi)
 | 
						hdr.Mode = fillGo18FileTypeBits(int64(chmodTarEntry(os.FileMode(hdr.Mode))), fi)
 | 
				
			||||||
	name, err = canonicalTarName(name, fi.IsDir())
 | 
						hdr.Name = canonicalTarName(name, fi.IsDir())
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, fmt.Errorf("tar: cannot canonicalize path: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	hdr.Name = name
 | 
					 | 
				
			||||||
	if err := setHeaderForSpecialDevice(hdr, name, fi.Sys()); err != nil {
 | 
						if err := setHeaderForSpecialDevice(hdr, name, fi.Sys()); err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -447,17 +443,14 @@ func newTarAppender(idMapping *idtools.IDMappings, writer io.Writer, chownOpts *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// canonicalTarName provides a platform-independent and consistent posix-style
 | 
					// canonicalTarName provides a platform-independent and consistent posix-style
 | 
				
			||||||
//path for files and directories to be archived regardless of the platform.
 | 
					//path for files and directories to be archived regardless of the platform.
 | 
				
			||||||
func canonicalTarName(name string, isDir bool) (string, error) {
 | 
					func canonicalTarName(name string, isDir bool) string {
 | 
				
			||||||
	name, err := CanonicalTarNameForPath(name)
 | 
						name = CanonicalTarNameForPath(name)
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return "", err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// suffix with '/' for directories
 | 
						// suffix with '/' for directories
 | 
				
			||||||
	if isDir && !strings.HasSuffix(name, "/") {
 | 
						if isDir && !strings.HasSuffix(name, "/") {
 | 
				
			||||||
		name += "/"
 | 
							name += "/"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return name, nil
 | 
						return name
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// addTarFile adds to the tar archive a file from `path` as `name`
 | 
					// addTarFile adds to the tar archive a file from `path` as `name`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -32,8 +32,8 @@ func getWalkRoot(srcPath string, include string) string {
 | 
				
			||||||
// CanonicalTarNameForPath returns platform-specific filepath
 | 
					// CanonicalTarNameForPath returns platform-specific filepath
 | 
				
			||||||
// to canonical posix-style path for tar archival. p is relative
 | 
					// to canonical posix-style path for tar archival. p is relative
 | 
				
			||||||
// path.
 | 
					// path.
 | 
				
			||||||
func CanonicalTarNameForPath(p string) (string, error) {
 | 
					func CanonicalTarNameForPath(p string) string {
 | 
				
			||||||
	return p, nil // already unix-style
 | 
						return p // already unix-style
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// chmodTarEntry is used to adjust the file permissions used in tar header based
 | 
					// chmodTarEntry is used to adjust the file permissions used in tar header based
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,10 +26,8 @@ func TestCanonicalTarNameForPath(t *testing.T) {
 | 
				
			||||||
		{"foo/dir/", "foo/dir/"},
 | 
							{"foo/dir/", "foo/dir/"},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, v := range cases {
 | 
						for _, v := range cases {
 | 
				
			||||||
		if out, err := CanonicalTarNameForPath(v.in); err != nil {
 | 
							if CanonicalTarNameForPath(v.in) != v.expected {
 | 
				
			||||||
			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
 | 
								t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, CanonicalTarNameForPath(v.in))
 | 
				
			||||||
		} else if out != v.expected {
 | 
					 | 
				
			||||||
			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -46,10 +44,8 @@ func TestCanonicalTarName(t *testing.T) {
 | 
				
			||||||
		{"foo/bar", true, "foo/bar/"},
 | 
							{"foo/bar", true, "foo/bar/"},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, v := range cases {
 | 
						for _, v := range cases {
 | 
				
			||||||
		if out, err := canonicalTarName(v.in, v.isDir); err != nil {
 | 
							if canonicalTarName(v.in, v.isDir) != v.expected {
 | 
				
			||||||
			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
 | 
								t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir))
 | 
				
			||||||
		} else if out != v.expected {
 | 
					 | 
				
			||||||
			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,10 +2,8 @@ package archive // import "github.com/docker/docker/pkg/archive"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"archive/tar"
 | 
						"archive/tar"
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/docker/docker/pkg/idtools"
 | 
						"github.com/docker/docker/pkg/idtools"
 | 
				
			||||||
	"github.com/docker/docker/pkg/longpath"
 | 
						"github.com/docker/docker/pkg/longpath"
 | 
				
			||||||
| 
						 | 
					@ -26,16 +24,8 @@ func getWalkRoot(srcPath string, include string) string {
 | 
				
			||||||
// CanonicalTarNameForPath returns platform-specific filepath
 | 
					// CanonicalTarNameForPath returns platform-specific filepath
 | 
				
			||||||
// to canonical posix-style path for tar archival. p is relative
 | 
					// to canonical posix-style path for tar archival. p is relative
 | 
				
			||||||
// path.
 | 
					// path.
 | 
				
			||||||
func CanonicalTarNameForPath(p string) (string, error) {
 | 
					func CanonicalTarNameForPath(p string) string {
 | 
				
			||||||
	// windows: convert windows style relative path with backslashes
 | 
						return filepath.ToSlash(p)
 | 
				
			||||||
	// into forward slashes. Since windows does not allow '/' or '\'
 | 
					 | 
				
			||||||
	// in file names, it is mostly safe to replace however we must
 | 
					 | 
				
			||||||
	// check just in case
 | 
					 | 
				
			||||||
	if strings.Contains(p, "/") {
 | 
					 | 
				
			||||||
		return "", fmt.Errorf("Windows path contains forward slash: %s", p)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return strings.Replace(p, string(os.PathSeparator), "/", -1), nil
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// chmodTarEntry is used to adjust the file permissions used in tar header based
 | 
					// chmodTarEntry is used to adjust the file permissions used in tar header based
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,19 +36,14 @@ func TestCopyFileWithInvalidDest(t *testing.T) {
 | 
				
			||||||
func TestCanonicalTarNameForPath(t *testing.T) {
 | 
					func TestCanonicalTarNameForPath(t *testing.T) {
 | 
				
			||||||
	cases := []struct {
 | 
						cases := []struct {
 | 
				
			||||||
		in, expected string
 | 
							in, expected string
 | 
				
			||||||
		shouldFail   bool
 | 
					 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{"foo", "foo", false},
 | 
							{"foo", "foo"},
 | 
				
			||||||
		{"foo/bar", "___", true}, // unix-styled windows path must fail
 | 
							{"foo/bar", "foo/bar"},
 | 
				
			||||||
		{`foo\bar`, "foo/bar", false},
 | 
							{`foo\bar`, "foo/bar"},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, v := range cases {
 | 
						for _, v := range cases {
 | 
				
			||||||
		if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
 | 
							if CanonicalTarNameForPath(v.in) != v.expected {
 | 
				
			||||||
			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
 | 
								t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, CanonicalTarNameForPath(v.in))
 | 
				
			||||||
		} else if v.shouldFail && err == nil {
 | 
					 | 
				
			||||||
			t.Fatalf("canonical path call should have failed with error. in=%s out=%s", v.in, out)
 | 
					 | 
				
			||||||
		} else if !v.shouldFail && out != v.expected {
 | 
					 | 
				
			||||||
			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -65,10 +60,8 @@ func TestCanonicalTarName(t *testing.T) {
 | 
				
			||||||
		{`foo\bar`, true, "foo/bar/"},
 | 
							{`foo\bar`, true, "foo/bar/"},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, v := range cases {
 | 
						for _, v := range cases {
 | 
				
			||||||
		if out, err := canonicalTarName(v.in, v.isDir); err != nil {
 | 
							if canonicalTarName(v.in, v.isDir) != v.expected {
 | 
				
			||||||
			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
 | 
								t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir))
 | 
				
			||||||
		} else if out != v.expected {
 | 
					 | 
				
			||||||
			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue