Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layerStore.Diff(): don't add an unnecessary buffer #1087

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/containers/storage/pkg/system"
"github.com/containers/storage/pkg/tarlog"
"github.com/containers/storage/pkg/truncindex"
multierror "github.com/hashicorp/go-multierror"
"github.com/klauspost/pgzip"
digest "github.com/opencontainers/go-digest"
"github.com/opencontainers/selinux/go-selinux/label"
Expand Down Expand Up @@ -1463,34 +1464,48 @@ func (r *layerStore) Diff(from, to string, options *DiffOptions) (io.ReadCloser,
}
return maybeCompressReadCloser(diff)
}
defer tsfile.Close()

decompressor, err := pgzip.NewReader(tsfile)
if err != nil {
return nil, err
}
defer decompressor.Close()

tsbytes, err := ioutil.ReadAll(decompressor)
if err != nil {
if e := tsfile.Close(); e != nil {
logrus.Debug(e)
}
return nil, err
}

metadata = storage.NewJSONUnpacker(bytes.NewBuffer(tsbytes))
metadata = storage.NewJSONUnpacker(decompressor)

fgetter, err := r.newFileGetter(to)
if err != nil {
return nil, err
errs := multierror.Append(nil, errors.Wrapf(err, "creating file-getter"))
if err := decompressor.Close(); err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "closing decompressor"))
}
if err := tsfile.Close(); err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "closing tarstream headers"))
}
return nil, errs.ErrorOrNil()
}

tarstream := asm.NewOutputTarStream(fgetter, metadata)
rc := ioutils.NewReadCloserWrapper(tarstream, func() error {
err1 := tarstream.Close()
err2 := fgetter.Close()
if err2 == nil {
return err1
var errs *multierror.Error
if err := decompressor.Close(); err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "closing decompressor"))
}
if err := tsfile.Close(); err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "closing tarstream headers"))
}
if err := tarstream.Close(); err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "closing reconstructed tarstream"))
}
if err := fgetter.Close(); err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "closing file-getter"))
}
if errs != nil {
return errs.ErrorOrNil()
}
return err2
return nil
})
return maybeCompressReadCloser(rc)
}
Expand Down
58 changes: 40 additions & 18 deletions tests/apply-junk.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,46 @@

load helpers

@test "applyjunk" {
function applyjunk_main() {
# Create and try to populate layers with... garbage. It should be
# rejected cleanly.
for compressed in cat gzip bzip2 xz zstd ; do
storage create-layer --id layer-${compressed}

echo [[${compressed} /etc/os-release]]
${compressed} < /etc/os-release > junkfile
run storage apply-diff --file junkfile layer-${compressed}
echo "$output"
[[ "$status" -ne 0 ]]
[[ "$output" =~ "invalid tar header" ]] || [[ "$output" =~ "unexpected EOF" ]]

echo [[${compressed}]]
echo "sorry, not even enough info for a tar header here" | ${compressed} > junkfile
run storage apply-diff --file junkfile layer-${compressed}
echo "$output"
[[ "$status" -ne 0 ]]
[[ "$output" =~ "unexpected EOF" ]]
done
compressed="$1"

storage create-layer --id layer-${compressed}

echo [[${compressed} /etc/os-release]]
if ! ${compressed} < /etc/os-release > junkfile ; then
skip "error running ${compressed}"
fi
run storage apply-diff --file junkfile layer-${compressed}
echo "$output"
[[ "$status" -ne 0 ]]
[[ "$output" =~ "invalid tar header" ]] || [[ "$output" =~ "unexpected EOF" ]]

echo [[${compressed}]]
echo "sorry, not even enough info for a tar header here" | ${compressed} > junkfile
run storage apply-diff --file junkfile layer-${compressed}
echo "$output"
[[ "$status" -ne 0 ]]
[[ "$output" =~ "unexpected EOF" ]]
}

@test "applyjunk-uncompressed" {
applyjunk_main cat
}

@test "applyjunk-gzip" {
applyjunk_main gzip
}

@test "applyjunk-bzip2" {
applyjunk_main bzip2
}

@test "applyjunk-xz" {
applyjunk_main xz
}

@test "applyjunk-zstd" {
applyjunk_main zstd
}