Skip to content

Commit

Permalink
chunked: use a limitWriter instead of LimitReader
Browse files Browse the repository at this point in the history
so we can use the WriteTo interface offered by the zstd library.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Dec 24, 2021
1 parent e4b2716 commit 45e6852
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions pkg/chunked/storage_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,20 +785,43 @@ func (c *chunkedDiffer) prepareCompressedStreamToFile(compression compressedFile
return nil
}

func limitWriter(r io.Writer, n int64) io.Writer { return &LimitedWriter{r, n} }

type LimitedWriter struct {
W io.Writer // underlying writer
N int64 // max bytes remaining
}

func (l *LimitedWriter) Write(p []byte) (n int, err error) {
if l.N <= 0 {
return len(p), nil
}
pp := p
if int64(len(p)) > l.N {
pp = p[0:l.N]
}
n, err = l.W.Write(pp)
l.N -= int64(n)
if l.N == 0 {
return len(p), nil
}
return
}

func (c *chunkedDiffer) appendCompressedStreamToFile(compression compressedFileType, destFile *destinationFile, reader io.Reader, size int64) (err error) {
l := limitWriter(destFile.to, size)
switch compression {
case fileTypeZstdChunked:
defer c.zstdReader.Reset(nil)
if _, err := io.CopyBuffer(destFile.to, io.LimitReader(c.zstdReader, size), c.copyBuffer); err != nil {
return err
}

_, err := c.zstdReader.WriteTo(l)
return err
case fileTypeEstargz:
if _, err := io.CopyBuffer(destFile.to, io.LimitReader(c.gzipReader, size), c.copyBuffer); err != nil {
return err
}
defer c.gzipReader.Close()
_, err := c.gzipReader.WriteTo(l)
return err
case fileTypeNoCompression:
_, err := io.CopyBuffer(destFile.to, io.LimitReader(reader, size), c.copyBuffer)
_, err := io.CopyBuffer(l, reader, c.copyBuffer)
if err != nil {
return err
}
Expand Down

0 comments on commit 45e6852

Please sign in to comment.