Skip to content

Commit 94e3ee6

Browse files
committed
tarfs: remove ReadFile
Signed-off-by: Brad Lugo <blugo@redhat.com>
1 parent e31accc commit 94e3ee6

2 files changed

Lines changed: 5 additions & 128 deletions

File tree

pkg/tarfs/tarfs.go

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -452,51 +452,6 @@ func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
452452
return ret, nil
453453
}
454454

455-
// ReadFile implements fs.ReadFileFS.
456-
func (f *FS) ReadFile(name string) ([]byte, error) {
457-
// ReadFileFS is implemented because it can avoid allocating an intermediate
458-
// "file" struct and can immediately allocate a byte slice of the correct
459-
// size.
460-
const op = `readfile`
461-
i, err := f.getInode(op, name)
462-
if err != nil {
463-
return nil, err
464-
}
465-
466-
dataSize := i.h.Size
467-
typ := i.h.FileInfo().Mode().Type()
468-
var r *tar.Reader
469-
switch {
470-
case typ.IsRegular() && i.h.Typeflag != tar.TypeLink:
471-
r = tar.NewReader(io.NewSectionReader(f.r, i.off, i.sz))
472-
case typ.IsRegular() && i.h.Typeflag == tar.TypeLink || typ&fs.ModeSymlink != 0: // is hardlink or symlink
473-
return f.ReadFile(i.h.Linkname)
474-
default:
475-
// Pretend all other kinds of files don't exist.
476-
return nil, &fs.PathError{
477-
Op: op,
478-
Path: name,
479-
Err: fs.ErrExist,
480-
}
481-
}
482-
if _, err := r.Next(); err != nil {
483-
return nil, &fs.PathError{
484-
Op: op,
485-
Path: name,
486-
Err: err,
487-
}
488-
}
489-
ret := make([]byte, dataSize)
490-
if _, err := io.ReadFull(r, ret); err != nil {
491-
return nil, &fs.PathError{
492-
Op: op,
493-
Path: name,
494-
Err: err,
495-
}
496-
}
497-
return ret, nil
498-
}
499-
500455
// Glob implements fs.GlobFS.
501456
//
502457
// See path.Match for the patten syntax.
@@ -548,10 +503,9 @@ func (f *FS) Sub(dir string) (fs.FS, error) {
548503

549504
// A bunch of static assertions for the fs interfaces.
550505
var (
551-
_ fs.FS = (*FS)(nil)
552-
_ fs.GlobFS = (*FS)(nil)
553-
_ fs.ReadDirFS = (*FS)(nil)
554-
_ fs.ReadFileFS = (*FS)(nil)
555-
_ fs.StatFS = (*FS)(nil)
556-
_ fs.SubFS = (*FS)(nil)
506+
_ fs.FS = (*FS)(nil)
507+
_ fs.GlobFS = (*FS)(nil)
508+
_ fs.ReadDirFS = (*FS)(nil)
509+
_ fs.StatFS = (*FS)(nil)
510+
_ fs.SubFS = (*FS)(nil)
557511
)

pkg/tarfs/tarfs_test.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -460,83 +460,6 @@ func TestSymlinks(t *testing.T) {
460460
}))
461461
}
462462

463-
func TestReadFile(t *testing.T) {
464-
type tarfsFile struct {
465-
Header tar.Header
466-
Data []byte
467-
}
468-
469-
tmp := t.TempDir()
470-
setupAndRun := func(openErr bool, tarfsFiles []tarfsFile, chk func(*testing.T, *FS)) {
471-
t.Helper()
472-
// This is a perfect candidate for using test.GenerateFixture, but
473-
// creates an import cycle.
474-
f, err := os.Create(filepath.Join(tmp, filepath.Base(t.Name())))
475-
if err != nil {
476-
t.Fatal(err)
477-
}
478-
defer f.Close()
479-
480-
tw := tar.NewWriter(f)
481-
for _, tarfsFile := range tarfsFiles {
482-
h := tarfsFile.Header
483-
h.Size = int64(len(tarfsFile.Data))
484-
h.Format = tar.FormatGNU
485-
if err := tw.WriteHeader(&h); err != nil {
486-
t.Error(err)
487-
}
488-
_, err := tw.Write(tarfsFile.Data)
489-
if err != nil {
490-
t.Error(err)
491-
}
492-
}
493-
if err := tw.Close(); err != nil {
494-
t.Error(err)
495-
}
496-
497-
sys, err := New(f)
498-
t.Log(err)
499-
if (err != nil) != openErr {
500-
t.Fail()
501-
}
502-
503-
if chk != nil {
504-
chk(t, sys)
505-
}
506-
}
507-
508-
t.Run("Hardlink", func(t *testing.T) {
509-
originalData := []byte(`Hello, World!`)
510-
abData := make([]byte, len(originalData))
511-
copy(abData, originalData)
512-
513-
setupAndRun(false, []tarfsFile{
514-
{
515-
Header: tar.Header{Name: `a/`},
516-
},
517-
{
518-
Header: tar.Header{Name: `a/b`},
519-
Data: abData,
520-
},
521-
{
522-
Header: tar.Header{
523-
Name: `a/c`,
524-
Typeflag: tar.TypeLink,
525-
Linkname: `a/b`,
526-
},
527-
},
528-
}, func(t *testing.T, sys *FS) {
529-
acFile, err := sys.ReadFile("a/c")
530-
if err != nil {
531-
t.Errorf("error while opening file: %v", err)
532-
}
533-
if !bytes.Equal(originalData, acFile) {
534-
t.Errorf("unexpected \"%s\", got \"%s\"", originalData, acFile)
535-
}
536-
})
537-
})
538-
}
539-
540463
func TestKnownLayers(t *testing.T) {
541464
ents, err := os.ReadDir(`testdata/known`)
542465
if err != nil {

0 commit comments

Comments
 (0)