Skip to content

Commit

Permalink
fix: improve errors for ReadAll and loadIndexfile (#526)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sparr authored Jun 27, 2023
1 parent 2e7b65f commit 6b5bd4b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion content/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewWithContext(ctx context.Context, root string) (*Store, error) {
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
}
if err := store.loadIndexFile(ctx); err != nil {
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
return nil, fmt.Errorf("invalid OCI Image Index: %w", err)
}

return store, nil
Expand Down
2 changes: 1 addition & 1 deletion content/oci/readonlyoci.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewFromFS(ctx context.Context, fsys fs.FS) (*ReadOnlyStore, error) {
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
}
if err := store.loadIndexFile(ctx); err != nil {
return nil, fmt.Errorf("invalid OCI Image Layout: %w", err)
return nil, fmt.Errorf("invalid OCI Image Index: %w", err)
}

return store, nil
Expand Down
7 changes: 5 additions & 2 deletions content/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (vr *VerifyReader) Read(p []byte) (n int, err error) {
return
}

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

vr := NewVerifyReader(r, desc)
if _, err := io.ReadFull(vr, buf); err != nil {
if n, err := io.ReadFull(vr, buf); err != nil {
if errors.Is(err, io.ErrUnexpectedEOF) {
return nil, fmt.Errorf("read failed: expected content size of %d, got %d, for digest %s: %w", desc.Size, n, desc.Digest.String(), err)
}
return nil, fmt.Errorf("read failed: %w", err)
}
if err := vr.Verify(); err != nil {
Expand Down

0 comments on commit 6b5bd4b

Please sign in to comment.