From 810df1700916e02f3cd8669453ce39ecd0c7023a Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Mon, 16 May 2022 16:56:58 -0500 Subject: [PATCH] Clean up temporary files when mirroring Signed-off-by: Ryan Gonzalez --- api/mirror.go | 12 ++++++++++++ cmd/mirror_update.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/api/mirror.go b/api/mirror.go index 83dca0ee..69684b06 100644 --- a/api/mirror.go +++ b/api/mirror.go @@ -542,6 +542,18 @@ func apiMirrorsUpdate(c *gin.Context) { log.Info().Msgf("%s: Background processes finished", b.Name) close(taskFinished) + defer func() { + for _, task := range queue { + if task.TempDownPath == "" { + continue + } + + if err := os.Remove(task.TempDownPath); err != nil && !os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "Failed to delete %s: %v\n", task.TempDownPath, err) + } + } + }() + select { case <-context.Done(): return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: interrupted") diff --git a/cmd/mirror_update.go b/cmd/mirror_update.go index f2219bcd..2bca585a 100644 --- a/cmd/mirror_update.go +++ b/cmd/mirror_update.go @@ -208,6 +208,18 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error { return fmt.Errorf("unable to update: %s", err) } + defer func() { + for _, task := range queue { + if task.TempDownPath == "" { + continue + } + + if err := os.Remove(task.TempDownPath); err != nil && !os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "Failed to delete %s: %v\n", task.TempDownPath, err) + } + } + }() + // Import downloaded files context.Progress().InitBar(int64(len(queue)), false, aptly.BarMirrorUpdateImportFiles)