Skip to content

Commit

Permalink
Merge branch 'main' into multiple-archive-support
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Oct 7, 2024
2 parents 64d1e74 + 5974449 commit 82587f6
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 172 deletions.
6 changes: 3 additions & 3 deletions cmd/chisel/cmd_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ var infoTests = []infoTest{{
var testKey = testutil.PGPKeys["key1"]

var defaultChiselYaml = `
format: chisel-v1
format: v1
archives:
ubuntu:
version: 22.04
components: [main, universe]
v1-public-keys: [test-key]
v1-public-keys:
public-keys: [test-key]
public-keys:
test-key:
id: ` + testKey.ID + `
armor: |` + "\n" + testutil.PrefixEachLine(testKey.PubKeyArmor, "\t\t\t\t\t\t")
Expand Down
116 changes: 113 additions & 3 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Validate(manifest *Manifest) (err error) {
return err
}
if !pkgExist[sk.Package] {
return fmt.Errorf("package %q not found in packages", sk.Package)
return fmt.Errorf("slice %s refers to missing package %q", slice.Name, sk.Package)
}
sliceExist[slice.Name] = true
return nil
Expand All @@ -127,7 +127,7 @@ func Validate(manifest *Manifest) (err error) {
pathToSlices := map[string][]string{}
err = manifest.IterateContents("", func(content *Content) error {
if !sliceExist[content.Slice] {
return fmt.Errorf("slice %s not found in slices", content.Slice)
return fmt.Errorf("content path %q refers to missing slice %s", content.Path, content.Slice)
}
if !slices.Contains(pathToSlices[content.Path], content.Slice) {
pathToSlices[content.Path] = append(pathToSlices[content.Path], content.Slice)
Expand Down Expand Up @@ -191,7 +191,12 @@ func Write(options *WriteOptions, writer io.Writer) error {
Schema: Schema,
})

err := manifestAddPackages(dbw, options.PackageInfo)
err := fastValidate(options)
if err != nil {
return err
}

err = manifestAddPackages(dbw, options.PackageInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -301,3 +306,108 @@ func unixPerm(mode fs.FileMode) (perm uint32) {
}
return perm
}

// fastValidate validates the data to be written into the manifest.
// This is validating internal structures which are supposed to be correct unless there is
// a bug. As such, only assertions that can be done quickly are performed here, instead
// of it being a comprehensive validation of all the structures.
func fastValidate(options *WriteOptions) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("internal error: invalid manifest: %s", err)
}
}()
pkgExist := map[string]bool{}
for _, pkg := range options.PackageInfo {
err := validatePackage(pkg)
if err != nil {
return err
}
pkgExist[pkg.Name] = true
}
sliceExist := map[string]bool{}
for _, slice := range options.Selection {
if _, ok := pkgExist[slice.Package]; !ok {
return fmt.Errorf("slice %s refers to missing package %q", slice.String(), slice.Package)
}
sliceExist[slice.String()] = true
}
for _, entry := range options.Report.Entries {
err := validateReportEntry(&entry)
if err != nil {
return err
}
for slice := range entry.Slices {
if _, ok := sliceExist[slice.String()]; !ok {
return fmt.Errorf("path %q refers to missing slice %s", entry.Path, slice.String())
}
}
}
return nil
}

func validateReportEntry(entry *ReportEntry) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("path %q has invalid options: %s", entry.Path, err)
}
}()

switch entry.Mode & fs.ModeType {
case 0:
// Regular file.
if entry.Link != "" {
return fmt.Errorf("link set for regular file")
}
case fs.ModeDir:
if entry.Link != "" {
return fmt.Errorf("link set for directory")
}
if entry.SHA256 != "" {
return fmt.Errorf("sha256 set for directory")
}
if entry.FinalSHA256 != "" {
return fmt.Errorf("final_sha256 set for directory")
}
if entry.Size != 0 {
return fmt.Errorf("size set for directory")
}
case fs.ModeSymlink:
if entry.Link == "" {
return fmt.Errorf("link not set for symlink")
}
if entry.SHA256 != "" {
return fmt.Errorf("sha256 set for symlink")
}
if entry.FinalSHA256 != "" {
return fmt.Errorf("final_sha256 set for symlink")
}
if entry.Size != 0 {
return fmt.Errorf("size set for symlink")
}
default:
return fmt.Errorf("unsupported file type: %s", entry.Path)
}

if len(entry.Slices) == 0 {
return fmt.Errorf("slices is empty")
}

return nil
}

func validatePackage(pkg *archive.PackageInfo) (err error) {
if pkg.Name == "" {
return fmt.Errorf("package name not set")
}
if pkg.Arch == "" {
return fmt.Errorf("package %q missing arch", pkg.Name)
}
if pkg.SHA256 == "" {
return fmt.Errorf("package %q missing sha256", pkg.Name)
}
if pkg.Version == "" {
return fmt.Errorf("package %q missing version", pkg.Name)
}
return nil
}
Loading

0 comments on commit 82587f6

Please sign in to comment.