Skip to content

Commit f3d7906

Browse files
sparrshizhMSFT
authored andcommitted
fix: improve errors for ReadAll and loadIndexfile (#526)
NewWithContext refers to both the `oci-layout` and `index.json` as "OCI Image Layout", this PR fixes the latter. ReadAll is a deeper change that will improve error reporting for a lot of cases, at the cost of error volume. Some or all of that change could happen higher up to satisfy this particular issue. I think the expected/actual size output belongs here. There seem to be more than a few code paths in this and other repos that end up calling ReadAll multiple times without reporting which call produced an error so I think it's appropriate to also output the digest here, but that's less compelling. The Verify comment clears up confusion I encountered while working on this issue. "verifies size" implies checking for both too big AND too small, but it only checks if the file is bigger (or reader is longer) than expected. Fixes #432 Signed-off-by: Clarence "Sparr" Risher <clrnc@amazon.com>
1 parent 9480525 commit f3d7906

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

content/oci/oci.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func NewWithContext(ctx context.Context, root string) (*Store, error) {
102102
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
103103
}
104104
if err := store.loadIndexFile(ctx); err != nil {
105-
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
105+
return nil, fmt.Errorf("invalid OCI Image Index: %w", err)
106106
}
107107

108108
return store, nil

content/oci/readonlyoci.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewFromFS(ctx context.Context, fsys fs.FS) (*ReadOnlyStore, error) {
5757
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
5858
}
5959
if err := store.loadIndexFile(ctx); err != nil {
60-
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
60+
return nil, fmt.Errorf("invalid OCI Image Index: %w", err)
6161
}
6262

6363
return store, nil

content/reader.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (vr *VerifyReader) Read(p []byte) (n int, err error) {
7070
return
7171
}
7272

73-
// Verify verifies the read content against the size and the digest.
73+
// Verify checks for remaining unread content and verifies the read content against the digest
7474
func (vr *VerifyReader) Verify() error {
7575
if vr.verified {
7676
return nil
@@ -120,7 +120,10 @@ func ReadAll(r io.Reader, desc ocispec.Descriptor) ([]byte, error) {
120120
buf := make([]byte, desc.Size)
121121

122122
vr := NewVerifyReader(r, desc)
123-
if _, err := io.ReadFull(vr, buf); err != nil {
123+
if n, err := io.ReadFull(vr, buf); err != nil {
124+
if errors.Is(err, io.ErrUnexpectedEOF) {
125+
return nil, fmt.Errorf("read failed: expected content size of %d, got %d, for digest %s: %w", desc.Size, n, desc.Digest.String(), err)
126+
}
124127
return nil, fmt.Errorf("read failed: %w", err)
125128
}
126129
if err := vr.Verify(); err != nil {

0 commit comments

Comments
 (0)