From 57f1de795524880dcff0abf6f2aaf428094e6933 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Tue, 3 Dec 2024 13:06:49 +0600 Subject: [PATCH] manifest: address comments from Alberto --- internal/manifest/manifest.go | 16 +++++----------- internal/manifest/manifest_test.go | 24 ++++++++++++++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/internal/manifest/manifest.go b/internal/manifest/manifest.go index 928b846a..4f3b35fd 100644 --- a/internal/manifest/manifest.go +++ b/internal/manifest/manifest.go @@ -346,23 +346,17 @@ func fastValidate(options *WriteOptions) (err error) { } } if entry.HardLinkID != 0 { + // TODO remove the following line after upgrading to Go 1.22 or higher. e := entry hardLinkGroups[e.HardLinkID] = append(hardLinkGroups[e.HardLinkID], &e) } } // Entries within a hard link group must have same content. - var linkIDs []uint64 - for id := range hardLinkGroups { - linkIDs = append(linkIDs, id) - } - sort.Slice(linkIDs, func(i, j int) bool { - return linkIDs[i] < linkIDs[j] - }) - for i, id := range linkIDs { - if uint64(i+1) != id { - return fmt.Errorf("skipped hard link ID %d, but %d exists", i+1, id) + for id := 1; id <= len(hardLinkGroups); id++ { + entries, ok := hardLinkGroups[uint64(id)] + if !ok { + return fmt.Errorf("cannot find hard link id %d", id) } - entries := hardLinkGroups[id] if len(entries) == 1 { return fmt.Errorf("hard link group %d has only one path: %s", id, entries[0].Path) } diff --git a/internal/manifest/manifest_test.go b/internal/manifest/manifest_test.go index 68f851ff..fea54728 100644 --- a/internal/manifest/manifest_test.go +++ b/internal/manifest/manifest_test.go @@ -35,15 +35,17 @@ var readManifestTests = []struct { {"jsonwall":"1.0","schema":"1.0","count":13} {"kind":"content","slice":"pkg1_manifest","path":"/manifest/manifest.wall"} {"kind":"content","slice":"pkg1_myslice","path":"/dir/file"} + {"kind":"content","slice":"pkg1_myslice","path":"/dir/file2"} {"kind":"content","slice":"pkg1_myslice","path":"/dir/foo/bar/"} - {"kind":"content","slice":"pkg1_myslice","path":"/dir/hardlink/file"} + {"kind":"content","slice":"pkg1_myslice","path":"/dir/hardlink/file2"} {"kind":"content","slice":"pkg1_myslice","path":"/dir/link/file"} {"kind":"content","slice":"pkg2_myotherslice","path":"/dir/foo/bar/"} {"kind":"package","name":"pkg1","version":"v1","sha256":"hash1","arch":"arch1"} {"kind":"package","name":"pkg2","version":"v2","sha256":"hash2","arch":"arch2"} - {"kind":"path","path":"/dir/file","mode":"0644","slices":["pkg1_myslice"],"sha256":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","final_sha256":"8067926c032c090867013d14fb0eb21ae858344f62ad07086fd32375845c91a6","size":21,"hard_link_id":1} + {"kind":"path","path":"/dir/file","mode":"0644","slices":["pkg1_myslice"],"sha256":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","final_sha256":"8067926c032c090867013d14fb0eb21ae858344f62ad07086fd32375845c91a6","size":21} + {"kind":"path","path":"/dir/file2","mode":"0644","slices":["pkg1_myslice"],"sha256":"b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c","size":3,"hard_link_id":1} {"kind":"path","path":"/dir/foo/bar/","mode":"01777","slices":["pkg2_myotherslice","pkg1_myslice"]} - {"kind":"path","path":"/dir/hardlink/file","mode":"0644","slices":["pkg1_myslice"],"sha256":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","final_sha256":"8067926c032c090867013d14fb0eb21ae858344f62ad07086fd32375845c91a6","size":21,"hard_link_id":1} + {"kind":"path","path":"/dir/hardlink/file2","mode":"0644","slices":["pkg1_myslice"],"sha256":"b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c","size":3,"hard_link_id":1} {"kind":"path","path":"/dir/link/file","mode":"0644","slices":["pkg1_myslice"],"link":"/dir/file"} {"kind":"path","path":"/manifest/manifest.wall","mode":"0644","slices":["pkg1_manifest"]} {"kind":"slice","name":"pkg1_manifest"} @@ -52,9 +54,10 @@ var readManifestTests = []struct { `, mfest: &manifestContents{ Paths: []*manifest.Path{ - {Kind: "path", Path: "/dir/file", Mode: "0644", Slices: []string{"pkg1_myslice"}, SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", FinalSHA256: "8067926c032c090867013d14fb0eb21ae858344f62ad07086fd32375845c91a6", Size: 0x15, Link: "", HardLinkID: 1}, + {Kind: "path", Path: "/dir/file", Mode: "0644", Slices: []string{"pkg1_myslice"}, SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", FinalSHA256: "8067926c032c090867013d14fb0eb21ae858344f62ad07086fd32375845c91a6", Size: 0x15, Link: ""}, + {Kind: "path", Path: "/dir/file2", Mode: "0644", Slices: []string{"pkg1_myslice"}, SHA256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", Size: 0x03, Link: "", HardLinkID: 0x01}, {Kind: "path", Path: "/dir/foo/bar/", Mode: "01777", Slices: []string{"pkg2_myotherslice", "pkg1_myslice"}, SHA256: "", FinalSHA256: "", Size: 0x0, Link: ""}, - {Kind: "path", Path: "/dir/hardlink/file", Mode: "0644", Slices: []string{"pkg1_myslice"}, SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", FinalSHA256: "8067926c032c090867013d14fb0eb21ae858344f62ad07086fd32375845c91a6", Size: 0x15, Link: "", HardLinkID: 1}, + {Kind: "path", Path: "/dir/hardlink/file2", Mode: "0644", Slices: []string{"pkg1_myslice"}, SHA256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", Size: 0x03, Link: "", HardLinkID: 0x01}, {Kind: "path", Path: "/dir/link/file", Mode: "0644", Slices: []string{"pkg1_myslice"}, SHA256: "", FinalSHA256: "", Size: 0x0, Link: "/dir/file"}, {Kind: "path", Path: "/manifest/manifest.wall", Mode: "0644", Slices: []string{"pkg1_manifest"}, SHA256: "", FinalSHA256: "", Size: 0x0, Link: ""}, }, @@ -70,8 +73,9 @@ var readManifestTests = []struct { Contents: []*manifest.Content{ {Kind: "content", Slice: "pkg1_manifest", Path: "/manifest/manifest.wall"}, {Kind: "content", Slice: "pkg1_myslice", Path: "/dir/file"}, + {Kind: "content", Slice: "pkg1_myslice", Path: "/dir/file2"}, {Kind: "content", Slice: "pkg1_myslice", Path: "/dir/foo/bar/"}, - {Kind: "content", Slice: "pkg1_myslice", Path: "/dir/hardlink/file"}, + {Kind: "content", Slice: "pkg1_myslice", Path: "/dir/hardlink/file2"}, {Kind: "content", Slice: "pkg1_myslice", Path: "/dir/link/file"}, {Kind: "content", Slice: "pkg2_myotherslice", Path: "/dir/foo/bar/"}, }, @@ -568,7 +572,7 @@ var generateManifestTests = []struct { }, error: `internal error: invalid manifest: path "/dir" has invalid options: size set for directory`, }, { - summary: "Invalid path: skipped hard link ID", + summary: "Skipped hard link id", report: &manifest.Report{ Root: "/", Entries: map[string]manifest.ReportEntry{ @@ -579,9 +583,9 @@ var generateManifestTests = []struct { }, }, }, - error: `internal error: invalid manifest: skipped hard link ID 1, but 2 exists`, + error: `internal error: invalid manifest: cannot find hard link id 1`, }, { - summary: "Invalid path: hard link group has only one path", + summary: "Hard link group has only one path", report: &manifest.Report{ Root: "/", Entries: map[string]manifest.ReportEntry{ @@ -594,7 +598,7 @@ var generateManifestTests = []struct { }, error: `internal error: invalid manifest: hard link group 1 has only one path: /file`, }, { - summary: "Invalid path: hard linked paths differ", + summary: "Hard linked paths differ", report: &manifest.Report{ Root: "/", Entries: map[string]manifest.ReportEntry{