Skip to content

Commit

Permalink
fix(push): use basename for artifacts when compressed in tar.gz
Browse files Browse the repository at this point in the history
When the artifact to be pushed has a folder in its path the full
name is used in the tar headers. This is an issue when we install the
artifact. This commits adds a flag that uses the basename when compressing
the artifact in the tar header.

Signed-off-by: Aldo Lacuku <[email protected]>
  • Loading branch information
alacuku committed Jul 24, 2024
1 parent 36b951f commit 18361df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/registry/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (o *pushOptions) runPush(ctx context.Context, args []string) error {
return err
}
}
path, err := utils.CreateTarGzArchive("", p)
path, err := utils.CreateTarGzArchive("", p, true)
if err != nil {
return err
}
Expand Down
18 changes: 13 additions & 5 deletions internal/utils/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
const TmpDirPrefix = "falcoctl-registry-push-"

// CreateTarGzArchive compresses and saves in a tar archive the passed file.
func CreateTarGzArchive(dir, path string) (file string, err error) {
func CreateTarGzArchive(dir, path string, stripComponents bool) (file string, err error) {
cleanedPath := filepath.Clean(path)
if dir == "" {
dir = TmpDirPrefix
Expand Down Expand Up @@ -96,23 +96,31 @@ func CreateTarGzArchive(dir, path string) (file string, err error) {
return nil
}

return copyToTarGz(path, tw, info)
return copyToTarGz(path, tw, info, stripComponents)
})
if err != nil {
return "", err
}
} else {
if err = copyToTarGz(path, tw, fInfo); err != nil {
if err = copyToTarGz(path, tw, fInfo, stripComponents); err != nil {
return "", err
}
}

return outFile.Name(), err
}

func copyToTarGz(path string, tw *tar.Writer, info fs.FileInfo) error {
func copyToTarGz(path string, tw *tar.Writer, info fs.FileInfo, stripComponents bool) error {
var headerName string

if stripComponents {
headerName = filepath.Base(path)
} else {
headerName = path
}

header := &tar.Header{
Name: path,
Name: headerName,
Size: info.Size(),
Mode: int64(info.Mode()),
Typeflag: tar.TypeReg,
Expand Down
39 changes: 37 additions & 2 deletions internal/utils/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCreateTarGzArchiveFile(t *testing.T) {
}
defer f1.Close()

tarball, err := CreateTarGzArchive(tmpPrefix, filepath.Join(dir, filename1))
tarball, err := CreateTarGzArchive(tmpPrefix, filepath.Join(dir, filename1), false)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -67,6 +67,41 @@ func TestCreateTarGzArchiveFile(t *testing.T) {
}
}

func TestCreateTarGzArchiveFileStripComponents(t *testing.T) {
dir := t.TempDir()
f1, err := os.Create(filepath.Join(dir, filename1))
if err != nil {
t.Fatalf(err.Error())
}
defer f1.Close()

tarball, err := CreateTarGzArchive(tmpPrefix, filepath.Join(dir, filename1), true)
if err != nil {
t.Fatalf(err.Error())
}
defer os.RemoveAll(filepath.Dir(tarball))

file, err := os.Open(tarball)
if err != nil {
t.Fatalf(err.Error())
}

paths, err := listHeaders(file)
fmt.Println(paths)
if err != nil {
t.Fatalf(err.Error())
}

if len(paths) != 1 {
t.Fatalf("Expected 1 path, got %d", len(paths))
}

base := paths[0]
if base != filename1 {
t.Errorf("Expected file1, got %s", base)
}
}

func TestCreateTarGzArchiveDir(t *testing.T) {
// Test that we can compress directories
dir := t.TempDir()
Expand All @@ -83,7 +118,7 @@ func TestCreateTarGzArchiveDir(t *testing.T) {
}
defer f2.Close()

tarball, err := CreateTarGzArchive(tmpPrefix, dir)
tarball, err := CreateTarGzArchive(tmpPrefix, dir, false)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/driver/type/modernbpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
package drivertype

import (
// Needed for go:linkname to be able to access a private function from cilium/ebpf/features.
_ "unsafe"
_ "unsafe" // Needed for go:linkname to be able to access a private function from cilium/ebpf/features.

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/features"
Expand Down

0 comments on commit 18361df

Please sign in to comment.