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 2 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
20 changes: 20 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,22 @@ func Test_deleteAnnotationRefName(t *testing.T) {
})
}
}

func TestNewFromTar_NonTarFile(t *testing.T) {

emptyfile := "testdata/emptyfile"
Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
nonemptyfile := "testdata/nonempty.tar"

_, err := NewFromTar(context.Background(), emptyfile)

if want := errdef.ErrNotTarFile; !errors.Is(err, want) {
t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", emptyfile, err, want)
}

_, err = NewFromTar(context.Background(), nonemptyfile)

if want := errdef.ErrNotTarFile; !errors.Is(err, want) {
t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", nonemptyfile, err, want)
}

}
Empty file.
1 change: 1 addition & 0 deletions content/oci/testdata/nonempty.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")
ErrNotTarFile = errors.New("non-empty tar file expected")
Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
)
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 errdef.ErrNotTarFile
}
break
}
if errors.Is(err, io.ErrUnexpectedEOF) {
// indicates that its either not a tarfile or it is a corrupted one
return errdef.ErrNotTarFile
Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
}
return err
}
pos, err := tarFile.Seek(0, io.SeekCurrent)
Expand Down
25 changes: 25 additions & 0 deletions internal/fs/tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,28 @@ func TestTarFS_Stat_Unsupported(t *testing.T) {
}
}
}

func TestTarFS_New_NonTarFile(t *testing.T) {

emptyfile := "testdata/emptyfile.tar"
nonemptyfile := "testdata/nonempty.tar"
tarwithoutextension := "testdata/tarwithoutextension"

_, err := New(emptyfile)

Wwwsylvia marked this conversation as resolved.
Show resolved Hide resolved
if want := errdef.ErrNotTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", emptyfile, err, want)
}

_, err = New(nonemptyfile)

if want := errdef.ErrNotTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", nonemptyfile, err, want)
}

_, err = New(tarwithoutextension)

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