Skip to content

Commit

Permalink
Handle tarballs that do not have dir headers
Browse files Browse the repository at this point in the history
The test asset tarball added by this commit contains the following
entries:
```
dir/file1
dir/file2
dir/nested-dir/file3
```

It explicitly lacks entries for `dir/` and `dir/nested-dir/`. This is
how the `tar` executable generates tarballs when specifying individual
files, and is how the BOSH Director creates the file bundle tarballs
sent to the BOSH agent.
  • Loading branch information
ystros authored and jpalermo committed Dec 4, 2024
1 parent 9b77f09 commit d718415
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fileutil/tarball_compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o
}

case tar.TypeReg:
directoryPath := filepath.Dir(fullName)
if err := c.fs.MkdirAll(directoryPath, fs.FileMode(0755)); err != nil {
return bosherr.WrapError(err, "Creating directory for decompressed file")
}

outFile, err := c.fs.OpenFile(fullName, os.O_CREATE|os.O_WRONLY, fs.FileMode(header.Mode))
if err != nil {
return bosherr.WrapError(err, "Creating decompressed file")
Expand Down
21 changes: 21 additions & 0 deletions fileutil/tarball_compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,27 @@ var _ = Describe("tarballCompressor", func() {
Expect(err.Error()).To(ContainSubstring(dstDir))
})

It("creates sub-directories even if the tarball does not have a header entry for the directory", func() {
compressor := NewTarballCompressor(fs)

// The contents of this tarball does not contain entries for the dir/ or dir/nested-dir/ directories. The tar executable
// automatically creates these directories when extracting files listed under the directories.
tarballPath := filepath.Join(fixtureSrcDir(), filepath.FromSlash("../compressor-decompress-missing-directory-header.tgz"))
err := compressor.DecompressFileToDir(tarballPath, dstDir, CompressorOptions{})
Expect(err).ToNot(HaveOccurred())

dstElements, err := pathsInDir(dstDir)
Expect(err).ToNot(HaveOccurred())
Expect(dstElements).To(Equal([]string{
"./",
"dir/",
"dir/file1",
"dir/file2",
"dir/nested-dir/",
"dir/nested-dir/file3",
}))
})

Context("with tarball contents owned by root", func() {

var (
Expand Down
Binary file not shown.

0 comments on commit d718415

Please sign in to comment.