Skip to content

Commit

Permalink
add eof check
Browse files Browse the repository at this point in the history
  • Loading branch information
joeybrown-sf committed Jul 15, 2024
1 parent 67a8cf5 commit 1b5aa7c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cache/image_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,39 @@ func (c *ImageCache) AddLayerFile(tarPath string, diffID string) error {
return c.newImage.AddLayerWithDiffID(tarPath, diffID)
}

// isLayerNotFound checks if the error is a layer not found error
//
// FIXME: we should not have to rely on trapping ErrUnexpectedEOF.
// If a blob is not present in the registry, we should get imgutil.ErrLayerNotFound,
// but we do not and instead get io.ErrUnexpectedEOF
func isLayerNotFound(err error) bool {
var e imgutil.ErrLayerNotFound
return errors.As(err, &e) || errors.Is(err, io.ErrUnexpectedEOF)
}

func (c *ImageCache) ReuseLayer(diffID string) error {
if c.committed {
return errCacheCommitted
}
err := c.origImage.ReuseLayer(diffID)
if err != nil {
if IsLayerNotFound(err) {
// FIXME: this path is not currently executed.
// If a blob is not present in the registry, we should get imgutil.ErrLayerNotFound.
// We should then skip attempting to reuse the layer.
// However, we do not get imgutil.ErrLayerNotFound when the blob is not present.
if isLayerNotFound(err) {
return NewReadErr(fmt.Sprintf("failed to find cache layer with SHA '%s'", diffID))
}
return fmt.Errorf("failed to reuse cache layer with SHA '%s'", diffID)
}
return nil
}

// IsLayerNotFound checks if the error is a layer not found error
func IsLayerNotFound(err error) bool {
var e imgutil.ErrLayerNotFound
return errors.As(err, &e)
}

// RetrieveLayer retrieves a layer from the cache
func (c *ImageCache) RetrieveLayer(diffID string) (io.ReadCloser, error) {
closer, err := c.origImage.GetLayer(diffID)
if err != nil {
if IsLayerNotFound(err) {
if isLayerNotFound(err) {
return nil, NewReadErr(fmt.Sprintf("failed to find cache layer with SHA '%s'", diffID))
}
return nil, fmt.Errorf("failed to get cache layer with SHA '%s'", diffID)
Expand Down

0 comments on commit 1b5aa7c

Please sign in to comment.