Debian Codegraph @

util/testutil/compression.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package testutil
18
19 import (
20 "bytes"
21 "io"
22
23 "github.com/containerd/stargz-snapshotter/estargz"
24 esgzexternaltoc "github.com/containerd/stargz-snapshotter/estargz/externaltoc"
25 "github.com/containerd/stargz-snapshotter/estargz/zstdchunked"
26 "github.com/klauspost/compress/zstd"
27 )
28
29 type Compression interface {
30 estargz.Compressor
31 estargz.Decompressor
32
33 // DecompressTOC decompresses the passed blob and returns a reader of TOC JSON.
34 // This is needed to be used from metadata pkg
35 DecompressTOC(io.Reader) (tocJSON io.ReadCloser, err error)
36 }
37
38 type CompressionFactory func() Compression
39
40 type zstdCompression struct {
41 *zstdchunked.Compressor
42 *zstdchunked.Decompressor
43 }
44
45 func ZstdCompressionWithLevel(compressionLevel zstd.EncoderLevel) CompressionFactory {
46 return func() Compression {
47 return &zstdCompression{&zstdchunked.Compressor{CompressionLevel: compressionLevel}, &zstdchunked.Decompressor{}}
48 }
49 }
50
51 type gzipCompression struct {
52 *estargz.GzipCompressor
53 *estargz.GzipDecompressor
54 }
55
56 func GzipCompressionWithLevel(compressionLevel int) CompressionFactory {
57 return func() Compression {
58 return gzipCompression{estargz.NewGzipCompressorWithLevel(compressionLevel), &estargz.GzipDecompressor{}}
59 }
60 }
61
62 type externalTOCGzipCompression struct {
63 *esgzexternaltoc.GzipCompressor
64 *esgzexternaltoc.GzipDecompressor
65 }
66
67 func ExternalTOCGzipCompressionWithLevel(compressionLevel int) CompressionFactory {
68 return func() Compression {
69 compressor := esgzexternaltoc.NewGzipCompressorWithLevel(compressionLevel)
70 decompressor := esgzexternaltoc.NewGzipDecompressor(func() ([]byte, error) {
71 buf := new(bytes.Buffer)
72 if _, err := compressor.WriteTOCTo(buf); err != nil {
73 return nil, err
74 }
75 return buf.Bytes(), nil
76 })
77 return &externalTOCGzipCompression{compressor, decompressor}
78 }
79
80 }