From a923c895d56daad17075a85c532074349c6768b3 Mon Sep 17 00:00:00 2001 From: Lorenzo Susini Date: Mon, 20 Nov 2023 16:49:22 +0000 Subject: [PATCH] fix: ensure files are places in their directories when creating tar.gz archives Signed-off-by: Lorenzo Susini --- internal/utils/compress.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/utils/compress.go b/internal/utils/compress.go index 4d560cb54..9f319ea44 100644 --- a/internal/utils/compress.go +++ b/internal/utils/compress.go @@ -31,6 +31,9 @@ import ( const TmpDirPrefix = "falcoctl-registry-push" // CreateTarGzArchive compresses and saves in a tar archive the passed file. +// The purpose of this function is to create a tar.gz when the input is: +// - a directory. In this case, the tar.gz will contain all the files in the directory, but not its subdirectories. +// - a file. In this case, the tar.gz will contain only the file. func CreateTarGzArchive(path string) (file string, err error) { cleanedPath := filepath.Clean(path) // Create output file. @@ -74,7 +77,7 @@ func CreateTarGzArchive(path string) (file string, err error) { if fInfo.IsDir() { // write header of the directory - header, err := tar.FileInfoHeader(fInfo, fInfo.Name()) + header, err := tar.FileInfoHeader(fInfo, path) if err != nil { return "", err } @@ -108,13 +111,15 @@ func CreateTarGzArchive(path string) (file string, err error) { } func copyToTarGz(path string, tw *tar.Writer, info fs.FileInfo) error { - header, err := tar.FileInfoHeader(info, info.Name()) - if err != nil { - return err + header := &tar.Header{ + Name: path, + Size: info.Size(), + Mode: int64(info.Mode()), + Typeflag: tar.TypeReg, } // write the header - if err = tw.WriteHeader(header); err != nil { + if err := tw.WriteHeader(header); err != nil { return err }