Skip to content
Merged
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
23 changes: 5 additions & 18 deletions image/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
38 changes: 10 additions & 28 deletions image/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,58 +41,40 @@ 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
}

return &d, nil
}

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())
}

Expand Down
34 changes: 10 additions & 24 deletions image/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}
}
Expand Down
5 changes: 0 additions & 5 deletions image/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ package image

import (
"archive/tar"
"fmt"
"io"
"os"
"path/filepath"

"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

Expand Down