Skip to content

Commit

Permalink
Added support for zstd compression
Browse files Browse the repository at this point in the history
Using github.com/klauspost/compress go module has brought in a
list of other compression implementations. Therefore the vendor
folder has gotten larger. Some files were also modified due to
the bump up in compress's version.

Apart from these, this commit adds an implementation for zstd
compression and a corresponding test.

Fixes opencontainers#316

Signed-off-by: Nisha K <nishak@vmware.com>
  • Loading branch information
Nisha K authored and mateuszkwiatkowski committed Feb 4, 2021
1 parent 9574d71 commit 6c6008e
Show file tree
Hide file tree
Showing 77 changed files with 17,607 additions and 319 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/docker/go-units v0.4.0
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.5.0 // indirect
github.com/klauspost/compress v1.10.9 // indirect
github.com/klauspost/compress v1.11.3
github.com/klauspost/pgzip v1.2.4
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
github.com/klauspost/compress v1.10.9 h1:pPRt1Z78crspaHISkpSSHjDlx+Tt9suHe519dsI0vF4=
github.com/klauspost/compress v1.10.9/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.3 h1:dB4Bn0tN3wdCzQxnS8r06kV74qN/TAfaIS0bVE8h3jc=
github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/pgzip v1.2.4 h1:TQ7CNpYKovDOmqzRHKxJh0BeaBI7UdQZYc6p7pMQh1A=
github.com/klauspost/pgzip v1.2.4/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
35 changes: 35 additions & 0 deletions mutate/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"runtime"

zstd "github.com/klauspost/compress/zstd"
gzip "github.com/klauspost/pgzip"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -67,3 +68,37 @@ func (gz gzipCompressor) Compress(reader io.Reader) (io.ReadCloser, error) {
func (gz gzipCompressor) MediaTypeSuffix() string {
return "gzip"
}

// ZstdCompressor provides zstd compression.
var ZstdCompressor Compressor = zstdCompressor{}

type zstdCompressor struct{}

func (zs zstdCompressor) Compress(reader io.Reader) (io.ReadCloser, error) {

pipeReader, pipeWriter := io.Pipe()
zenc, err := zstd.NewWriter(pipeWriter)
if err != nil {
return nil, err
}
go func() {
if _, err := io.Copy(zenc, reader); err != nil {
// #nosec G104
_ = pipeWriter.CloseWithError(errors.Wrap(err, "compressing layer"))
}
if err := zenc.Close(); err != nil {
// #nosec G104
_ = pipeWriter.CloseWithError(errors.Wrap(err, "close gzip writer"))
}
if err := pipeWriter.Close(); err != nil {
// #nosec G104
_ = pipeWriter.CloseWithError(errors.Wrap(err, "close pipe writer"))
}
}()

return pipeReader, nil
}

func (zs zstdCompressor) MediaTypeSuffix() string {
return "zstd"
}
21 changes: 21 additions & 0 deletions mutate/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package mutate

import (
"bytes"
"io"
"io/ioutil"
"testing"

zstd "github.com/klauspost/compress/zstd"
gzip "github.com/klauspost/pgzip"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -45,3 +47,22 @@ func TestGzipCompressor(t *testing.T) {

assert.Equal(string(content), fact)
}

func TestZstdCompressor(t *testing.T) {
assert := assert.New(t)

buf := bytes.NewBufferString(fact)
c := ZstdCompressor

r, err := c.Compress(buf)
assert.NoError(err)
assert.Equal(c.MediaTypeSuffix(), "zstd")

dec, err := zstd.NewReader(r)
assert.NoError(err)

var content bytes.Buffer
_, err = io.Copy(&content, dec)
assert.NoError(err)
assert.Equal(content.String(), fact)
}
10 changes: 5 additions & 5 deletions vendor/github.com/klauspost/compress/flate/fast_encoder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 78 additions & 58 deletions vendor/github.com/klauspost/compress/flate/gen_inflate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6c6008e

Please sign in to comment.