Skip to content

Commit

Permalink
fix: mimic oci-layout (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Nov 9, 2023
1 parent b2485cb commit c722ce9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 11 additions & 5 deletions pkg/registry/blobs_disk.go
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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))
}
5 changes: 3 additions & 2 deletions pkg/registry/blobs_disk_test.go
Expand Up @@ -15,6 +15,7 @@
package registry_test

import (
"fmt"
"net/http/httptest"
"os"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down

0 comments on commit c722ce9

Please sign in to comment.