cuelabs.dev/go/oci/ociregistry@v0.0.0-20240906074133-82eb438dd565/ocimem/deleter.go (about) 1 // Copyright 2023 CUE Labs AG 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ocimem 16 17 import ( 18 "context" 19 "fmt" 20 21 "cuelabs.dev/go/oci/ociregistry" 22 ) 23 24 var ( 25 errCannotDeleteTag = fmt.Errorf("%w: tag deletion not permitted", ociregistry.ErrDenied) 26 errCannotDeleteTaggedBlob = fmt.Errorf("%w: deletion of tagged blob not permitted", ociregistry.ErrDenied) 27 errCannotDeleteTaggedManifest = fmt.Errorf("%w: deletion of tagged manifest not permitted", ociregistry.ErrDenied) 28 ) 29 30 func (r *Registry) DeleteBlob(ctx context.Context, repoName string, digest ociregistry.Digest) error { 31 r.mu.Lock() 32 defer r.mu.Unlock() 33 if _, err := r.blobForDigest(repoName, digest); err != nil { 34 return err 35 } 36 repo, ok := r.repos[repoName] 37 if !ok { 38 return nil 39 } 40 if r.cfg.ImmutableTags { 41 ok, err := refersTo(repo, repoTagIter(repo), digest) 42 if err != nil { 43 return err 44 } 45 if ok { 46 return errCannotDeleteTaggedBlob 47 } 48 } 49 // TODO if r.cfg.ImmutableTags, refuse to delete the blob 50 // if it's referred to, directly or indirectly, by a tag. 51 delete(r.repos[repoName].blobs, digest) 52 return nil 53 } 54 55 func (r *Registry) DeleteManifest(ctx context.Context, repoName string, digest ociregistry.Digest) error { 56 r.mu.Lock() 57 defer r.mu.Unlock() 58 if _, err := r.manifestForDigest(repoName, digest); err != nil { 59 return err 60 } 61 repo := r.repos[repoName] 62 if r.cfg.ImmutableTags { 63 ok, err := refersTo(repo, repoTagIter(repo), digest) 64 if err != nil { 65 return err 66 } 67 if ok { 68 return errCannotDeleteTaggedManifest 69 } 70 } 71 // TODO should this also delete any tags referring to this digest? 72 delete(repo.manifests, digest) 73 return nil 74 } 75 76 func (r *Registry) DeleteTag(ctx context.Context, repoName string, tagName string) error { 77 r.mu.Lock() 78 defer r.mu.Unlock() 79 repo, err := r.repo(repoName) 80 if err != nil { 81 return err 82 } 83 if _, ok := repo.tags[tagName]; !ok { 84 return fmt.Errorf("%w: tag does not exist", ociregistry.ErrManifestUnknown) 85 } 86 if r.cfg.ImmutableTags { 87 return errCannotDeleteTag 88 } 89 delete(repo.tags, tagName) 90 return nil 91 }