Skip to content

Commit 9480525

Browse files
sparrshizhMSFT
authored andcommitted
fix(oci): create blobs dir and define blobs dir name constant (#520)
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 <clrnc@amazon.com>
1 parent f1c44e1 commit 9480525

5 files changed

+14
-9
lines changed

content/oci/oci.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ import (
4343
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/image-layout.md#indexjson-file
4444
const ociImageIndexFile = "index.json"
4545

46+
// ociBlobsDir is the name of the blobs directory
47+
// from the OCI Image Layout Specification.
48+
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/image-layout.md#content
49+
const ociBlobsDir = "blobs"
50+
4651
// Store implements `oras.Target`, and represents a content store
4752
// based on file system with the OCI-Image layout.
4853
// Reference: https://github.com/opencontainers/image-spec/blob/v1.1.0-rc2/image-layout.md
@@ -90,7 +95,7 @@ func NewWithContext(ctx context.Context, root string) (*Store, error) {
9095
graph: graph.NewMemory(),
9196
}
9297

93-
if err := ensureDir(rootAbs); err != nil {
98+
if err := ensureDir(filepath.Join(rootAbs, ociBlobsDir)); err != nil {
9499
return nil, err
95100
}
96101
if err := store.ensureOCILayoutFile(); err != nil {

content/oci/readonlyoci_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestReadOnlyStore(t *testing.T) {
123123
// build fs
124124
fsys := fstest.MapFS{}
125125
for i, desc := range descs {
126-
path := strings.Join([]string{"blobs", desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
126+
path := strings.Join([]string{ociBlobsDir, desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
127127
fsys[path] = &fstest.MapFile{Data: blobs[i]}
128128
}
129129
fsys[ocispec.ImageLayoutFile] = &fstest.MapFile{Data: layoutJSON}
@@ -603,7 +603,7 @@ func TestReadOnlyStore_Copy_OCIToMemory(t *testing.T) {
603603
// build fs
604604
fsys := fstest.MapFS{}
605605
for i, desc := range descs {
606-
path := strings.Join([]string{"blobs", desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
606+
path := strings.Join([]string{ociBlobsDir, desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
607607
fsys[path] = &fstest.MapFile{Data: blobs[i]}
608608
}
609609
fsys[ocispec.ImageLayoutFile] = &fstest.MapFile{Data: layoutJSON}
@@ -717,7 +717,7 @@ func TestReadOnlyStore_Tags(t *testing.T) {
717717
// build fs
718718
fsys := fstest.MapFS{}
719719
for i, desc := range descs {
720-
path := strings.Join([]string{"blobs", desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
720+
path := strings.Join([]string{ociBlobsDir, desc.Digest.Algorithm().String(), desc.Digest.Encoded()}, "/")
721721
fsys[path] = &fstest.MapFile{Data: blobs[i]}
722722
}
723723
fsys[ocispec.ImageLayoutFile] = &fstest.MapFile{Data: layoutJSON}

content/oci/readonlystorage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ func blobPath(dgst digest.Digest) (string, error) {
9595
return "", fmt.Errorf("cannot calculate blob path from invalid digest %s: %w: %v",
9696
dgst.String(), errdef.ErrInvalidDigest, err)
9797
}
98-
return path.Join("blobs", dgst.Algorithm().String(), dgst.Encoded()), nil
98+
return path.Join(ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded()), nil
9999
}

content/oci/readonlystorage_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestReadOnlyStorage_Exists(t *testing.T) {
3838
dgst := digest.FromBytes(blob)
3939
desc := content.NewDescriptorFromBytes("", blob)
4040
fsys := fstest.MapFS{
41-
strings.Join([]string{"blobs", dgst.Algorithm().String(), dgst.Encoded()}, "/"): {},
41+
strings.Join([]string{ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded()}, "/"): {},
4242
}
4343
s := NewStorageFromFS(fsys)
4444
ctx := context.Background()
@@ -76,7 +76,7 @@ func TestReadOnlyStorage_Fetch(t *testing.T) {
7676
dgst := digest.FromBytes(blob)
7777
desc := content.NewDescriptorFromBytes("", blob)
7878
fsys := fstest.MapFS{
79-
strings.Join([]string{"blobs", dgst.Algorithm().String(), dgst.Encoded()}, "/"): {
79+
strings.Join([]string{ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded()}, "/"): {
8080
Data: blob,
8181
},
8282
}
@@ -123,7 +123,7 @@ func TestReadOnlyStorage_DirFS(t *testing.T) {
123123
dgst := digest.FromBytes(blob)
124124
desc := content.NewDescriptorFromBytes("test", blob)
125125
// write blob to disk
126-
path := filepath.Join(tempDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
126+
path := filepath.Join(tempDir, ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded())
127127
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
128128
t.Fatal("error calling Mkdir(), error =", err)
129129
}

content/oci/storage_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func TestStorage_Fetch_ExistingBlobs(t *testing.T) {
289289
}
290290

291291
tempDir := t.TempDir()
292-
path := filepath.Join(tempDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
292+
path := filepath.Join(tempDir, ociBlobsDir, dgst.Algorithm().String(), dgst.Encoded())
293293
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
294294
t.Fatal("error calling Mkdir(), error =", err)
295295
}

0 commit comments

Comments
 (0)