From 7f488fc136dd7f1dadc2076c05b400467f463690 Mon Sep 17 00:00:00 2001 From: rkoster <hi@rkoster.dev> Date: Thu, 17 Oct 2024 10:19:03 +0200 Subject: [PATCH] Incorporate review feedback - don't use import alias - close all readers / writers - fix typo - also use bosh filesystem interface when reading file contents --- fileutil/tarball_compressor.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/fileutil/tarball_compressor.go b/fileutil/tarball_compressor.go index 3673ba6c..dd753388 100644 --- a/fileutil/tarball_compressor.go +++ b/fileutil/tarball_compressor.go @@ -9,7 +9,7 @@ import ( "path/filepath" "archive/tar" - gzip "github.com/klauspost/pgzip" + "github.com/klauspost/pgzip" bosherr "github.com/cloudfoundry/bosh-utils/errors" boshsys "github.com/cloudfoundry/bosh-utils/system" @@ -37,7 +37,7 @@ func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string defer tarball.Close() - zw := gzip.NewWriter(tarball) + zw := pgzip.NewWriter(tarball) tw := tar.NewWriter(zw) for _, file := range files { @@ -66,7 +66,9 @@ func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string } if fi.Mode().IsRegular() { - data, err := os.Open(f) + data, err := c.fs.OpenFile(f, os.O_RDONLY, 0) + defer data.Close() + if err != nil { return bosherr.WrapError(err, "Reading tar source file") } @@ -99,11 +101,15 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o } tarball, err := c.fs.OpenFile(tarballPath, os.O_RDONLY, 0) + defer tarball.Close() + if err != nil { return bosherr.WrapError(err, "Opening tarball") } - zr, err := gzip.NewReader(tarball) + zr, err := pgzip.NewReader(tarball) + defer zr.Close() + if err != nil { return bosherr.WrapError(err, "Creating gzip reader") } @@ -151,7 +157,7 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o return bosherr.WrapError(err, "Decompressing file contents") } default: - return fmt.Errorf("uknown type: %v in %s for tar: %s", + return fmt.Errorf("unknown type: %v in %s for tar: %s", header.Typeflag, header.Name, tarballPath) }