Skip to content

Commit

Permalink
Merge pull request opencontainers#201 from xiekeyang/refactor
Browse files Browse the repository at this point in the history
improve walk function on returning result
  • Loading branch information
vbatts authored Aug 31, 2016
2 parents 6e6d327 + 38fbdd4 commit 6a9e9d3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 75 deletions.
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

0 comments on commit 6a9e9d3

Please sign in to comment.