diff --git a/fileutil/compressor_interface.go b/fileutil/compressor_interface.go index 5a69543d..3ec51cd3 100644 --- a/fileutil/compressor_interface.go +++ b/fileutil/compressor_interface.go @@ -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) diff --git a/fileutil/fakes/fake_compressor.go b/fileutil/fakes/fake_compressor.go index 38c218ae..7f5aafae 100644 --- a/fileutil/fakes/fake_compressor.go +++ b/fileutil/fakes/fake_compressor.go @@ -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() @@ -30,9 +32,9 @@ 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() } @@ -40,10 +42,10 @@ func (fc *FakeCompressor) CompressFilesInDir(dir string) (string, error) { 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() } diff --git a/fileutil/tarball_compressor.go b/fileutil/tarball_compressor.go index 997f1f3c..037eb7d6 100644 --- a/fileutil/tarball_compressor.go +++ b/fileutil/tarball_compressor.go @@ -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") @@ -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...) } @@ -61,7 +64,7 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o if err != nil { return bosherr.WrapError(err, "Resolving tarball path") } - args := []string{sameOwnerOption, "-xzf", resolvedTarballPath, "-C", dir} + args := []string{sameOwnerOption, "-xf", resolvedTarballPath, "-C", dir} if options.StripComponents != 0 { args = append(args, fmt.Sprintf("--strip-components=%d", options.StripComponents)) } diff --git a/fileutil/tarball_compressor_test.go b/fileutil/tarball_compressor_test.go index 2a80682e..c3a74bff 100644 --- a/fileutil/tarball_compressor_test.go +++ b/fileutil/tarball_compressor_test.go @@ -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) @@ -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() { @@ -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) @@ -182,7 +206,7 @@ var _ = Describe("tarballCompressor", func() { Expect(cmdRunner.RunCommands[0]).To(Equal( []string{ "tar", "--no-same-owner", - "-xzf", tarballPath, + "-xf", tarballPath, "-C", dstDir, }, )) @@ -204,7 +228,7 @@ var _ = Describe("tarballCompressor", func() { Expect(cmdRunner.RunCommands[0]).To(Equal( []string{ "tar", "--same-owner", - "-xzf", tarballPath, + "-xf", tarballPath, "-C", dstDir, }, )) @@ -222,7 +246,7 @@ var _ = Describe("tarballCompressor", func() { Expect(cmdRunner.RunCommands[0]).To(Equal( []string{ "tar", "--no-same-owner", - "-xzf", tarballPath, + "-xf", tarballPath, "-C", dstDir, "some/path/in/archive", }, @@ -241,7 +265,7 @@ var _ = Describe("tarballCompressor", func() { Expect(cmdRunner.RunCommands[0]).To(Equal( []string{ "tar", "--no-same-owner", - "-xzf", tarballPath, + "-xf", tarballPath, "-C", dstDir, "--strip-components=3", },