Skip to content

Commit

Permalink
fix(registry/push): correctly remove temporary dir
Browse files Browse the repository at this point in the history
When pushing multiple plugins we need to remove the temporary dirs created
when compressing them. This commit implements a fix that tracks
all the temporary dirs created at runtime and removes them at the end.

Signed-off-by: Aldo Lacuku <[email protected]>
  • Loading branch information
alacuku committed Mar 15, 2024
1 parent 816f79c commit 8bcb9b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions cmd/registry/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func (o *pushOptions) runPush(ctx context.Context, args []string) error {
ref := args[0]
paths := args[1:]
// When creating the tar.gz archives we need to remove them after we are done.
// We save the temporary dir where they live here.
var toBeDeleted string
// Holds the path for each temporary dir.
var toBeDeletedTmpDirs []string
logger := o.Printer.Logger

registry, err := utils.GetRegistryFromRef(ref)
Expand All @@ -144,10 +144,13 @@ func (o *pushOptions) runPush(ctx context.Context, args []string) error {

logger.Info("Preparing to push artifact", o.Printer.Logger.Args("name", args[0], "type", o.ArtifactType))

// Make sure to remove temporary working dir.
// Make sure to remove temporary working dirs.
defer func() {
if err := os.RemoveAll(toBeDeleted); err != nil {
logger.Warn("Unable to remove temporary dir", logger.Args("name", toBeDeleted, "error", err.Error()))
for _, dir := range toBeDeletedTmpDirs {
logger.Debug("Removing temporary dir", logger.Args("name", dir))
if err := os.RemoveAll(dir); err != nil {
logger.Warn("Unable to remove temporary dir", logger.Args("name", dir, "error", err.Error()))
}
}
}()

Expand All @@ -172,9 +175,7 @@ func (o *pushOptions) runPush(ctx context.Context, args []string) error {
return err
}
paths[i] = path
if toBeDeleted == "" {
toBeDeleted = filepath.Dir(path)
}
toBeDeletedTmpDirs = append(toBeDeletedTmpDirs, filepath.Dir(path))
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/utils/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// TmpDirPrefix prefix used for the temporary directory where the tar.gz archives live before pushing
// to the OCI registry.
const TmpDirPrefix = "falcoctl-registry-push"
const TmpDirPrefix = "falcoctl-registry-push-"

// CreateTarGzArchive compresses and saves in a tar archive the passed file.
func CreateTarGzArchive(path string) (file string, err error) {
Expand Down

0 comments on commit 8bcb9b0

Please sign in to comment.