From 06a03bd48f5628c908c322168a9bf1c9a683dead Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 4 Jun 2024 22:10:01 +0200 Subject: [PATCH] dump: use the sanitized path for root check otherwise if the root is stored as "./", it ends up adding the root node twice causing mkcomposefs to fail. Closes: https://github.com/containers/storage/issues/1941 Signed-off-by: Giuseppe Scrivano --- pkg/chunked/dump/dump.go | 2 +- pkg/chunked/dump/dump_test.go | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/chunked/dump/dump.go b/pkg/chunked/dump/dump.go index 324f4ba0e..555a620e1 100644 --- a/pkg/chunked/dump/dump.go +++ b/pkg/chunked/dump/dump.go @@ -113,7 +113,7 @@ func dumpNode(out io.Writer, added map[string]struct{}, links map[string]int, ve path := sanitizeName(entry.Name) parent := filepath.Dir(path) - if _, found := added[parent]; !found && entry.Name != "/" { + if _, found := added[parent]; !found && path != "/" { parentEntry := &internal.FileMetadata{ Name: parent, Type: internal.TypeDir, diff --git a/pkg/chunked/dump/dump_test.go b/pkg/chunked/dump/dump_test.go index 11b15071e..cbc6707b7 100644 --- a/pkg/chunked/dump/dump_test.go +++ b/pkg/chunked/dump/dump_test.go @@ -62,6 +62,12 @@ func TestDumpNode(t *testing.T) { }, } + rootEntry := &internal.FileMetadata{ + Name: "./", + Type: internal.TypeDir, + ModTime: &modTime, + } + directoryEntry := &internal.FileMetadata{ Name: "mydir", Type: internal.TypeDir, @@ -94,11 +100,16 @@ func TestDumpNode(t *testing.T) { ModTime: &modTime, } - var bufRegularFile, bufDirectory, bufSymlink, bufHardlink, bufMissingParent bytes.Buffer + var bufRootEntry, bufRegularFile, bufDirectory, bufSymlink, bufHardlink, bufMissingParent bytes.Buffer added := map[string]struct{}{"/": {}} - err := dumpNode(&bufRegularFile, added, map[string]int{}, map[string]string{}, regularFileEntry) + err := dumpNode(&bufRootEntry, map[string]struct{}{}, map[string]int{}, map[string]string{}, rootEntry) + if err != nil { + t.Errorf("unexpected error for root entry: %v", err) + } + + err = dumpNode(&bufRegularFile, added, map[string]int{}, map[string]string{}, regularFileEntry) if err != nil { t.Errorf("unexpected error for regular file: %v", err) } @@ -123,18 +134,24 @@ func TestDumpNode(t *testing.T) { t.Errorf("unexpected error: %v", err) } + expectedRootEntry := "/ 0 40000 1 0 0 0 1672531200.0 - - -\n" expectedRegularFile := "/example.txt 100 100000 1 1000 1000 0 1672531200.0 ab/cdef1234567890 - - user.key1=value1\n" expectedDirectory := "/mydir 0 40000 1 1000 1000 0 1672531200.0 - - - user.key2=value2\n" expectedSymlink := "/mysymlink 0 120000 1 0 0 0 1672531200.0 targetfile - -\n" expectedHardlink := "/myhardlink 0 @100000 1 0 0 0 1672531200.0 /existingfile - -\n" expectedActualMissingParent := "/foo 0 40755 1 0 0 0 0.0 - - -\n/foo/bar 0 40755 1 0 0 0 0.0 - - -\n/foo/bar/baz 0 40755 1 0 0 0 0.0 - - -\n/foo/bar/baz/entry 0 100000 1 0 0 0 1672531200.0 - - -\n" + actualRootEntry := bufRootEntry.String() actualRegularFile := bufRegularFile.String() actualDirectory := bufDirectory.String() actualSymlink := bufSymlink.String() actualHardlink := bufHardlink.String() actualMissingParent := bufMissingParent.String() + if actualRootEntry != expectedRootEntry { + t.Errorf("for root entry, got %q, want %q", actualRootEntry, expectedRootEntry) + } + if actualRegularFile != expectedRegularFile { t.Errorf("for regular file, got %q, want %q", actualRegularFile, expectedRegularFile) }