From 4195a791de0357759deecd61a9195c8c2a03df04 Mon Sep 17 00:00:00 2001 From: Brian Upton Date: Tue, 3 Dec 2024 09:30:54 -0800 Subject: [PATCH] Fix behavior for StripComponents with leading ./ The previous reimplementation of tar --strip-components did not match how tar treats leading ./ strings in its file headers. --strip-components should treat the leading . as its own component. The BOSH agent's usage of the TarballCompressor relies on this behavior, and the change is causing failures. --- fileutil/tarball_compressor.go | 2 +- fileutil/tarball_compressor_test.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/fileutil/tarball_compressor.go b/fileutil/tarball_compressor.go index 23e8857d..db1dabc8 100644 --- a/fileutil/tarball_compressor.go +++ b/fileutil/tarball_compressor.go @@ -146,7 +146,7 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o fullName := filepath.Join(dir, filepath.FromSlash(header.Name)) if options.StripComponents > 0 { - components := strings.Split(filepath.Clean(header.Name), string(filepath.Separator)) + components := strings.Split(header.Name, string(filepath.Separator)) if len(components) <= options.StripComponents { continue } diff --git a/fileutil/tarball_compressor_test.go b/fileutil/tarball_compressor_test.go index d586f4d5..9fba5d89 100644 --- a/fileutil/tarball_compressor_test.go +++ b/fileutil/tarball_compressor_test.go @@ -353,9 +353,15 @@ var _ = Describe("tarballCompressor", func() { dstElements, err := pathsInDir(dstDir) Expect(err).ToNot(HaveOccurred()) + // tar --strip-components treats a leading `./` in the file headers as its own component. + // So ./dir/some-file becomes dir/some-file with strip-components = 1. + // The example tar file in this test contains the leading ./ for each of its files. Expect(dstElements).To(Equal([]string{ "./", - "double-nested-file", + "empty-nested-dir/", + "nested-dir/", + "nested-dir/double-nested-file", + "nested-file", })) }) })