diff --git a/archive.go b/archive.go index e2f1d42..c8ac1b1 100644 --- a/archive.go +++ b/archive.go @@ -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) diff --git a/archive_test.go b/archive_test.go index c6dfebd..4eda59d 100644 --- a/archive_test.go +++ b/archive_test.go @@ -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()) @@ -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 diff --git a/test/ext/ext-hello.txt b/test/ext/ext-hello.txt new file mode 100644 index 0000000..8dc6f1a --- /dev/null +++ b/test/ext/ext-hello.txt @@ -0,0 +1 @@ +ext-hello diff --git a/test/src/ext-hello.txt b/test/src/ext-hello.txt new file mode 120000 index 0000000..e7219a0 --- /dev/null +++ b/test/src/ext-hello.txt @@ -0,0 +1 @@ +../ext/ext-hello.txt \ No newline at end of file diff --git a/test/src/hello.symlink b/test/src/hello.symlink new file mode 120000 index 0000000..a5162f8 --- /dev/null +++ b/test/src/hello.symlink @@ -0,0 +1 @@ +hello.txt \ No newline at end of file