From 681c3d72e99189be1126c41e4cd277b25e322ddb Mon Sep 17 00:00:00 2001 From: Aditya R Date: Wed, 3 May 2023 18:05:42 +0530 Subject: [PATCH] store: lock additional store before validating image * We must lock store before doing any operation if its not the default imagestore. * Refactor and address some comments from: https://github.com/containers/storage/pull/1578 Signed-off-by: Aditya R --- store.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/store.go b/store.go index 79e36cf6c4..a4dd215c2f 100644 --- a/store.go +++ b/store.go @@ -2470,28 +2470,22 @@ func (s *store) DeleteLayer(id string) error { func (s *store) DeleteImage(id string, commit bool) (layers []string, err error) { layersToRemove := []string{} if err := s.writeToAllStores(func(rlstore rwLayerStore) error { - // Perform delete image on primary imageStore but if image - // is not found on primary store then try delete on any additional - // write-able image store. - imageStores := []rwImageStore{s.imageStore} - imageStores = append(imageStores, s.rwImageStores...) + // Delete image from all available imagestores configured to be used. imageFound := false - primaryImageStore := true - for _, imageStore := range imageStores { - if !imageStore.Exists(id) { - primaryImageStore = false - continue - } - imageFound = true - if !primaryImageStore { + for _, is := range append([]rwImageStore{s.imageStore}, s.rwImageStores...) { + if is != s.imageStore { // This is an additional writeable image store // so we must perform lock - if err := imageStore.startWriting(); err != nil { + if err := is.startWriting(); err != nil { return err } - defer imageStore.stopWriting() + defer is.stopWriting() } - image, err := imageStore.Get(id) + if !is.Exists(id) { + continue + } + imageFound = true + image, err := is.Get(id) if err != nil { return err } @@ -2507,7 +2501,7 @@ func (s *store) DeleteImage(id string, commit bool) (layers []string, err error) if container, ok := aContainerByImage[id]; ok { return fmt.Errorf("image used by %v: %w", container, ErrImageUsedByContainer) } - images, err := imageStore.Images() + images, err := is.Images() if err != nil { return err } @@ -2529,7 +2523,7 @@ func (s *store) DeleteImage(id string, commit bool) (layers []string, err error) } } if commit { - if err = imageStore.Delete(id); err != nil { + if err = is.Delete(id); err != nil { return err } }