Skip to content

Commit dc8d653

Browse files
authored
Merge pull request #229 from xiekeyang/bugfix
bugfix: walk function exit incorrectly
2 parents 66556df + 04fea6c commit dc8d653

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

image/config.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func findConfig(w walker, d *descriptor) (*config, error) {
5353
var c config
5454
cpath := filepath.Join("blobs", d.normalizeDigest())
5555

56-
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
56+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
5757
if info.IsDir() || filepath.Clean(path) != cpath {
58-
return fmt.Errorf("%s: config not found", cpath)
58+
return nil
5959
}
6060
buf, err := ioutil.ReadAll(r)
6161
if err != nil {
@@ -69,12 +69,15 @@ func findConfig(w walker, d *descriptor) (*config, error) {
6969
if err := json.Unmarshal(buf, &c); err != nil {
7070
return err
7171
}
72-
73-
return nil
74-
}); err != nil {
72+
return errEOW
73+
}); err {
74+
case nil:
75+
return nil, fmt.Errorf("%s: config not found", cpath)
76+
case errEOW:
77+
return &c, nil
78+
default:
7579
return nil, err
7680
}
77-
return &c, nil
7881
}
7982

8083
func (c *config) runtimeSpec(rootfs string) (*specs.Spec, error) {

image/descriptor.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,49 @@ func findDescriptor(w walker, name string) (*descriptor, error) {
4141
var d descriptor
4242
dpath := filepath.Join("refs", name)
4343

44-
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
44+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
4545
if info.IsDir() || filepath.Clean(path) != dpath {
46-
return fmt.Errorf("%s: descriptor not found", dpath)
46+
return nil
4747
}
4848

4949
if err := json.NewDecoder(r).Decode(&d); err != nil {
5050
return err
5151
}
5252

53-
return nil
54-
}); err != nil {
53+
return errEOW
54+
}); err {
55+
case nil:
56+
return nil, fmt.Errorf("%s: descriptor not found", dpath)
57+
case errEOW:
58+
return &d, nil
59+
default:
5560
return nil, err
5661
}
57-
58-
return &d, nil
5962
}
6063

6164
func (d *descriptor) validate(w walker) error {
62-
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
65+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
6366
if info.IsDir() {
64-
return fmt.Errorf("%s: not found", d.normalizeDigest())
67+
return nil
6568
}
6669

6770
digest, err := filepath.Rel("blobs", filepath.Clean(path))
6871
if err != nil || d.normalizeDigest() != digest {
69-
return fmt.Errorf("%s: not found", d.normalizeDigest())
72+
return nil
7073
}
7174

7275
if err := d.validateContent(r); err != nil {
7376
return err
7477
}
75-
78+
return errEOW
79+
}); err {
80+
case nil:
81+
return fmt.Errorf("%s: not found", d.normalizeDigest())
82+
case errEOW:
7683
return nil
77-
}); err != nil {
84+
default:
7885
return errors.Wrapf(err, "%s: validation failed", d.normalizeDigest())
7986
}
80-
81-
return nil
8287
}
8388

8489
func (d *descriptor) validateContent(r io.Reader) error {

image/manifest.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func findManifest(w walker, d *descriptor) (*manifest, error) {
4040
var m manifest
4141
mpath := filepath.Join("blobs", d.normalizeDigest())
4242

43-
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
43+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
4444
if info.IsDir() || filepath.Clean(path) != mpath {
45-
return fmt.Errorf("%s: manifest not found", mpath)
45+
return nil
4646
}
4747

4848
buf, err := ioutil.ReadAll(r)
@@ -62,12 +62,15 @@ func findManifest(w walker, d *descriptor) (*manifest, error) {
6262
return fmt.Errorf("%s: no layers found", path)
6363
}
6464

65-
return nil
66-
}); err != nil {
65+
return errEOW
66+
}); err {
67+
case nil:
68+
return nil, fmt.Errorf("%s: manifest not found", mpath)
69+
case errEOW:
70+
return &m, nil
71+
default:
6772
return nil, err
6873
}
69-
70-
return &m, nil
7174
}
7275

7376
func (m *manifest) validate(w walker) error {
@@ -90,26 +93,30 @@ func (m *manifest) unpack(w walker, dest string) error {
9093
continue
9194
}
9295

93-
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
96+
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
9497
if info.IsDir() {
95-
return fmt.Errorf("%s: blob digest not found", d.normalizeDigest())
98+
return nil
9699
}
97100

98101
dd, err := filepath.Rel("blobs", filepath.Clean(path))
99102
if err != nil || d.normalizeDigest() != dd {
100-
return fmt.Errorf("%s: blob digest not found", d.normalizeDigest())
103+
return nil
101104
}
102105

103106
if err := unpackLayer(dest, r); err != nil {
104107
return errors.Wrap(err, "error extracting layer")
105108
}
106109

110+
return errEOW
111+
}); err {
112+
case nil:
113+
return fmt.Errorf("%s: layer not found", dest)
114+
case errEOW:
107115
return nil
108-
}); err != nil {
116+
default:
109117
return err
110118
}
111119
}
112-
113120
return nil
114121
}
115122

image/walker.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ package image
1616

1717
import (
1818
"archive/tar"
19+
"fmt"
1920
"io"
2021
"os"
2122
"path/filepath"
2223

2324
"github.com/pkg/errors"
2425
)
2526

27+
var (
28+
errEOW = fmt.Errorf("end of walk") // error to signal stop walking
29+
)
30+
2631
// walkFunc is a function type that gets called for each file or directory visited by the Walker.
2732
type walkFunc func(path string, _ os.FileInfo, _ io.Reader) error
2833

0 commit comments

Comments
 (0)