Skip to content

Commit

Permalink
Fix behavior for StripComponents with leading ./
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ystros committed Dec 3, 2024
1 parent 3dc5178 commit 4195a79
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fileutil/tarball_compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion fileutil/tarball_compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}))
})
})
Expand Down

0 comments on commit 4195a79

Please sign in to comment.