Skip to content

Commit

Permalink
fix removal of temp file in GetBlob on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Norgate <[email protected]>
  • Loading branch information
Mike Norgate committed Sep 5, 2023
1 parent 0140e69 commit 62835fe
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion storage/storage_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/containers/storage/pkg/archive"
"github.com/containers/storage/pkg/ioutils"
digest "github.com/opencontainers/go-digest"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -129,15 +130,20 @@ func (s *storageImageSource) GetBlob(ctx context.Context, info types.BlobInfo, c
return nil, 0, err
}
success := false
tmpFileRemovePending := true
defer func() {
if !success {
tmpFile.Close()
if tmpFileRemovePending {
os.Remove(tmpFile.Name())
}
}
}()
// On Unix and modern Windows (2022 at least) we can eagerly unlink the file to ensure it's automatically
// cleaned up on process termination (or if the caller forgets to invoke Close())
// On older versions of Windows we will have to fallback to relying on the caller to invoke Close()
if err := os.Remove(tmpFile.Name()); err != nil {
return nil, 0, err
tmpFileRemovePending = false
}

if _, err := io.Copy(tmpFile, rc); err != nil {
Expand All @@ -148,6 +154,14 @@ func (s *storageImageSource) GetBlob(ctx context.Context, info types.BlobInfo, c
}

success = true

if tmpFileRemovePending {
return ioutils.NewReadCloserWrapper(tmpFile, func() error {
tmpFile.Close()
return os.Remove(tmpFile.Name())
}), n, nil
}

return tmpFile, n, nil
}

Expand Down

0 comments on commit 62835fe

Please sign in to comment.