From 263d2ce49d2a92130be913536bdee4c9d268670a Mon Sep 17 00:00:00 2001 From: Patrick Crosby Date: Mon, 9 Mar 2020 14:29:50 -0500 Subject: [PATCH] Handle symlinks properly when zipping a directory (#22981) --- go/service/saltpack.go | 23 +++++++++++++++++++++-- go/service/testdata/archive/symlink_to_2 | 1 + go/service/testdata/archive/symlink_to_a | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) create mode 120000 go/service/testdata/archive/symlink_to_2 create mode 120000 go/service/testdata/archive/symlink_to_a diff --git a/go/service/saltpack.go b/go/service/saltpack.go index ec932ec05487..d5eca8f61663 100644 --- a/go/service/saltpack.go +++ b/go/service/saltpack.go @@ -917,9 +917,28 @@ func zipDir(directory string, prog *progress.ProgressWriter) io.ReadCloser { return nil } + if (info.Mode() & os.ModeSymlink) != 0 { + // figure out the destination of the link + dest, err := os.Readlink(path) + if err != nil { + return err + } + header.Name = stripped + zw, err := w.CreateHeader(header) + if err != nil { + return err + } + // the body of the entry is the link destination + _, err = zw.Write([]byte(dest)) + if err != nil { + return err + } + return nil + } + // make a regular file and copy all the data into it. header.Name = stripped - w, err := w.CreateHeader(header) + zw, err := w.CreateHeader(header) if err != nil { return err } @@ -929,7 +948,7 @@ func zipDir(directory string, prog *progress.ProgressWriter) io.ReadCloser { } defer f.Close() tr := io.TeeReader(bufio.NewReader(f), prog) - _, err = io.Copy(w, tr) + _, err = io.Copy(zw, tr) if err != nil { return err } diff --git a/go/service/testdata/archive/symlink_to_2 b/go/service/testdata/archive/symlink_to_2 new file mode 120000 index 000000000000..d8263ee98605 --- /dev/null +++ b/go/service/testdata/archive/symlink_to_2 @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/go/service/testdata/archive/symlink_to_a b/go/service/testdata/archive/symlink_to_a new file mode 120000 index 000000000000..8d14cbf983b3 --- /dev/null +++ b/go/service/testdata/archive/symlink_to_a @@ -0,0 +1 @@ +a.txt \ No newline at end of file