diff --git a/cmd/registry/push/push.go b/cmd/registry/push/push.go index fa97cf98..73410230 100644 --- a/cmd/registry/push/push.go +++ b/cmd/registry/push/push.go @@ -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 } diff --git a/internal/utils/compress.go b/internal/utils/compress.go index f84c258d..d648b688 100644 --- a/internal/utils/compress.go +++ b/internal/utils/compress.go @@ -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 @@ -96,13 +96,13 @@ 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 } } @@ -110,9 +110,17 @@ func CreateTarGzArchive(dir, path string) (file string, err error) { 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, diff --git a/internal/utils/compress_test.go b/internal/utils/compress_test.go index 36790567..d035ada6 100644 --- a/internal/utils/compress_test.go +++ b/internal/utils/compress_test.go @@ -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()) } @@ -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() @@ -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()) } diff --git a/pkg/driver/type/modernbpf.go b/pkg/driver/type/modernbpf.go index bea76110..a04a804f 100644 --- a/pkg/driver/type/modernbpf.go +++ b/pkg/driver/type/modernbpf.go @@ -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"