From 7bc781d49cb2068e86977e62c9d9e8428ee54af7 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Wed, 6 Nov 2024 14:49:09 +0100 Subject: [PATCH] fix: fix OCI format, GZIP file can be <1024 bytes (wagoodman/dive#511) Fixes wagoodman/dive#507 Fixes wagoodman/dive#510 Fixes wagoodman/dive#526 Fixes wagoodman/dive#534 Co-authored-by: Maddog2050 <17902029+Maddog2050@users.noreply.github.com> --- dive/image/docker/image_archive.go | 37 ++++++++++++++---------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/dive/image/docker/image_archive.go b/dive/image/docker/image_archive.go index 827b58ba..f90c45c3 100644 --- a/dive/image/docker/image_archive.go +++ b/dive/image/docker/image_archive.go @@ -93,35 +93,32 @@ func NewImageArchive(tarFile io.ReadCloser) (*ImageArchive, error) { // never consume more bytes than this buffer contains so we can start again. // 512 bytes ought to be enough (as that's the size of a TAR entry header), - // but play it safe with 1024 bytes. This should also include very small layers - // (unless they've also been gzipped, but Docker does not appear to do it) + // but play it safe with 1024 bytes. This should also include very small layers. buffer := make([]byte, 1024) n, err := io.ReadFull(tarReader, buffer) if err != nil && err != io.ErrUnexpectedEOF { return img, err } - // Only try reading a TAR if file is "big enough" - if n == cap(buffer) { - var unwrappedReader io.Reader - unwrappedReader, err = gzip.NewReader(io.MultiReader(bytes.NewReader(buffer[:n]), tarReader)) - if err != nil { - // Not a gzipped entry - unwrappedReader = io.MultiReader(bytes.NewReader(buffer[:n]), tarReader) - } + // Try reading a GZIP + var unwrappedReader io.Reader + unwrappedReader, err = gzip.NewReader(io.MultiReader(bytes.NewReader(buffer[:n]), tarReader)) + if err != nil { + // Not a gzipped entry + unwrappedReader = io.MultiReader(bytes.NewReader(buffer[:n]), tarReader) + } - // Try reading a TAR - layerReader := tar.NewReader(unwrappedReader) - tree, err := processLayerTar(name, layerReader) - if err == nil { - currentLayer++ - // add the layer to the image - img.layerMap[tree.Name] = tree - continue - } + // Try reading a TAR + layerReader := tar.NewReader(unwrappedReader) + tree, err := processLayerTar(name, layerReader) + if err == nil { + currentLayer++ + // add the layer to the image + img.layerMap[tree.Name] = tree + continue } - // Not a TAR (or smaller than our buffer), might be a JSON file + // Not a TAR or GZIP, might be a JSON file decoder := json.NewDecoder(bytes.NewReader(buffer[:n])) token, err := decoder.Token() if _, ok := token.(json.Delim); err == nil && ok {