Skip to content

Commit

Permalink
feat: support links inside archives (#2436)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Sep 1, 2021
1 parent b132d00 commit 275e17b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 4 deletions.
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

1 comment on commit 275e17b

@vercel
Copy link

@vercel vercel bot commented on 275e17b Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.