Skip to content

Commit

Permalink
fix(oci): create blobs dir and define blobs dir name constant (#520)
Browse files Browse the repository at this point in the history
https://github.com/opencontainers/image-spec/blob/v1.0/image-layout.md#content
specifies that the blobs dir must exist, so the changed ensureDir in
this PR is a bugfix.

The addition and use of the ociBlobsDir constant is more of a cleanup
suggestion, and I can remove it if necessary. I've also asked for it to
be added upstream at
opencontainers/image-spec#1069 but that will
probably take significantly longer.

Signed-off-by: Clarence "Sparr" Risher <[email protected]>
  • Loading branch information
sparr authored and shizhMSFT committed Jul 4, 2023
1 parent f1c44e1 commit 9480525
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
7 changes: 6 additions & 1 deletion content/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ import (
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/image-layout.md#indexjson-file
const ociImageIndexFile = "index.json"

// ociBlobsDir is the name of the blobs directory
// from the OCI Image Layout Specification.
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/image-layout.md#content
const ociBlobsDir = "blobs"

// Store implements `oras.Target`, and represents a content store
// based on file system with the OCI-Image layout.
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/image-layout.md
Expand Down Expand Up @@ -90,7 +95,7 @@ func NewWithContext(ctx context.Context, root string) (*Store, error) {
graph: graph.NewMemory(),
}

if err := ensureDir(rootAbs); err != nil {
if err := ensureDir(filepath.Join(rootAbs, ociBlobsDir)); err != nil {
return nil, err
}
if err := store.ensureOCILayoutFile(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions content/oci/readonlyoci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestReadOnlyStore(t *testing.T) {
// build fs
fsys := fstest.MapFS{}
for i, desc := range descs {
path := strings.Join([]string{"blobs", desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
path := strings.Join([]string{ociBlobsDir, desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
fsys[path] = &fstest.MapFile{Data: blobs[i]}
}
fsys[ocispec.ImageLayoutFile] = &fstest.MapFile{Data: layoutJSON}
Expand Down Expand Up @@ -603,7 +603,7 @@ func TestReadOnlyStore_Copy_OCIToMemory(t *testing.T) {
// build fs
fsys := fstest.MapFS{}
for i, desc := range descs {
path := strings.Join([]string{"blobs", desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
path := strings.Join([]string{ociBlobsDir, desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
fsys[path] = &fstest.MapFile{Data: blobs[i]}
}
fsys[ocispec.ImageLayoutFile] = &fstest.MapFile{Data: layoutJSON}
Expand Down Expand Up @@ -717,7 +717,7 @@ func TestReadOnlyStore_Tags(t *testing.T) {
// build fs
fsys := fstest.MapFS{}
for i, desc := range descs {
path := strings.Join([]string{"blobs", desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
path := strings.Join([]string{ociBlobsDir, desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
fsys[path] = &fstest.MapFile{Data: blobs[i]}
}
fsys[ocispec.ImageLayoutFile] = &fstest.MapFile{Data: layoutJSON}
Expand Down
2 changes: 1 addition & 1 deletion content/oci/readonlystorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ func blobPath(dgst digest.Digest) (string, error) {
return "", fmt.Errorf("cannot calculate blob path from invalid digest %s: %w: %v",
dgst.String(), errdef.ErrInvalidDigest, err)
}
return path.Join("blobs", dgst.Algorithm().String(), dgst.Encoded()), nil
return path.Join(ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded()), nil
}
6 changes: 3 additions & 3 deletions content/oci/readonlystorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestReadOnlyStorage_Exists(t *testing.T) {
dgst := digest.FromBytes(blob)
desc := content.NewDescriptorFromBytes("", blob)
fsys := fstest.MapFS{
strings.Join([]string{"blobs", dgst.Algorithm().String(), dgst.Encoded()}, "/"): {},
strings.Join([]string{ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded()}, "/"): {},
}
s := NewStorageFromFS(fsys)
ctx := context.Background()
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestReadOnlyStorage_Fetch(t *testing.T) {
dgst := digest.FromBytes(blob)
desc := content.NewDescriptorFromBytes("", blob)
fsys := fstest.MapFS{
strings.Join([]string{"blobs", dgst.Algorithm().String(), dgst.Encoded()}, "/"): {
strings.Join([]string{ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded()}, "/"): {
Data: blob,
},
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestReadOnlyStorage_DirFS(t *testing.T) {
dgst := digest.FromBytes(blob)
desc := content.NewDescriptorFromBytes("test", blob)
// write blob to disk
path := filepath.Join(tempDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
path := filepath.Join(tempDir, ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded())
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
t.Fatal("error calling Mkdir(), error =", err)
}
Expand Down
2 changes: 1 addition & 1 deletion content/oci/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestStorage_Fetch_ExistingBlobs(t *testing.T) {
}

tempDir := t.TempDir()
path := filepath.Join(tempDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
path := filepath.Join(tempDir, ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded())
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
t.Fatal("error calling Mkdir(), error =", err)
}
Expand Down

0 comments on commit 9480525

Please sign in to comment.