Skip to content

Commit

Permalink
fix: fix OCI format, GZIP file can be <1024 bytes (wagoodman#511)
Browse files Browse the repository at this point in the history
Fixes wagoodman#507
Fixes wagoodman#510
Fixes wagoodman#526
Fixes wagoodman#534

Co-authored-by: Maddog2050 <[email protected]>
  • Loading branch information
joschi and Maddog2050 committed Nov 7, 2024
1 parent 3c82597 commit c20ee8a
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions dive/image/docker/image_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c20ee8a

Please sign in to comment.