Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions fileutil/compressor_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ type CompressorOptions struct {
SameOwner bool
PathInArchive string
StripComponents int
NoCompression bool
}

type Compressor interface {
// CompressFilesInDir returns path to a compressed file
CompressFilesInDir(dir string) (path string, err error)
CompressFilesInDir(dir string, options CompressorOptions) (path string, err error)

CompressSpecificFilesInDir(dir string, files []string) (path string, err error)
CompressSpecificFilesInDir(dir string, files []string, options CompressorOptions) (path string, err error)

DecompressFileToDir(path string, dir string, options CompressorOptions) (err error)

Expand Down
10 changes: 6 additions & 4 deletions fileutil/fakes/fake_compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (

type FakeCompressor struct {
CompressFilesInDirDir string
CompressFilesInDirOptions boshcmd.CompressorOptions
CompressFilesInDirTarballPath string
CompressFilesInDirErr error
CompressFilesInDirCallBack func()

CompressSpecificFilesInDirDir string
CompressSpecificFilesInDirFiles []string
CompressSpecificFilesInDirOptions boshcmd.CompressorOptions
CompressSpecificFilesInDirTarballPath string
CompressSpecificFilesInDirErr error
CompressSpecificFilesInDirCallBack func()
Expand All @@ -30,20 +32,20 @@ func NewFakeCompressor() *FakeCompressor {
return &FakeCompressor{}
}

func (fc *FakeCompressor) CompressFilesInDir(dir string) (string, error) {
func (fc *FakeCompressor) CompressFilesInDir(dir string, options boshcmd.CompressorOptions) (string, error) {
fc.CompressFilesInDirDir = dir

fc.CompressFilesInDirOptions = options
if fc.CompressFilesInDirCallBack != nil {
fc.CompressFilesInDirCallBack()
}

return fc.CompressFilesInDirTarballPath, fc.CompressFilesInDirErr
}

func (fc *FakeCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
func (fc *FakeCompressor) CompressSpecificFilesInDir(dir string, files []string, options boshcmd.CompressorOptions) (string, error) {
fc.CompressSpecificFilesInDirDir = dir
fc.CompressSpecificFilesInDirFiles = files

fc.CompressSpecificFilesInDirOptions = options
if fc.CompressSpecificFilesInDirCallBack != nil {
fc.CompressSpecificFilesInDirCallBack()
}
Expand Down
11 changes: 7 additions & 4 deletions fileutil/tarball_compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func NewTarballCompressor(
return tarballCompressor{cmdRunner: cmdRunner, fs: fs}
}

func (c tarballCompressor) CompressFilesInDir(dir string) (string, error) {
return c.CompressSpecificFilesInDir(dir, []string{"."})
func (c tarballCompressor) CompressFilesInDir(dir string, options CompressorOptions) (string, error) {
return c.CompressSpecificFilesInDir(dir, []string{"."}, options)
}

func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string, options CompressorOptions) (string, error) {
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
Expand All @@ -34,7 +34,10 @@ func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string

tarballPath := tarball.Name()

args := []string{"-czf", tarballPath, "-C", dir}
args := []string{"-cf", tarballPath, "-C", dir}
if !options.NoCompression {
args = append(args, "-z")
}
if runtime.GOOS == "darwin" {
args = append([]string{"--no-mac-metadata"}, args...)
}
Expand Down
28 changes: 26 additions & 2 deletions fileutil/tarball_compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("tarballCompressor", func() {

defer os.Remove(symlinkPath)

tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir)
tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir, CompressorOptions{})
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tgzName)

Expand Down Expand Up @@ -94,6 +94,30 @@ var _ = Describe("tarballCompressor", func() {
Expect(err).ToNot(HaveOccurred())
Expect(content).To(ContainSubstring("this is other app stdout"))
})

It("uses NoCompression option to create uncompressed tarball", func() {
cmdRunner := fakesys.NewFakeCmdRunner()
compressor := NewTarballCompressor(cmdRunner, fs)

tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir, CompressorOptions{NoCompression: true})
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tgzName)

Expect(1).To(Equal(len(cmdRunner.RunCommands)))
Expect(cmdRunner.RunCommands[0]).ToNot(ContainElement("-z"))
})

It("uses compression by default when NoCompression is false", func() {
cmdRunner := fakesys.NewFakeCmdRunner()
compressor := NewTarballCompressor(cmdRunner, fs)

tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir, CompressorOptions{NoCompression: false})
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tgzName)

Expect(1).To(Equal(len(cmdRunner.RunCommands)))
Expect(cmdRunner.RunCommands[0]).To(ContainElement("-z"))
})
})

Describe("CompressSpecificFilesInDir", func() {
Expand All @@ -104,7 +128,7 @@ var _ = Describe("tarballCompressor", func() {
"some_directory",
"app.stderr.log",
}
tgzName, err := compressor.CompressSpecificFilesInDir(srcDir, files)
tgzName, err := compressor.CompressSpecificFilesInDir(srcDir, files, CompressorOptions{})
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tgzName)

Expand Down