Skip to content

Commit

Permalink
fix(tarfs): follow hardlinks in ReadFile
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Lugo <[email protected]>
  • Loading branch information
BradLugo committed Apr 8, 2024
1 parent 762e6a4 commit 2b4d980
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
golang.org/x/mod v0.16.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,17 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
Expand Down
22 changes: 20 additions & 2 deletions pkg/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,28 @@ func (f *FS) ReadFile(name string) ([]byte, error) {
if err != nil {
return nil, err
}
if i.h.FileInfo().Mode().Type()&fs.ModeSymlink != 0 {

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:
tgt, err := f.getInode(op, i.h.Linkname)
if err != nil {
return nil, err
}
r = tar.NewReader(io.NewSectionReader(f.r, tgt.off, tgt.sz))
case typ&fs.ModeSymlink != 0: // typ.IsSymlink()
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,
}
}
r := tar.NewReader(io.NewSectionReader(f.r, i.off, i.sz))
if _, err := r.Next(); 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, path.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

0 comments on commit 2b4d980

Please sign in to comment.