diff --git a/libimage/image.go b/libimage/image.go index 3897d2f00..da4ff8b7a 100644 --- a/libimage/image.go +++ b/libimage/image.go @@ -402,7 +402,7 @@ func (i *Image) removeRecursive(ctx context.Context, rmMap map[string]*RemoveIma // have a closer look at the errors. On top, image removal should be // tolerant toward corrupted images. handleError := func(err error) error { - if errors.Is(err, storage.ErrImageUnknown) || errors.Is(err, storage.ErrNotAnImage) || errors.Is(err, storage.ErrLayerUnknown) { + if ErrorIsImageUnknown(err) { // The image or layers of the image may already have been removed // in which case we consider the image to be removed. return nil diff --git a/libimage/layer_tree.go b/libimage/layer_tree.go index c9de814a7..e6b012f90 100644 --- a/libimage/layer_tree.go +++ b/libimage/layer_tree.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/containers/storage" + storageTypes "github.com/containers/storage/types" ociv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sirupsen/logrus" ) @@ -31,10 +32,19 @@ func (t *layerTree) node(layerID string) *layerNode { return node } +// ErrorIsImageUnknown returns true if the specified error indicates that an +// image is unknown or has been partially removed (e.g., a missing layer). +func ErrorIsImageUnknown(err error) bool { + return errors.Is(err, storage.ErrImageUnknown) || + errors.Is(err, storageTypes.ErrLayerUnknown) || + errors.Is(err, storageTypes.ErrSizeUnknown) || + errors.Is(err, storage.ErrNotAnImage) +} + // toOCI returns an OCI image for the specified image. // // WARNING: callers are responsible for handling cases where the target image -// has been removed and need to check for `storage.ErrImageUnknown`. +// has been (partially) removed and can use `ErrorIsImageUnknown` to detect it. func (t *layerTree) toOCI(ctx context.Context, i *Image) (*ociv1.Image, error) { var err error oci, exists := t.ociCache[i.ID()] @@ -159,7 +169,7 @@ func (t *layerTree) children(ctx context.Context, parent *Image, all bool) ([]*I parentID := parent.ID() parentOCI, err := t.toOCI(ctx, parent) if err != nil { - if errors.Is(err, storage.ErrImageUnknown) { + if ErrorIsImageUnknown(err) { return nil, nil } return nil, err @@ -172,7 +182,7 @@ func (t *layerTree) children(ctx context.Context, parent *Image, all bool) ([]*I } childOCI, err := t.toOCI(ctx, child) if err != nil { - if errors.Is(err, storage.ErrImageUnknown) { + if ErrorIsImageUnknown(err) { return false, nil } return false, err @@ -265,7 +275,7 @@ func (t *layerTree) parent(ctx context.Context, child *Image) (*Image, error) { childID := child.ID() childOCI, err := t.toOCI(ctx, child) if err != nil { - if errors.Is(err, storage.ErrImageUnknown) { + if ErrorIsImageUnknown(err) { return nil, nil } return nil, err @@ -281,7 +291,7 @@ func (t *layerTree) parent(ctx context.Context, child *Image) (*Image, error) { } emptyOCI, err := t.toOCI(ctx, empty) if err != nil { - if errors.Is(err, storage.ErrImageUnknown) { + if ErrorIsImageUnknown(err) { return nil, nil } return nil, err @@ -316,7 +326,7 @@ func (t *layerTree) parent(ctx context.Context, child *Image) (*Image, error) { } parentOCI, err := t.toOCI(ctx, parent) if err != nil { - if errors.Is(err, storage.ErrImageUnknown) { + if ErrorIsImageUnknown(err) { return nil, nil } return nil, err