From 03dbe70b0d4d10e10ef0d14cf5a07efe29611dc0 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Fri, 11 Oct 2024 21:40:40 +0300 Subject: [PATCH] Always delete downloader temporary data file If the download failed, the temporary data file (data.tmp.pid) was not deleted. Ensure it is delete in all cases. Also move the creation of the temporary file right before we need it. The chance that creating a temporary file will fail is practically zero so there is no need to do this upfront. This makes the code easier to follow and maintain. Signed-off-by: Nir Soffer --- pkg/downloader/downloader.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index 18418e845b0..c64495d781e 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -599,13 +599,6 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url } logrus.Debugf("downloading %q into %q", url, localPath) - localPathTmp := perProcessTempfile(localPath) - fileWriter, err := os.Create(localPathTmp) - if err != nil { - return err - } - defer fileWriter.Close() - resp, err := httpclientutil.Get(ctx, http.DefaultClient, url) if err != nil { return err @@ -631,6 +624,14 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url hideBar(bar) } + localPathTmp := perProcessTempfile(localPath) + fileWriter, err := os.Create(localPathTmp) + if err != nil { + return err + } + defer fileWriter.Close() + defer os.RemoveAll(localPathTmp) + writers := []io.Writer{fileWriter} var digester digest.Digester if expectedDigest != "" {