Skip to content

Commit

Permalink
feat: support deletion for OCI storage (#608)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>

Part 3/4 of #454

Based on draft PR #582

---------

Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Oct 7, 2023
1 parent 6456d16 commit 7eef77e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions content/oci/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ func (s *Storage) Push(_ context.Context, expected ocispec.Descriptor, content i
return nil
}

// Delete removes the target from the system.
func (s *Storage) Delete(ctx context.Context, target ocispec.Descriptor) error {
path, err := blobPath(target.Digest)
if err != nil {
return fmt.Errorf("%s: %s: %w", target.Digest, target.MediaType, errdef.ErrInvalidDigest)
}
targetPath := filepath.Join(s.root, path)
return os.Remove(targetPath)
}

// ingest write the content into a temporary ingest file.
func (s *Storage) ingest(expected ocispec.Descriptor, content io.Reader) (path string, ingestErr error) {
if err := ensureDir(s.ingestRoot); err != nil {
Expand Down
36 changes: 36 additions & 0 deletions content/oci/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,39 @@ func TestStorage_Fetch_Concurrent(t *testing.T) {
t.Fatal(err)
}
}

func TestStorage_Delete(t *testing.T) {
content := []byte("test delete")
desc := ocispec.Descriptor{
MediaType: "test",
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}
tempDir := t.TempDir()
s, err := NewStorage(tempDir)
if err != nil {
t.Fatal("New() error =", err)
}
ctx := context.Background()
if err := s.Push(ctx, desc, bytes.NewReader(content)); err != nil {
t.Fatal("Storage.Push() error =", err)
}
exists, err := s.Exists(ctx, desc)
if err != nil {
t.Fatal("Storage.Exists() error =", err)
}
if !exists {
t.Errorf("Storage.Exists() = %v, want %v", exists, true)
}
err = s.Delete(ctx, desc)
if err != nil {
t.Fatal("Storage.Delete() error =", err)
}
exists, err = s.Exists(ctx, desc)
if err != nil {
t.Fatal("Storage.Exists() error =", err)
}
if exists {
t.Errorf("Storage.Exists() = %v, want %v", exists, false)
}
}

0 comments on commit 7eef77e

Please sign in to comment.