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 <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2024-06-04 22:10:01 +02:00
parent 7857557d61
commit 06a03bd48f
No known key found for this signature in database
GPG Key ID: 67E38F7A8BA21772
2 changed files with 20 additions and 3 deletions

View File

@ -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,

View File

@ -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)
}