Skip to content

Commit

Permalink
fix: retain symlinks added to zip archives
Browse files Browse the repository at this point in the history
  • Loading branch information
orirawlings committed Nov 22, 2022
1 parent 090365b commit d9bb45b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/archive/zip/zip.go
Expand Up @@ -5,8 +5,10 @@ package zip
import (
"archive/zip"
"compress/flate"
"fmt"
"io"
"os"
"path/filepath"

"github.com/goreleaser/goreleaser/pkg/config"
)
Expand Down Expand Up @@ -57,9 +59,17 @@ func (a Archive) Add(f config.File) error {
if err != nil {
return err
}
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
if info.IsDir() {
return nil
}
if info.Mode()&os.ModeSymlink != 0 {
link, err := os.Readlink(f.Source) // #nosec
if err != nil {
return fmt.Errorf("%s: %w", f.Source, err)
}
_, err = w.Write([]byte(filepath.ToSlash(link)))
return err
}
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions pkg/archive/zip/zip_test.go
Expand Up @@ -2,6 +2,7 @@ package zip

import (
"archive/zip"
"bytes"
"io"
"io/fs"
"os"
Expand Down Expand Up @@ -85,6 +86,13 @@ func TestZipFile(t *testing.T) {
}
if zf.Name == "link.txt" {
require.True(t, zf.FileInfo().Mode()&os.ModeSymlink != 0)
rc, err := zf.Open()
require.NoError(t, err)
var content bytes.Buffer
_, err = io.Copy(&content, rc)
require.NoError(t, err)
rc.Close()
require.Equal(t, content.String(), "regular.txt")
}
}
require.Equal(t, []string{
Expand Down

0 comments on commit d9bb45b

Please sign in to comment.