Skip to content

Commit

Permalink
image: fix the interaction of findManifest and findConfig with walker…
Browse files Browse the repository at this point in the history
….find

Before rev. 9027d58 findManifest and findConfig called w.walk directly,
providing a walkFunc that returned errEOW in case a file has been
found and processed successfully.  A switch construct evaluating the
error-result of w.walk then tested for the errEOW case, meaning the file
has been found.

Starting with rev. 9027d58 the call to w.walk in findManifest and
findConfig has been replaced by w.find. Though, the function literal
provided by findManifest and findConfig to w.find -- now a findFunc,
not a walkFunc anymore -- still returns errEOW. As a consequence,
walker.find implementations for tar and zip would treat errEOW as an
error, making oci-image-tool 'validate' and 'create' stop with an error:

	<tar-image>: find failed: unable to walk: end of walk

This patch adjusts tarWalker's and zipWalker's implementations of
walker.find, using errEOW to stop w.walk early in case of success; there
is no need for the 'done' variable anymore. Additionally, places where
w.find is called, functions findManifest, unpackManifest, and findConfig,
have been adapted so that their findFunc argument to w.find does not
return errEOW anymore; instead nil is returned on success. Consequently,
the switch constructs now test err for 'nil' instead of 'errEOW', and
'os.ErrNotExist' instead of nil.

Fixes opencontainers#205

Signed-off-by: Michael Teichgräber <mteichgraeber@gmx.de>
  • Loading branch information
knieriem committed Jun 10, 2018
1 parent c95f76c commit 06a4e96
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
7 changes: 4 additions & 3 deletions image/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -48,11 +49,11 @@ func findConfig(w walker, d *v1.Descriptor) (*v1.Image, error) {
return err
}

return errEOW
return nil
}); err {
case nil:
case os.ErrNotExist:
return nil, fmt.Errorf("%s: config not found", cpath)
case errEOW:
case nil:
return &c, nil
default:
return nil, err
Expand Down
11 changes: 5 additions & 6 deletions image/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func findManifest(w walker, d *v1.Descriptor) (*v1.Manifest, error) {
return err
}

return errEOW
return nil
}); err {
case nil:
case os.ErrNotExist:
return nil, fmt.Errorf("%s: manifest not found", mpath)
case errEOW:
case nil:
return &m, nil
default:
return nil, err
Expand Down Expand Up @@ -110,11 +110,10 @@ func unpackManifest(m *v1.Manifest, w walker, dest string) (retErr error) {
return errors.Wrap(err, "unpack: error extracting layer")
}

return errEOW
return nil
}); err {
case nil:
case os.ErrNotExist:
return fmt.Errorf("%s: layer not found", dest)
case errEOW:
default:
return err
}
Expand Down
32 changes: 12 additions & 20 deletions image/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,24 @@ func (w *tarWalker) get(desc v1.Descriptor, dst io.Writer) (int64, error) {
}

func (w *tarWalker) find(path string, ff findFunc) error {
done := false

f := func(relpath string, info os.FileInfo, rdr io.Reader) error {
var err error
if done {
return nil
}

if filepath.Clean(relpath) == path && !info.IsDir() {
if err = ff(relpath, rdr); err != nil {
return err
}
done = true
return errEOW
}
return nil
}

if err := w.walk(f); err != nil {
return errors.Wrapf(err, "find failed: unable to walk")
}
if !done {
switch err := w.walk(f); err {
case nil:
return os.ErrNotExist
case errEOW:
default:
return errors.Wrapf(err, "find failed: unable to walk")
}

return nil
Expand Down Expand Up @@ -305,28 +301,24 @@ func (w *zipWalker) get(desc v1.Descriptor, dst io.Writer) (int64, error) {
}

func (w *zipWalker) find(path string, ff findFunc) error {
done := false

f := func(relpath string, info os.FileInfo, rdr io.Reader) error {
var err error
if done {
return nil
}

if filepath.Clean(relpath) == path && !info.IsDir() {
if err = ff(relpath, rdr); err != nil {
return err
}
done = true
return errEOW
}
return nil
}

if err := w.walk(f); err != nil {
return errors.Wrapf(err, "find failed: unable to walk")
}
if !done {
switch err := w.walk(f); err {
case nil:
return os.ErrNotExist
case errEOW:
default:
return errors.Wrapf(err, "find failed: unable to walk")
}

return nil
Expand Down

0 comments on commit 06a4e96

Please sign in to comment.