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

feat: support links inside archives #2436

Merged
merged 2 commits into from Sep 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/archive/targz/targz.go
Expand Up @@ -43,7 +43,7 @@ func (a Archive) Add(f config.File) error {
return err
}
defer file.Close()
info, err := file.Stat()
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return err
}
Expand All @@ -66,6 +66,13 @@ func (a Archive) Add(f config.File) error {
header.Gid = 0
header.Gname = f.Info.Group
}
if info.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(f.Source)
if err != nil {
return err
}
header.Linkname = target
}
if err = a.tw.WriteHeader(header); err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/archive/targz/targz_test.go
Expand Up @@ -50,6 +50,14 @@ func TestTarGzFile(t *testing.T) {
Source: "../testdata/sub1/sub2/subfoo.txt",
Destination: "sub1/sub2/subfoo.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/regular.txt",
Destination: "regular.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/link.txt",
Destination: "link.txt",
}))

require.NoError(t, archive.Close())
require.Error(t, archive.Add(config.File{
Expand Down Expand Up @@ -84,6 +92,9 @@ func TestTarGzFile(t *testing.T) {
ex := next.FileInfo().Mode() | 0o111
require.Equal(t, next.FileInfo().Mode().String(), ex.String())
}
if next.Name == "link.txt" {
require.Equal(t, next.Linkname, "regular.txt")
}
}
require.Equal(t, []string{
"foo.txt",
Expand All @@ -92,6 +103,8 @@ func TestTarGzFile(t *testing.T) {
"sub1/executable",
"sub1/sub2",
"sub1/sub2/subfoo.txt",
"regular.txt",
"link.txt",
}, paths)
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/archive/tarxz/tarxz.go
Expand Up @@ -42,7 +42,7 @@ func (a Archive) Add(f config.File) error {
return err
}
defer file.Close()
info, err := file.Stat()
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return err
}
Expand All @@ -65,10 +65,17 @@ func (a Archive) Add(f config.File) error {
header.Gid = 0
header.Gname = f.Info.Group
}
if info.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(f.Source)
if err != nil {
return err
}
header.Linkname = target
}
if err = a.tw.WriteHeader(header); err != nil {
return err
}
if info.IsDir() {
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil
}
_, err = io.Copy(a.tw, file)
Expand Down
13 changes: 13 additions & 0 deletions pkg/archive/tarxz/tarxz_test.go
Expand Up @@ -50,6 +50,14 @@ func TestTarXzFile(t *testing.T) {
Source: "../testdata/sub1/sub2/subfoo.txt",
Destination: "sub1/sub2/subfoo.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/regular.txt",
Destination: "regular.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/link.txt",
Destination: "link.txt",
}))

require.NoError(t, archive.Close())
require.Error(t, archive.Add(config.File{
Expand Down Expand Up @@ -83,6 +91,9 @@ func TestTarXzFile(t *testing.T) {
ex := next.FileInfo().Mode() | 0o111
require.Equal(t, next.FileInfo().Mode().String(), ex.String())
}
if next.Name == "link.txt" {
require.Equal(t, next.Linkname, "regular.txt")
}
}
require.Equal(t, []string{
"foo.txt",
Expand All @@ -91,6 +102,8 @@ func TestTarXzFile(t *testing.T) {
"sub1/executable",
"sub1/sub2",
"sub1/sub2/subfoo.txt",
"regular.txt",
"link.txt",
}, paths)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/archive/testdata/link.txt
Empty file.
2 changes: 1 addition & 1 deletion pkg/archive/zip/zip.go
Expand Up @@ -39,7 +39,7 @@ func (a Archive) Add(f config.File) error {
return err
}
defer file.Close()
info, err := file.Stat()
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/archive/zip/zip_test.go
Expand Up @@ -48,6 +48,14 @@ func TestZipFile(t *testing.T) {
Source: "../testdata/sub1/sub2/subfoo.txt",
Destination: "sub1/sub2/subfoo.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/regular.txt",
Destination: "regular.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/link.txt",
Destination: "link.txt",
}))

require.NoError(t, archive.Close())
require.Error(t, archive.Add(config.File{
Expand Down Expand Up @@ -75,12 +83,17 @@ func TestZipFile(t *testing.T) {
ex := zf.Mode() | 0o111
require.Equal(t, zf.Mode().String(), ex.String())
}
if zf.Name == "link.txt" {
require.True(t, zf.FileInfo().Mode()&os.ModeSymlink != 0)
}
}
require.Equal(t, []string{
"foo.txt",
"sub1/bar.txt",
"sub1/executable",
"sub1/sub2/subfoo.txt",
"regular.txt",
"link.txt",
}, paths)
}

Expand Down