Skip to content

Commit

Permalink
Merge pull request #422 from fujiwara/pr402
Browse files Browse the repository at this point in the history
treat symlink as file when adding to zip / #402
  • Loading branch information
fujiwara authored Aug 9, 2024
2 parents 4864b99 + fc0ed79 commit d51ba89
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
16 changes: 16 additions & 0 deletions archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ func matchExcludes(path string, excludes []string) bool {
}

func addToZip(z *zip.Writer, path, relpath string, info os.FileInfo) error {
// treat symlink as file
if info.Mode()&os.ModeSymlink != 0 {
link, err := os.Readlink(path)
if err != nil {
log.Printf("[error] failed to read symlink %s: %s", path, err)
return err
}
linkTarget := filepath.Join(filepath.Dir(path), link)
log.Printf("[debug] resolve symlink %s to %s", path, linkTarget)
info, err = os.Stat(linkTarget)
if err != nil {
log.Printf("[error] failed to stat symlink target %s: %s", linkTarget, err)
return err
}
path = linkTarget
}
header, err := zip.FileInfoHeader(info)
if err != nil {
log.Println("[error] failed to create zip file header", err)
Expand Down
6 changes: 3 additions & 3 deletions archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func testCreateZipArchive(t *testing.T, s zipTestSuite) {
excludes = append(excludes, []string{"*.bin", "skip/*"}...)
r, info, err := lambroll.CreateZipArchive(s.SrcDir, excludes)
if err != nil {
t.Error("faile to CreateZipArchive", err)
t.Error("failed to CreateZipArchive", err)
}
defer r.Close()
defer os.Remove(r.Name())
Expand All @@ -51,8 +51,8 @@ func testCreateZipArchive(t *testing.T, s zipTestSuite) {
if err != nil {
t.Error("failed to new zip reader", err)
}
if len(zr.File) != 4 {
t.Errorf("unexpected included files num %d expect %d", len(zr.File), 3)
if len(zr.File) != 6 {
t.Errorf("unexpected included files num %d expect %d", len(zr.File), 6)
}
for _, f := range zr.File {
h := f.FileHeader
Expand Down
1 change: 1 addition & 0 deletions test/ext/ext-hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ext-hello
1 change: 1 addition & 0 deletions test/src/ext-hello.txt
1 change: 1 addition & 0 deletions test/src/hello.symlink

0 comments on commit d51ba89

Please sign in to comment.