Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete layout blob #936

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/v1/layout/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,19 @@ func (l Path) writeLayer(layer v1.Layer) error {
return l.WriteBlob(d, r)
}

// RemoveBlob removes a file from the blobs directory in the Path
// at blobs/{hash.Algorithm}/{hash.Hex}
// It does *not* remove any reference to it from other manifests or indexes, or
// from the root index.json.
func (l Path) RemoveBlob(hash v1.Hash) error {
dir := l.path("blobs", hash.Algorithm)
err := os.Remove(filepath.Join(dir, hash.Hex))
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}

// WriteImage writes an image, including its manifest, config and all of its
// layers, to the blobs directory. If any blob already exists, as determined by
// the hash filename, does not write it.
Expand Down
42 changes: 42 additions & 0 deletions pkg/v1/layout/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,45 @@ func TestReplaceImage(t *testing.T) {
t.Fatal("could not find digest3", digest3)
}
}

func TestRemoveBlob(t *testing.T) {
// need to set up a basic path
tmp, err := ioutil.TempDir("", "remove-blob-test")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(tmp)

var ii v1.ImageIndex
ii = empty.Index
l, err := Write(tmp, ii)
if err != nil {
t.Fatal(err)
}

// create a random blob
b := []byte("abcdefghijklmnop")
hash, _, err := v1.SHA256(bytes.NewReader(b))

if err := l.WriteBlob(hash, ioutil.NopCloser(bytes.NewReader(b))); err != nil {
t.Fatal(err)
}
// make sure it exists
b2, err := l.Bytes(hash)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, b2) {
t.Fatal("mismatched bytes")
}
// now the real test, delete it
if err := l.RemoveBlob(hash); err != nil {
t.Fatal(err)
}
// now it should not exist
b2, err = l.Bytes(hash)
if err == nil {
t.Fatal("still existed after deletion")
}
}