Skip to content

Commit

Permalink
copy: do not ignore errors on Close
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jul 2, 2021
1 parent 67a4c99 commit 3250f2d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,20 +1653,26 @@ func (c *copier) copyBlobFromStream(ctx context.Context, srcStream io.Reader, sr
return uploadedInfo, nil
}

// compressGoroutine reads all input from src and writes its compressed equivalent to dest.
func (c *copier) compressGoroutine(dest *io.PipeWriter, src io.Reader, metadata map[string]string, compressionFormat compression.Algorithm) {
err := errors.New("Internal error: unexpected panic in compressGoroutine")
defer func() { // Note that this is not the same as {defer dest.CloseWithError(err)}; we need err to be evaluated lazily.
_ = dest.CloseWithError(err) // CloseWithError(nil) is equivalent to Close(), always returns nil
}()

compressor, err := compression.CompressStreamWithMetadata(dest, metadata, compressionFormat, c.compressionLevel)
// doCompression reads all input from src and writes its compressed equivalent to dest.
func doCompression(dest io.Writer, src io.Reader, metadata map[string]string, compressionFormat compression.Algorithm, compressionLevel *int) error {
compressor, err := compression.CompressStreamWithMetadata(dest, metadata, compressionFormat, compressionLevel)
if err != nil {
return
return err
}
defer compressor.Close()

buf := make([]byte, compressionBufferSize)

_, err = io.CopyBuffer(compressor, src, buf) // Sets err to nil, i.e. causes dest.Close()
if err != nil {
compressor.Close()
return err
}

return compressor.Close()
}

// compressGoroutine reads all input from src and writes its compressed equivalent to dest.
func (c *copier) compressGoroutine(dest *io.PipeWriter, src io.Reader, metadata map[string]string, compressionFormat compression.Algorithm) {
err := doCompression(dest, src, metadata, compressionFormat, c.compressionLevel)
_ = dest.CloseWithError(err) // CloseWithError(nil) is equivalent to Close(), always returns nil
}

0 comments on commit 3250f2d

Please sign in to comment.