Skip to content

Commit

Permalink
Add import gzip and bzip2 support
Browse files Browse the repository at this point in the history
Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
  • Loading branch information
jokemanfire committed May 18, 2024
1 parent 24c2ae8 commit 83e4a72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
8 changes: 6 additions & 2 deletions core/images/archive/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ func WithImportCompression() ImportOpt {
// - existing OCI reference names are untouched
func ImportIndex(ctx context.Context, store content.Store, reader io.Reader, opts ...ImportOpt) (ocispec.Descriptor, error) {
var (
tr = tar.NewReader(reader)

ociLayout ocispec.ImageLayout
mfsts []struct {
Config string
Expand All @@ -82,6 +80,12 @@ func ImportIndex(ctx context.Context, store content.Store, reader io.Reader, opt
return ocispec.Descriptor{}, err
}
}
s, err := compression.DecompressStream(reader)
if err != nil {
return ocispec.Descriptor{}, fmt.Errorf("failed to detect the format of the import file: %w", err)
}

tr := tar.NewReader(s)

for {
hdr, err := tr.Next()
Expand Down
23 changes: 18 additions & 5 deletions pkg/archive/compression/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package compression
import (
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"context"
"encoding/binary"
Expand All @@ -45,6 +46,8 @@ const (
Gzip
// Zstd is zstd compression algorithm.
Zstd
// Bzip2 is bzip2 compression algorithm
Bzip2
)

const (
Expand Down Expand Up @@ -135,8 +138,9 @@ const (
)

var (
gzipMagic = []byte{0x1F, 0x8B, 0x08}
zstdMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
gzipMagic = []byte{0x1F, 0x8B, 0x08}
zstdMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
bzip2Magic = []byte{0x42, 0x5A, 0x68}
)

type matcher = func([]byte) bool
Expand Down Expand Up @@ -171,8 +175,9 @@ func zstdMatcher() matcher {
// DetectCompression detects the compression algorithm of the source.
func DetectCompression(source []byte) Compression {
for compression, fn := range map[Compression]matcher{
Gzip: magicNumberMatcher(gzipMagic),
Zstd: zstdMatcher(),
Gzip: magicNumberMatcher(gzipMagic),
Zstd: zstdMatcher(),
Bzip2: magicNumberMatcher(bzip2Magic),
} {
if fn(source) {
return compression
Expand Down Expand Up @@ -230,7 +235,15 @@ func DecompressStream(archive io.Reader) (DecompressReadCloser, error) {
return nil
},
}, nil

case Bzip2:
bzip2reader := bzip2.NewReader(buf)
return &readCloserWrapper{
Reader: bzip2reader,
compression: compression,
closer: func() error {
return nil
},
}, nil
default:
return nil, fmt.Errorf("unsupported compression format %s", (&compression).Extension())
}
Expand Down

0 comments on commit 83e4a72

Please sign in to comment.