Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate manifest before writing #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 109 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,104 @@ func unixPerm(mode fs.FileMode) (perm uint32) {
}
return perm
}

func fastValidate(options *WriteOptions) (err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably name this something else. How about validateOptions, or preValidateManifest? Or, something else along that line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment would be nice here as well. :)

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
Loading