Skip to content

Commit

Permalink
Merge pull request moby#21676 from aaronlehmann/tmpfile-close
Browse files Browse the repository at this point in the history
Pull: only close temporary file once
  • Loading branch information
vdemeester committed Mar 31, 2016
2 parents 60821fe + 930ae3d commit 3d4b3cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 14 additions & 1 deletion distribution/pull_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,20 @@ func (ld *v1LayerDescriptor) Download(ctx context.Context, progressOutput progre
logrus.Debugf("Downloaded %s to tempfile %s", ld.ID(), ld.tmpFile.Name())

ld.tmpFile.Seek(0, 0)
return ld.tmpFile, ld.layerSize, nil

// hand off the temporary file to the download manager, so it will only
// be closed once
tmpFile := ld.tmpFile
ld.tmpFile = nil

return ioutils.NewReadCloserWrapper(tmpFile, func() error {
tmpFile.Close()
err := os.RemoveAll(tmpFile.Name())
if err != nil {
logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
}
return err
}), ld.layerSize, nil
}

func (ld *v1LayerDescriptor) Close() {
Expand Down
14 changes: 13 additions & 1 deletion distribution/pull_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,19 @@ func (ld *v2LayerDescriptor) Download(ctx context.Context, progressOutput progre
ld.verifier = nil
return nil, 0, xfer.DoNotRetry{Err: err}
}
return tmpFile, size, nil

// hand off the temporary file to the download manager, so it will only
// be closed once
ld.tmpFile = nil

return ioutils.NewReadCloserWrapper(tmpFile, func() error {
tmpFile.Close()
err := os.RemoveAll(tmpFile.Name())
if err != nil {
logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
}
return err
}), size, nil
}

func (ld *v2LayerDescriptor) Close() {
Expand Down

0 comments on commit 3d4b3cb

Please sign in to comment.