1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2024-12-29 01:44:39 +02:00

feat: support links inside archives (#2436)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-08-31 23:08:07 -03:00 committed by GitHub
parent b132d00aec
commit 275e17bc76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 4 deletions

View File

@ -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
}
@ -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
}

View File

@ -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{
@ -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",
@ -92,6 +103,8 @@ func TestTarGzFile(t *testing.T) {
"sub1/executable",
"sub1/sub2",
"sub1/sub2/subfoo.txt",
"regular.txt",
"link.txt",
}, paths)
}

View File

@ -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
}
@ -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)

View File

@ -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{
@ -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",
@ -91,6 +102,8 @@ func TestTarXzFile(t *testing.T) {
"sub1/executable",
"sub1/sub2",
"sub1/sub2/subfoo.txt",
"regular.txt",
"link.txt",
}, paths)
}

1
pkg/archive/testdata/link.txt vendored Symbolic link
View File

@ -0,0 +1 @@
regular.txt

0
pkg/archive/testdata/regular.txt vendored Normal file
View File

View File

@ -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
}

View File

@ -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{
@ -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)
}