diff --git a/image/config.go b/image/config.go index 994d2ffd7..944a921aa 100644 --- a/image/config.go +++ b/image/config.go @@ -53,15 +53,10 @@ func findConfig(w walker, d *descriptor) (*config, error) { var c config cpath := filepath.Join("blobs", d.normalizeDigest()) - f := func(path string, info os.FileInfo, r io.Reader) error { - if info.IsDir() { - return nil + if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { + if info.IsDir() || filepath.Clean(path) != cpath { + return fmt.Errorf("%s: config not found", cpath) } - - if filepath.Clean(path) != cpath { - return nil - } - buf, err := ioutil.ReadAll(r) if err != nil { return errors.Wrapf(err, "%s: error reading config", path) @@ -75,18 +70,10 @@ func findConfig(w walker, d *descriptor) (*config, error) { return err } - return errEOW - } - - switch err := w.walk(f); err { - case nil: - return nil, fmt.Errorf("%s: config not found", cpath) - case errEOW: - // found, continue below - default: + return nil + }); err != nil { return nil, err } - return &c, nil } diff --git a/image/descriptor.go b/image/descriptor.go index 9a5d183ee..8ecba38c3 100644 --- a/image/descriptor.go +++ b/image/descriptor.go @@ -41,28 +41,17 @@ func findDescriptor(w walker, name string) (*descriptor, error) { var d descriptor dpath := filepath.Join("refs", name) - f := func(path string, info os.FileInfo, r io.Reader) error { - if info.IsDir() { - return nil - } - - if filepath.Clean(path) != dpath { - return nil + if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { + if info.IsDir() || filepath.Clean(path) != dpath { + return fmt.Errorf("%s: descriptor not found", dpath) } if err := json.NewDecoder(r).Decode(&d); err != nil { return err } - return errEOW - } - - switch err := w.walk(f); err { - case nil: - return nil, fmt.Errorf("%s: descriptor not found", dpath) - case errEOW: - // found, continue below - default: + return nil + }); err != nil { return nil, err } @@ -70,29 +59,22 @@ func findDescriptor(w walker, name string) (*descriptor, error) { } func (d *descriptor) validate(w walker) error { - f := func(path string, info os.FileInfo, r io.Reader) error { + if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { if info.IsDir() { - return nil + return fmt.Errorf("%s: not found", d.normalizeDigest()) } digest, err := filepath.Rel("blobs", filepath.Clean(path)) if err != nil || d.normalizeDigest() != digest { - return nil // ignore + return fmt.Errorf("%s: not found", d.normalizeDigest()) } if err := d.validateContent(r); err != nil { return err } - return errEOW - } - - switch err := w.walk(f); err { - case nil: - return fmt.Errorf("%s: not found", d.normalizeDigest()) - case errEOW: - // found, continue below - default: + return nil + }); err != nil { return errors.Wrapf(err, "%s: validation failed", d.normalizeDigest()) } diff --git a/image/manifest.go b/image/manifest.go index 13550ea51..7af8a0922 100644 --- a/image/manifest.go +++ b/image/manifest.go @@ -40,13 +40,9 @@ func findManifest(w walker, d *descriptor) (*manifest, error) { var m manifest mpath := filepath.Join("blobs", d.normalizeDigest()) - f := func(path string, info os.FileInfo, r io.Reader) error { - if info.IsDir() { - return nil - } - - if filepath.Clean(path) != mpath { - return nil + if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { + if info.IsDir() || filepath.Clean(path) != mpath { + return fmt.Errorf("%s: manifest not found", mpath) } buf, err := ioutil.ReadAll(r) @@ -66,15 +62,8 @@ func findManifest(w walker, d *descriptor) (*manifest, error) { return fmt.Errorf("%s: no layers found", path) } - return errEOW - } - - switch err := w.walk(f); err { - case nil: - return nil, fmt.Errorf("%s: manifest not found", mpath) - case errEOW: - // found, continue below - default: + return nil + }); err != nil { return nil, err } @@ -101,25 +90,22 @@ func (m *manifest) unpack(w walker, dest string) error { continue } - f := func(path string, info os.FileInfo, r io.Reader) error { + if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { if info.IsDir() { - return nil + return fmt.Errorf("%s: blob digest not found", d.normalizeDigest()) } dd, err := filepath.Rel("blobs", filepath.Clean(path)) if err != nil || d.normalizeDigest() != dd { - return nil // ignore + return fmt.Errorf("%s: blob digest not found", d.normalizeDigest()) } if err := unpackLayer(dest, r); err != nil { return errors.Wrap(err, "error extracting layer") } - return errEOW - } - - err := w.walk(f) - if err != nil && err != errEOW { + return nil + }); err != nil { return err } } diff --git a/image/walker.go b/image/walker.go index 958782ccd..cdfa661f8 100644 --- a/image/walker.go +++ b/image/walker.go @@ -16,7 +16,6 @@ package image import ( "archive/tar" - "fmt" "io" "os" "path/filepath" @@ -24,10 +23,6 @@ import ( "github.com/pkg/errors" ) -var ( - errEOW = fmt.Errorf("end of walk") // error to signal stop walking -) - // walkFunc is a function type that gets called for each file or directory visited by the Walker. type walkFunc func(path string, _ os.FileInfo, _ io.Reader) error