Browse code

Merge pull request #21676 from aaronlehmann/tmpfile-close

Pull: only close temporary file once

Vincent Demeester authored on 2016/03/31 18:49:37
Showing 2 changed files
... ...
@@ -334,7 +334,20 @@ func (ld *v1LayerDescriptor) Download(ctx context.Context, progressOutput progre
334 334
 	logrus.Debugf("Downloaded %s to tempfile %s", ld.ID(), ld.tmpFile.Name())
335 335
 
336 336
 	ld.tmpFile.Seek(0, 0)
337
-	return ld.tmpFile, ld.layerSize, nil
337
+
338
+	// hand off the temporary file to the download manager, so it will only
339
+	// be closed once
340
+	tmpFile := ld.tmpFile
341
+	ld.tmpFile = nil
342
+
343
+	return ioutils.NewReadCloserWrapper(tmpFile, func() error {
344
+		tmpFile.Close()
345
+		err := os.RemoveAll(tmpFile.Name())
346
+		if err != nil {
347
+			logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
348
+		}
349
+		return err
350
+	}), ld.layerSize, nil
338 351
 }
339 352
 
340 353
 func (ld *v1LayerDescriptor) Close() {
... ...
@@ -278,7 +278,19 @@ func (ld *v2LayerDescriptor) Download(ctx context.Context, progressOutput progre
278 278
 		ld.verifier = nil
279 279
 		return nil, 0, xfer.DoNotRetry{Err: err}
280 280
 	}
281
-	return tmpFile, size, nil
281
+
282
+	// hand off the temporary file to the download manager, so it will only
283
+	// be closed once
284
+	ld.tmpFile = nil
285
+
286
+	return ioutils.NewReadCloserWrapper(tmpFile, func() error {
287
+		tmpFile.Close()
288
+		err := os.RemoveAll(tmpFile.Name())
289
+		if err != nil {
290
+			logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
291
+		}
292
+		return err
293
+	}), size, nil
282 294
 }
283 295
 
284 296
 func (ld *v2LayerDescriptor) Close() {