Skip to content

Commit

Permalink
Add v1util.EstargzReadCloser utility.
Browse files Browse the repository at this point in the history
This adds the first utility function we will need to start producing estargz layers.
  • Loading branch information
mattmoor committed Dec 16, 2020
1 parent 8b5370a commit 6f042a1
Show file tree
Hide file tree
Showing 40 changed files with 3,423 additions and 68 deletions.
4 changes: 2 additions & 2 deletions go.mod

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

12 changes: 10 additions & 2 deletions go.sum

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

25 changes: 25 additions & 0 deletions pkg/v1/v1util/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"

"github.com/containerd/stargz-snapshotter/estargz"
v1 "github.com/google/go-containerregistry/pkg/v1"
)

var gzipMagicHeader = []byte{'\x1f', '\x8b'}
Expand Down Expand Up @@ -80,6 +84,27 @@ func GunzipReadCloser(r io.ReadCloser) (io.ReadCloser, error) {
}, nil
}

// EstargzReadCloser reads uncompressed input data from the io.ReadCloser and
// returns an io.ReadCloser from which compressed data may be read.
// Refer to estargz for the options:
// https://pkg.go.dev/github.com/containerd/[email protected]/estargz#Option
func EstargzReadCloser(r io.ReadCloser, opts ...estargz.Option) (io.ReadCloser, v1.Hash, error) {
defer r.Close()

bs, err := ioutil.ReadAll(r)
if err != nil {
return nil, v1.Hash{}, err
}
br := bytes.NewReader(bs)

rc, d, err := estargz.Build(io.NewSectionReader(br, 0, int64(len(bs))), nil, opts...)
if err != nil {
return nil, v1.Hash{}, err
}
h, err := v1.NewHash(d.String())
return rc, h, err
}

// IsGzipped detects whether the input stream is compressed.
func IsGzipped(r io.Reader) (bool, error) {
magicHeader := make([]byte, 2)
Expand Down
61 changes: 61 additions & 0 deletions pkg/v1/v1util/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package v1util

import (
"archive/tar"
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
Expand All @@ -43,6 +45,65 @@ func TestReader(t *testing.T) {
}
}

func TestEstargzReader(t *testing.T) {
want := "This is the input string."
buf := bytes.NewBuffer(nil)
tw := tar.NewWriter(buf)

if err := tw.WriteHeader(&tar.Header{
Name: "foo",
Size: int64(len(want)),
}); err != nil {
t.Fatal("WriteHeader() =", err)
}
if _, err := tw.Write([]byte(want)); err != nil {
t.Fatal("tw.Write() =", err)
}
tw.Close()

zipped, _, err := EstargzReadCloser(ioutil.NopCloser(buf))
if err != nil {
t.Fatal("EstargzReadCloser() =", err)
}
unzipped, err := GunzipReadCloser(zipped)
if err != nil {
t.Error("GunzipReadCloser() =", err)
}
defer unzipped.Close()

found := false

r := tar.NewReader(unzipped)
for {
hdr, err := r.Next()
if err == io.EOF {
break
} else if err != nil {
t.Fatal("tar.Next() =", err)
}

if hdr.Name != "foo" {
continue
}
found = true

b, err := ioutil.ReadAll(r)
if err != nil {
t.Error("ReadAll() =", err)
}
if got := string(b); got != want {
t.Errorf("ReadAll(); got %q, want %q", got, want)
}
if err := unzipped.Close(); err != nil {
t.Error("Close() =", err)
}
}

if !found {
t.Error(`Did not find the expected file "foo"`)
}
}

func TestIsGzipped(t *testing.T) {
tests := []struct {
in []byte
Expand Down
202 changes: 202 additions & 0 deletions vendor/github.com/containerd/stargz-snapshotter/estargz/LICENSE

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

Loading

0 comments on commit 6f042a1

Please sign in to comment.