Merge pull request #1929 from giuseppe/compose-fs-add-missing-dir-parents

composefs: add parent directory if missing
This commit is contained in:
openshift-merge-bot[bot] 2024-05-28 13:16:15 +00:00 committed by GitHub
commit 989e69d83a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 9 deletions

View File

@ -104,9 +104,23 @@ func sanitizeName(name string) string {
return path
}
func dumpNode(out io.Writer, links map[string]int, verityDigests map[string]string, entry *internal.FileMetadata) error {
func dumpNode(out io.Writer, added map[string]struct{}, links map[string]int, verityDigests map[string]string, entry *internal.FileMetadata) error {
path := sanitizeName(entry.Name)
parent := filepath.Dir(path)
if _, found := added[parent]; !found && entry.Name != "/" {
parentEntry := &internal.FileMetadata{
Name: parent,
Type: internal.TypeDir,
Mode: 0o755,
}
if err := dumpNode(out, added, links, verityDigests, parentEntry); err != nil {
return err
}
}
added[path] = struct{}{}
if _, err := fmt.Fprint(out, escaped(path, ESCAPE_STANDARD)); err != nil {
return err
}
@ -201,6 +215,7 @@ func GenerateDump(tocI interface{}, verityDigests map[string]string) (io.Reader,
}()
links := make(map[string]int)
added := make(map[string]struct{})
for _, e := range toc.Entries {
if e.Linkname == "" {
continue
@ -211,14 +226,14 @@ func GenerateDump(tocI interface{}, verityDigests map[string]string) (io.Reader,
links[e.Linkname] = links[e.Linkname] + 1
}
if len(toc.Entries) == 0 || (sanitizeName(toc.Entries[0].Name) != "/") {
if len(toc.Entries) == 0 {
root := &internal.FileMetadata{
Name: "/",
Type: internal.TypeDir,
Mode: 0o755,
}
if err := dumpNode(w, links, verityDigests, root); err != nil {
if err := dumpNode(w, added, links, verityDigests, root); err != nil {
pipeW.CloseWithError(err)
closed = true
return
@ -229,7 +244,7 @@ func GenerateDump(tocI interface{}, verityDigests map[string]string) (io.Reader,
if e.Type == internal.TypeChunk {
continue
}
if err := dumpNode(w, links, verityDigests, &e); err != nil {
if err := dumpNode(w, added, links, verityDigests, &e); err != nil {
pipeW.CloseWithError(err)
closed = true
return

View File

@ -87,37 +87,52 @@ func TestDumpNode(t *testing.T) {
Linkname: "existingfile",
}
var bufRegularFile, bufDirectory, bufSymlink, bufHardlink bytes.Buffer
missingParentEntry := &internal.FileMetadata{
Name: "foo/bar/baz/entry",
Type: internal.TypeReg,
ModTime: &modTime,
}
err := dumpNode(&bufRegularFile, map[string]int{}, map[string]string{}, regularFileEntry)
var bufRegularFile, bufDirectory, bufSymlink, bufHardlink, bufMissingParent bytes.Buffer
added := map[string]struct{}{"/": {}}
err := dumpNode(&bufRegularFile, added, map[string]int{}, map[string]string{}, regularFileEntry)
if err != nil {
t.Errorf("unexpected error for regular file: %v", err)
}
err = dumpNode(&bufDirectory, map[string]int{}, map[string]string{}, directoryEntry)
err = dumpNode(&bufDirectory, added, map[string]int{}, map[string]string{}, directoryEntry)
if err != nil {
t.Errorf("unexpected error for directory: %v", err)
}
err = dumpNode(&bufSymlink, map[string]int{}, map[string]string{}, symlinkEntry)
err = dumpNode(&bufSymlink, added, map[string]int{}, map[string]string{}, symlinkEntry)
if err != nil {
t.Errorf("unexpected error for symlink: %v", err)
}
err = dumpNode(&bufHardlink, map[string]int{}, map[string]string{}, hardlinkEntry)
err = dumpNode(&bufHardlink, added, map[string]int{}, map[string]string{}, hardlinkEntry)
if err != nil {
t.Errorf("unexpected error for hardlink: %v", err)
}
err = dumpNode(&bufMissingParent, added, map[string]int{}, map[string]string{}, missingParentEntry)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
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"
actualRegularFile := bufRegularFile.String()
actualDirectory := bufDirectory.String()
actualSymlink := bufSymlink.String()
actualHardlink := bufHardlink.String()
actualMissingParent := bufMissingParent.String()
if actualRegularFile != expectedRegularFile {
t.Errorf("for regular file, got %q, want %q", actualRegularFile, expectedRegularFile)
@ -134,4 +149,7 @@ func TestDumpNode(t *testing.T) {
if actualHardlink != expectedHardlink {
t.Errorf("for hardlink, got %q, want %q", actualHardlink, expectedHardlink)
}
if actualMissingParent != expectedActualMissingParent {
t.Errorf("for missing parent, got %q, want %q", actualMissingParent, expectedActualMissingParent)
}
}