Skip to content

Commit

Permalink
fix: ensure files are places in their directories when creating tar.g…
Browse files Browse the repository at this point in the history
…z archives

Signed-off-by: Lorenzo Susini <[email protected]>
  • Loading branch information
loresuso committed Nov 20, 2023
1 parent e8a4485 commit a923c89
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/utils/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit a923c89

Please sign in to comment.