From f36672e87e520bc2b4a9f1e137d3a1c81cf1b34d Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Fri, 31 May 2024 13:50:49 +0400 Subject: [PATCH] Handle small gzipped layers These are encountered when analyzing python:3 container image. --- dive/image/docker/image_archive.go | 35 ++++++++++++++---------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/dive/image/docker/image_archive.go b/dive/image/docker/image_archive.go index 827b58ba..a602fc14 100644 --- a/dive/image/docker/image_archive.go +++ b/dive/image/docker/image_archive.go @@ -93,32 +93,29 @@ 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 gzipped entry + 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