Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always add parent directories of files to snapshots. #1166

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,28 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
return err
}
}

addedPaths := make(map[string]bool)
for _, path := range files {
if _, fileExists := addedPaths[path]; fileExists {
continue
}
for _, parentPath := range util.ParentDirectories(path) {
if parentPath == "/" {
continue
}
if _, dirExists := addedPaths[parentPath]; dirExists {
continue
}
if err := t.AddFileToTar(parentPath); err != nil {
return err
}
addedPaths[parentPath] = true
}
if err := t.AddFileToTar(path); err != nil {
return err
}
addedPaths[path] = true
}
return nil
}
Expand Down
22 changes: 21 additions & 1 deletion pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func TestSnapshotFSFileChange(t *testing.T) {
fooPath: "newbaz1",
batPath: "baz",
}
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(batPath) {
if path == "/" {
continue
}
snapshotFiles[path+"/"] = ""
}

actualFiles := []string{}
for {
Expand All @@ -77,6 +83,9 @@ func TestSnapshotFSFileChange(t *testing.T) {
if _, isFile := snapshotFiles[hdr.Name]; !isFile {
t.Fatalf("File %s unexpectedly in tar", hdr.Name)
}
if hdr.Typeflag == tar.TypeDir {
continue
}
contents, _ := ioutil.ReadAll(tr)
if string(contents) != snapshotFiles[hdr.Name] {
t.Fatalf("Contents of %s incorrect, expected: %s, actual: %s", hdr.Name, snapshotFiles[hdr.Name], string(contents))
Expand Down Expand Up @@ -153,6 +162,12 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
snapshotFiles := map[string]string{
batPathWithoutLeadingSlash: "baz2",
}
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(batPathWithoutLeadingSlash) {
if path == "/" {
continue
}
snapshotFiles[path+"/"] = ""
}

foundFiles := []string{}
for {
Expand All @@ -164,6 +179,9 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
if _, isFile := snapshotFiles[hdr.Name]; !isFile {
t.Fatalf("File %s unexpectedly in tar", hdr.Name)
}
if hdr.Typeflag == tar.TypeDir {
continue
}
contents, _ := ioutil.ReadAll(tr)
if string(contents) != snapshotFiles[hdr.Name] {
t.Fatalf("Contents of %s incorrect, expected: %s, actual: %s", hdr.Name, snapshotFiles[hdr.Name], string(contents))
Expand Down Expand Up @@ -203,7 +221,9 @@ func TestSnapshotFiles(t *testing.T) {
expectedFiles := []string{
filepath.Join(testDirWithoutLeadingSlash, "foo"),
}
expectedFiles = append(expectedFiles, util.ParentDirectoriesWithoutLeadingSlash(filepath.Join(testDir, "foo"))...)
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(filepath.Join(testDir, "foo")) {
expectedFiles = append(expectedFiles, strings.TrimRight(path, "/")+"/")
}

f, err := os.Open(tarPath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,10 @@ func ParentDirectories(path string) []string {
}
dir, _ = filepath.Split(dir)
dir = filepath.Clean(dir)
paths = append(paths, dir)
paths = append([]string{dir}, paths...)
}
if len(paths) == 0 {
paths = append(paths, config.RootDir)
paths = []string{config.RootDir}
}
return paths
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ func Test_ParentDirectories(t *testing.T) {
defer func() { config.RootDir = original }()
config.RootDir = tt.rootDir
actual := ParentDirectories(tt.path)
sort.Strings(actual)
sort.Strings(tt.expected)

testutil.CheckErrorAndDeepEqual(t, false, nil, tt.expected, actual)
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/tar_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (t *Tar) AddFileToTar(p string) error {
hdr.Name = strings.TrimPrefix(p, config.RootDir)
hdr.Name = strings.TrimLeft(hdr.Name, "/")
}
if hdr.Typeflag == tar.TypeDir && !strings.HasSuffix(hdr.Name, "/") {
hdr.Name = hdr.Name + "/"
}
// rootfs may not have been extracted when using cache, preventing uname/gname from resolving
// this makes this layer unnecessarily differ from a cached layer which does contain this information
hdr.Uname = ""
Expand Down