Skip to content

Commit

Permalink
store: lock additional store before validating image
Browse files Browse the repository at this point in the history
* We must lock store before doing any operation if its not the default
  imagestore.
* Refactor and address some comments from: #1578

Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed May 5, 2023
1 parent 5eb8586 commit 681c3d7
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 681c3d7

Please sign in to comment.