Skip to content

Commit c722ce9

Browse files
authored
fix: mimic oci-layout (#1810)
1 parent b2485cb commit c722ce9

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pkg/registry/blobs_disk.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ type diskHandler struct {
3030

3131
func NewDiskBlobHandler(dir string) BlobHandler { return &diskHandler{dir: dir} }
3232

33+
func (m *diskHandler) blobHashPath(h v1.Hash) string {
34+
return filepath.Join(m.dir, h.Algorithm, h.Hex)
35+
}
36+
3337
func (m *diskHandler) Stat(_ context.Context, _ string, h v1.Hash) (int64, error) {
34-
fi, err := os.Stat(filepath.Join(m.dir, h.String()))
38+
fi, err := os.Stat(m.blobHashPath(h))
3539
if errors.Is(err, os.ErrNotExist) {
3640
return 0, errNotFound
3741
} else if err != nil {
@@ -40,7 +44,7 @@ func (m *diskHandler) Stat(_ context.Context, _ string, h v1.Hash) (int64, error
4044
return fi.Size(), nil
4145
}
4246
func (m *diskHandler) Get(_ context.Context, _ string, h v1.Hash) (io.ReadCloser, error) {
43-
return os.Open(filepath.Join(m.dir, h.String()))
47+
return os.Open(m.blobHashPath(h))
4448
}
4549
func (m *diskHandler) Put(_ context.Context, _ string, h v1.Hash, rc io.ReadCloser) error {
4650
// 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
5761
}(); err != nil {
5862
return err
5963
}
60-
61-
return os.Rename(f.Name(), filepath.Join(m.dir, h.String()))
64+
if err := os.MkdirAll(filepath.Join(m.dir, h.Algorithm), os.ModePerm); err != nil {
65+
return err
66+
}
67+
return os.Rename(f.Name(), m.blobHashPath(h))
6268
}
6369
func (m *diskHandler) Delete(_ context.Context, _ string, h v1.Hash) error {
64-
return os.Remove(filepath.Join(m.dir, h.String()))
70+
return os.Remove(m.blobHashPath(h))
6571
}

pkg/registry/blobs_disk_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package registry_test
1616

1717
import (
18+
"fmt"
1819
"net/http/httptest"
1920
"os"
2021
"path/filepath"
@@ -59,7 +60,7 @@ func TestDiskPush(t *testing.T) {
5960
if h, err := img.ConfigName(); err != nil {
6061
t.Fatal(err)
6162
} else {
62-
want[h.String()] = true
63+
want[fmt.Sprintf("%s/%s", h.Algorithm, h.Hex)] = true
6364
}
6465
ls, err := img.Layers()
6566
if err != nil {
@@ -69,7 +70,7 @@ func TestDiskPush(t *testing.T) {
6970
if h, err := l.Digest(); err != nil {
7071
t.Fatal(err)
7172
} else {
72-
want[h.String()] = true
73+
want[fmt.Sprintf("%s/%s", h.Algorithm, h.Hex)] = true
7374
}
7475
}
7576

0 commit comments

Comments
 (0)