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

tarfs: remove ReadFile #1325

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
56 changes: 5 additions & 51 deletions pkg/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,51 +452,6 @@ func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
return ret, nil
}

// ReadFile implements fs.ReadFileFS.
func (f *FS) ReadFile(name string) ([]byte, error) {
// ReadFileFS is implemented because it can avoid allocating an intermediate
// "file" struct and can immediately allocate a byte slice of the correct
// size.
const op = `readfile`
i, err := f.getInode(op, name)
if err != nil {
return nil, err
}

dataSize := i.h.Size
typ := i.h.FileInfo().Mode().Type()
var r *tar.Reader
switch {
case typ.IsRegular() && i.h.Typeflag != tar.TypeLink:
r = tar.NewReader(io.NewSectionReader(f.r, i.off, i.sz))
case typ.IsRegular() && i.h.Typeflag == tar.TypeLink || typ&fs.ModeSymlink != 0: // is hardlink or symlink
return f.ReadFile(i.h.Linkname)
default:
// Pretend all other kinds of files don't exist.
return nil, &fs.PathError{
Op: op,
Path: name,
Err: fs.ErrExist,
}
}
if _, err := r.Next(); err != nil {
return nil, &fs.PathError{
Op: op,
Path: name,
Err: err,
}
}
ret := make([]byte, dataSize)
if _, err := io.ReadFull(r, ret); err != nil {
return nil, &fs.PathError{
Op: op,
Path: name,
Err: err,
}
}
return ret, nil
}

// Glob implements fs.GlobFS.
//
// See path.Match for the patten syntax.
Expand Down Expand Up @@ -548,10 +503,9 @@ func (f *FS) Sub(dir string) (fs.FS, error) {

// A bunch of static assertions for the fs interfaces.
var (
_ fs.FS = (*FS)(nil)
_ fs.GlobFS = (*FS)(nil)
_ fs.ReadDirFS = (*FS)(nil)
_ fs.ReadFileFS = (*FS)(nil)
_ fs.StatFS = (*FS)(nil)
_ fs.SubFS = (*FS)(nil)
_ fs.FS = (*FS)(nil)
_ fs.GlobFS = (*FS)(nil)
_ fs.ReadDirFS = (*FS)(nil)
_ fs.StatFS = (*FS)(nil)
_ fs.SubFS = (*FS)(nil)
)
77 changes: 0 additions & 77 deletions pkg/tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,83 +460,6 @@ func TestSymlinks(t *testing.T) {
}))
}

func TestReadFile(t *testing.T) {
type tarfsFile struct {
Header tar.Header
Data []byte
}

tmp := t.TempDir()
setupAndRun := func(openErr bool, tarfsFiles []tarfsFile, chk func(*testing.T, *FS)) {
t.Helper()
// This is a perfect candidate for using test.GenerateFixture, but
// creates an import cycle.
f, err := os.Create(filepath.Join(tmp, filepath.Base(t.Name())))
if err != nil {
t.Fatal(err)
}
defer f.Close()

tw := tar.NewWriter(f)
for _, tarfsFile := range tarfsFiles {
h := tarfsFile.Header
h.Size = int64(len(tarfsFile.Data))
h.Format = tar.FormatGNU
if err := tw.WriteHeader(&h); err != nil {
t.Error(err)
}
_, err := tw.Write(tarfsFile.Data)
if err != nil {
t.Error(err)
}
}
if err := tw.Close(); err != nil {
t.Error(err)
}

sys, err := New(f)
t.Log(err)
if (err != nil) != openErr {
t.Fail()
}

if chk != nil {
chk(t, sys)
}
}

t.Run("Hardlink", func(t *testing.T) {
originalData := []byte(`Hello, World!`)
abData := make([]byte, len(originalData))
copy(abData, originalData)

setupAndRun(false, []tarfsFile{
{
Header: tar.Header{Name: `a/`},
},
{
Header: tar.Header{Name: `a/b`},
Data: abData,
},
{
Header: tar.Header{
Name: `a/c`,
Typeflag: tar.TypeLink,
Linkname: `a/b`,
},
},
}, func(t *testing.T, sys *FS) {
acFile, err := sys.ReadFile("a/c")
if err != nil {
t.Errorf("error while opening file: %v", err)
}
if !bytes.Equal(originalData, acFile) {
t.Errorf("unexpected \"%s\", got \"%s\"", originalData, acFile)
}
})
})
}

func TestKnownLayers(t *testing.T) {
ents, err := os.ReadDir(`testdata/known`)
if err != nil {
Expand Down
Loading