Skip to content

Commit

Permalink
fix: mimic oci-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Oct 11, 2023
1 parent dbcd01c commit 2aeb32c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 7 additions & 5 deletions pkg/registry/blobs_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type diskHandler struct {
func NewDiskBlobHandler(dir string) BlobHandler { return &diskHandler{dir: dir} }

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(filepath.Join(m.dir, h.Algorithm, h.Hex))

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
if errors.Is(err, os.ErrNotExist) {
return 0, errNotFound
} else if err != nil {
Expand All @@ -40,7 +40,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(filepath.Join(m.dir, h.Algorithm, h.Hex))
}
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 +57,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(), filepath.Join(m.dir, h.Algorithm, h.Hex))
}
func (m *diskHandler) Delete(_ context.Context, _ string, h v1.Hash) error {
return os.Remove(filepath.Join(m.dir, h.String()))
return os.Remove(filepath.Join(m.dir, h.Algorithm, h.Hex))

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
}
5 changes: 3 additions & 2 deletions pkg/registry/blobs_disk_test.go
Original file line number Diff line number Diff line change
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 2aeb32c

Please sign in to comment.