Skip to content

Commit

Permalink
Small changes after code review
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Vlérick <[email protected]>
  • Loading branch information
Pvlerick committed Sep 28, 2023
1 parent a53807a commit c939c10
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f/go.mod h1:J6OG
github.com/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY=
github.com/otiai10/copy v1.12.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
26 changes: 13 additions & 13 deletions oci/layout/oci_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (ref ociReference) getIndex() (*imgspecv1.Index, error) {
return parseIndex(ref.indexPath())
}

func parseJson[T any](path string) (*T, error) {
func parseJSON[T any](path string) (*T, error) {
content, err := os.Open(path)
if err != nil {
return nil, err
Expand All @@ -182,7 +182,7 @@ func parseJson[T any](path string) (*T, error) {
}

func parseIndex(path string) (*imgspecv1.Index, error) {
return parseJson[imgspecv1.Index](path)
return parseJSON[imgspecv1.Index](path)
}

func (ref ociReference) getManifestDescriptor() (imgspecv1.Descriptor, error) {
Expand Down Expand Up @@ -269,7 +269,7 @@ func (ref ociReference) getManifest(descriptor *imgspecv1.Descriptor) (*imgspecv
return nil, err
}

manifest, err := parseJson[imgspecv1.Manifest](manifestPath)
manifest, err := parseJSON[imgspecv1.Manifest](manifestPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -351,23 +351,23 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex
}

// Delete all blobs used by this image only
blobsToDelete := make([]digest.Digest, 0, len(manifest.Layers)+2)
blobsToDelete := set.New[digest.Digest]()
for _, descriptor := range append(manifest.Layers, manifest.Config, *imageDescriptorWrapper.descriptor) {
if !blobsUsedByOtherImages.Contains(descriptor.Digest) {
blobsToDelete = append(blobsToDelete, descriptor.Digest)
blobsToDelete.Add(descriptor.Digest)
} else {
logrus.Debug("Blob ", descriptor.Digest.Hex(), " is used by another image, leaving it")
}
}
for _, digest := range blobsToDelete {
for _, digest := range blobsToDelete.Values() {
//TODO Check if there is shared blob path?
blobPath, err := ref.blobPath(digest, "")
if err != nil {
return err
}
logrus.Debug("Deleting blob ", digest.Hex())
err = os.Remove(blobPath)
if err != nil {
if err != nil && !os.IsNotExist(err) {
return err
}
}
Expand Down Expand Up @@ -402,7 +402,7 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex
// New index is ready, it has to be saved to disk now
// ... if it is the root index, it's easy, just overwrite it
if indexPath == ref.indexPath() {
return saveJson(ref.indexPath(), index)
return saveJSON(ref.indexPath(), index)
} else {
indexDigest, err := digest.Parse("sha256:" + filepath.Base(indexPath))
if err != nil {
Expand All @@ -418,12 +418,12 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex
if err != nil {
return err
}
indexNewDigest := digest.SHA256.FromBytes(buffer.Bytes())
indexNewDigest := digest.Canonical.FromBytes(buffer.Bytes())
indexNewPath, err := ref.blobPath(indexNewDigest, "")
if err != nil {
return err
}
err = saveJson(indexNewPath, index)
err = saveJSON(indexNewPath, index)
if err != nil {
return err
}
Expand All @@ -440,18 +440,18 @@ func (ref ociReference) DeleteImage(ctx context.Context, sys *types.SystemContex
return nil
}

func saveJson(path string, content any) error {
func saveJSON(path string, content any) error {
// If the file already exists, get its mode to preserve it
var mode fs.FileMode
existinfFileInfo, err := os.Stat(path)
existingfi, err := os.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
return err
} else { // File does not exist, use default mode
mode = 0644
}
} else {
mode = existinfFileInfo.Mode()
mode = existingfi.Mode()
}

// Then write the file
Expand Down
10 changes: 3 additions & 7 deletions oci/layout/oci_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package layout
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -341,7 +340,7 @@ func TestReferenceDeleteImage(t *testing.T) {

// Check that all blobs were deleted
blobsDir := filepath.Join(tmpDir, "blobs")
files, err := ioutil.ReadDir(filepath.Join(blobsDir, "sha256"))
files, err := os.ReadDir(filepath.Join(blobsDir, "sha256"))
require.NoError(t, err)
require.Empty(t, files)

Expand All @@ -364,7 +363,7 @@ func TestReferenceDeleteImage_emptyImageName(t *testing.T) {

// Check that all blobs were deleted
blobsDir := filepath.Join(tmpDir, "blobs")
files, err := ioutil.ReadDir(filepath.Join(blobsDir, "sha256"))
files, err := os.ReadDir(filepath.Join(blobsDir, "sha256"))
require.NoError(t, err)
require.Empty(t, files)

Expand Down Expand Up @@ -463,10 +462,7 @@ func TestReferenceDeleteImage_someBlobsAreUsedByOtherImages(t *testing.T) {
otherImagesStillPresent = append(otherImagesStillPresent, true)
}
}
require.Equal(t, 2, len(otherImagesStillPresent))
for _, v := range otherImagesStillPresent {
require.True(t, v)
}
assert.Equal(t, []bool{true, true}, otherImagesStillPresent)
}

func TestReferenceDeleteImage_inNestedIndex(t *testing.T) {
Expand Down

0 comments on commit c939c10

Please sign in to comment.