From 6dcc6fb50d6634b691bfe9434ea7f320f5ea90f0 Mon Sep 17 00:00:00 2001 From: Daniel Mikusa Date: Fri, 20 Aug 2021 13:10:27 -0400 Subject: [PATCH] Fixes a bug where file listing would fail if there is a relative symlink. This will now resolve relative symlinks based on the parent directory of the symlink. Ex: /foo/bar/my-link -> my-file Then: it resolves my-link to /foo/bar/my-file Signed-off-by: Daniel Mikusa --- sherpa/file_listing.go | 11 +++++++---- sherpa/file_listing_test.go | 12 +++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sherpa/file_listing.go b/sherpa/file_listing.go index d0ee771..69b6890 100644 --- a/sherpa/file_listing.go +++ b/sherpa/file_listing.go @@ -162,12 +162,15 @@ func process(entry FileEntry) (FileEntry, error) { return entry, nil } -func isSymlinkToDir(root string, f fs.FileInfo) (bool, error) { +func isSymlinkToDir(symlink string, f fs.FileInfo) (bool, error) { if f.Mode().Type() == fs.ModeSymlink { - // rawPath := filepath.Join(root, f.Name()) - path, err := os.Readlink(root) + path, err := os.Readlink(symlink) if err != nil { - return false, fmt.Errorf("unable to read symlink %s\n%w", root, err) + return false, fmt.Errorf("unable to read symlink %s\n%w", symlink, err) + } + + if !filepath.IsAbs(path) { + path = filepath.Join(filepath.Dir(symlink), path) } stat, err := os.Stat(path) diff --git a/sherpa/file_listing_test.go b/sherpa/file_listing_test.go index f390e9c..0cd6660 100644 --- a/sherpa/file_listing_test.go +++ b/sherpa/file_listing_test.go @@ -65,17 +65,19 @@ func testFileListing(t *testing.T, context spec.G, it spec.S) { Expect(ioutil.WriteFile(filepath.Join(path, "test-directory", "bravo.txt"), []byte{2}, 0644)).To(Succeed()) Expect(os.Symlink(filepath.Join(path, "test-directory"), filepath.Join(path, "symlink-test-dir"))) Expect(os.Symlink(filepath.Join(path, "test-directory", "bravo.txt"), filepath.Join(path, "symlink-bravo.txt"))) + Expect(os.Symlink("alpha.txt", filepath.Join(path, "symlink-relative.txt"))) e, err := sherpa.NewFileListing(path) Expect(err).NotTo(HaveOccurred()) - Expect(e).To(HaveLen(5)) + Expect(e).To(HaveLen(6)) Expect(e[0].Path).To(HaveSuffix("alpha.txt")) Expect(e[1].Path).To(HaveSuffix("symlink-bravo.txt")) - Expect(e[2].Path).To(HaveSuffix("symlink-test-dir")) - Expect(e[3].Path).To(HaveSuffix("test-directory")) - Expect(e[4].Path).To(HaveSuffix("bravo.txt")) - Expect(e[1].SHA256).To(Equal(e[4].SHA256)) // symlink to file should have hash of target file + Expect(e[2].Path).To(HaveSuffix("symlink-relative.txt")) + Expect(e[3].Path).To(HaveSuffix("symlink-test-dir")) + Expect(e[4].Path).To(HaveSuffix("test-directory")) + Expect(e[5].Path).To(HaveSuffix("bravo.txt")) + Expect(e[1].SHA256).To(Equal(e[5].SHA256)) // symlink to file should have hash of target file }) it("create listing and get SHA256", func() {