Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update error returned when oci.NewFromTar is used with a non-tar file #793

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions content/oci/readonlyoci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/internal/docker"
"oras.land/oras-go/v2/internal/spec"
"oras.land/oras-go/v2/registry"
Expand Down Expand Up @@ -836,3 +837,18 @@ func Test_deleteAnnotationRefName(t *testing.T) {
})
}
}

func TestNewFromTar_NonTarFile(t *testing.T) {

testFiles := []string{
"testdata/emptyfile",
"testdata/nontar.tar",
}
for _, name := range testFiles {
_, err := NewFromTar(context.Background(), name)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", name, err, want)
}
}

}
Empty file.
1 change: 1 addition & 0 deletions content/oci/testdata/nontar.tar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-empty file
1 change: 1 addition & 0 deletions errdef/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ var (
ErrSizeExceedsLimit = errors.New("size exceeds limit")
ErrUnsupported = errors.New("unsupported")
ErrUnsupportedVersion = errors.New("unsupported version")
ErrInvalidTarFile = errors.New("invalid tar file")
)
9 changes: 9 additions & 0 deletions internal/fs/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func New(path string) (*TarFS, error) {
if err != nil {
return nil, fmt.Errorf("failed to resolve absolute path for %s: %w", path, err)
}

tarfs := &TarFS{
path: pathAbs,
entries: make(map[string]*entry),
Expand Down Expand Up @@ -135,8 +136,16 @@ func (tfs *TarFS) indexEntries() error {
header, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
if len(tfs.entries) == 0 {
// indicates the given file is empty
return fmt.Errorf("%s: file is empty: %w", tfs.path, errdef.ErrInvalidTarFile)
Comment on lines +139 to +141
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking out loud: From the tarfs point of view, the tar file can be empty. But since tarfs is an internal package and is only used by ReadOnlyStorage and ReadOnlyOCI, which requires oci-layout file in it, I think it makes sense to return an error for an empty tar file. 🤔

}
Comment on lines +139 to +142
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should return error on empty valid tar files since it breaks the encapsulation principal.

If we really want to check for empty tar file, it should be checked in the caller.

break
}
if errors.Is(err, io.ErrUnexpectedEOF) {
// indicates that its either not a tarfile or it is a corrupted one
return fmt.Errorf("%s: %w", tfs.path, errdef.ErrInvalidTarFile)
}
Comment on lines +145 to +148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The io.ErrUnexpectedEOF error itself indicates the tar file is corrupted. There is no need to wrap it to errdef.ErrInvalidTarFile.

return err
}
pos, err := tarFile.Seek(0, io.SeekCurrent)
Expand Down
19 changes: 19 additions & 0 deletions internal/fs/tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,22 @@ func TestTarFS_Stat_Unsupported(t *testing.T) {
}
}
}

func TestTarFS_New_NonTarFile(t *testing.T) {
testFiles := []string{
"testdata/emptyfile.tar",
"testdata/nontar.tar",
}
for _, name := range testFiles {
_, err := New(name)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", name, err, want)
}
}

tarwithoutextension := "testdata/tarwithoutextension"
_, err := New(tarwithoutextension)
if err != nil {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", tarwithoutextension, err, nil)
Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
}
}
Empty file.
1 change: 1 addition & 0 deletions internal/fs/tarfs/testdata/nontar.tar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-empty file
Binary file not shown.
Loading