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: follow hardlinks in ReadFile #1305

Merged
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
19 changes: 16 additions & 3 deletions pkg/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,31 @@
if err != nil {
return nil, err
}
if i.h.FileInfo().Mode().Type()&fs.ModeSymlink != 0 {

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:

Check warning on line 469 in pkg/tarfs/tarfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/tarfs/tarfs.go#L469

Added line #L469 was not covered by tests
// Pretend all other kinds of files don't exist.
return nil, &fs.PathError{
Op: op,
Path: name,
Err: fs.ErrExist,

Check warning on line 474 in pkg/tarfs/tarfs.go

View check run for this annotation

Codecov / codecov/patch

pkg/tarfs/tarfs.go#L471-L474

Added lines #L471 - L474 were not covered by tests
}
}
r := tar.NewReader(io.NewSectionReader(f.r, i.off, i.sz))
if _, err := r.Next(); err != nil {
return nil, &fs.PathError{
Op: op,
Path: name,
Err: err,
}
}
ret := make([]byte, i.h.Size)
ret := make([]byte, dataSize)
if _, err := io.ReadFull(r, ret); err != nil {
return nil, &fs.PathError{
Op: op,
Expand Down
77 changes: 77 additions & 0 deletions pkg/tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,83 @@ 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