Repository: google/go-containerregistry
Pull request #1810 (2023-10-11 20:32:08)

fix: mimic oci-layout in diskblobhandler
this change makes blob disk implementation mimic oci-layout for storing blobs.  This will speed up the pushing of 
oci-layout images on disk to the registry by providing the oci-layout image as the storage directory. 

Cross-ref: https://github.com/bazel-contrib/rules_oci/issues/365

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/google/go-containerregistry/pull/1810?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#1810](https://app.codecov.io/gh/google/go-containerregistry/pull/1810?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (9f997a9) into [main](https://app.codecov.io/gh/google/go-containerregistry/commit/dbcd01c402b2f05bcf6fb988014c5f37e9b13559?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (dbcd01c) will **increase** coverage by `0.04%`.
> The diff coverage is `44.44%`.

```diff
@@            Coverage Diff             @@
##             main    #1810      +/-   ##
==========================================
+ Coverage   71.88%   71.93%   +0.04%     
==========================================
  Files         122      122              
  Lines        9832     9837       +5     
==========================================
+ Hits         7068     7076       +8     
+ Misses       2081     2079       -2     
+ Partials      683      682       -1     
```


| [Files](https://app.codecov.io/gh/google/go-containerregistry/pull/1810?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [pkg/registry/blobs\_disk.go](https://app.codecov.io/gh/google/go-containerregistry/pull/1810?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-cGtnL3JlZ2lzdHJ5L2Jsb2JzX2Rpc2suZ28=) | `48.48% <44.44%> (-1.52%)` | :arrow_down: |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/google/go-containerregistry/pull/1810/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)


thesayyn: cc @imjasonh @jonjohnsonjr 
thesayyn: @imjasonh @jonjohnsonjr friendly ping...

Patch:
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index bb62d6e0..a5911bf9 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -15,7 +15,7 @@ jobs:
         run: git fetch --prune --unshallow
       - uses: actions/setup-go@v4
         with:
-          go-version: 1.18
+          go-version: 1.21
           check-latest: true
       - uses: goreleaser/goreleaser-action@v4.2.0
         id: run-goreleaser
diff --git a/pkg/registry/blobs_disk.go b/pkg/registry/blobs_disk.go
index dc86bec3..361390f0 100644
--- a/pkg/registry/blobs_disk.go
+++ b/pkg/registry/blobs_disk.go
@@ -30,8 +30,12 @@ type diskHandler struct {
 
 func NewDiskBlobHandler(dir string) BlobHandler { return &diskHandler{dir: dir} }
 
+func (m *diskHandler) blobHashPath(h v1.Hash) string {
+	return filepath.Join(m.dir, h.Algorithm, h.Hex)
+}
+
 func (m *diskHandler) Stat(_ context.Context, _ string, h v1.Hash) (int64, error) {
-	fi, err := os.Stat(filepath.Join(m.dir, h.String()))
+	fi, err := os.Stat(m.blobHashPath(h))
 	if errors.Is(err, os.ErrNotExist) {
 		return 0, errNotFound
 	} else if err != nil {
@@ -40,7 +44,7 @@ func (m *diskHandler) Stat(_ context.Context, _ string, h v1.Hash) (int64, error
 	return fi.Size(), nil
 }
 func (m *diskHandler) Get(_ context.Context, _ string, h v1.Hash) (io.ReadCloser, error) {
-	return os.Open(filepath.Join(m.dir, h.String()))
+	return os.Open(m.blobHashPath(h))
 }
 func (m *diskHandler) Put(_ context.Context, _ string, h v1.Hash, rc io.ReadCloser) error {
 	// Put the temp file in the same directory to avoid cross-device problems
@@ -57,9 +61,11 @@ func (m *diskHandler) Put(_ context.Context, _ string, h v1.Hash, rc io.ReadClos
 	}(); err != nil {
 		return err
 	}
-
-	return os.Rename(f.Name(), filepath.Join(m.dir, h.String()))
+	if err := os.MkdirAll(filepath.Join(m.dir, h.Algorithm), os.ModePerm); err != nil {
+		return err
+	}
+	return os.Rename(f.Name(), m.blobHashPath(h))
 }
 func (m *diskHandler) Delete(_ context.Context, _ string, h v1.Hash) error {
-	return os.Remove(filepath.Join(m.dir, h.String()))
+	return os.Remove(m.blobHashPath(h))
 }


Test patch:
diff --git a/pkg/registry/blobs_disk_test.go b/pkg/registry/blobs_disk_test.go
index df29318f..00690c9d 100644
--- a/pkg/registry/blobs_disk_test.go
+++ b/pkg/registry/blobs_disk_test.go
@@ -15,6 +15,7 @@
 package registry_test
 
 import (
+	"fmt"
 	"net/http/httptest"
 	"os"
 	"path/filepath"
@@ -59,7 +60,7 @@ func TestDiskPush(t *testing.T) {
 	if h, err := img.ConfigName(); err != nil {
 		t.Fatal(err)
 	} else {
-		want[h.String()] = true
+		want[fmt.Sprintf("%s/%s", h.Algorithm, h.Hex)] = true
 	}
 	ls, err := img.Layers()
 	if err != nil {
@@ -69,7 +70,7 @@ func TestDiskPush(t *testing.T) {
 		if h, err := l.Digest(); err != nil {
 			t.Fatal(err)
 		} else {
-			want[h.String()] = true
+			want[fmt.Sprintf("%s/%s", h.Algorithm, h.Hex)] = true
 		}
 	}