Skip to content

Commit 81f565f

Browse files
authored
Merge pull request moby#3729 from jedevc/oci-index-store-create-layout
client: create oci-layout file in StoreIndex
2 parents 35287b9 + 8d14365 commit 81f565f

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

client/ociindex/ociindex.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ const (
2020
)
2121

2222
type StoreIndex struct {
23-
indexPath string
24-
lockPath string
23+
indexPath string
24+
lockPath string
25+
layoutPath string
2526
}
2627

2728
func NewStoreIndex(storePath string) StoreIndex {
2829
indexPath := path.Join(storePath, indexFile)
30+
layoutPath := path.Join(storePath, ocispecs.ImageLayoutFile)
2931
return StoreIndex{
30-
indexPath: indexPath,
31-
lockPath: indexPath + lockFileSuffix,
32+
indexPath: indexPath,
33+
lockPath: indexPath + lockFileSuffix,
34+
layoutPath: layoutPath,
3235
}
3336
}
3437

@@ -58,6 +61,7 @@ func (s StoreIndex) Read() (*ocispecs.Index, error) {
5861
}
5962

6063
func (s StoreIndex) Put(tag string, desc ocispecs.Descriptor) error {
64+
// lock the store to prevent concurrent access
6165
lock := flock.New(s.lockPath)
6266
locked, err := lock.TryLock()
6367
if err != nil {
@@ -71,36 +75,49 @@ func (s StoreIndex) Put(tag string, desc ocispecs.Descriptor) error {
7175
os.RemoveAll(s.lockPath)
7276
}()
7377

74-
f, err := os.OpenFile(s.indexPath, os.O_RDWR|os.O_CREATE, 0644)
78+
// create the oci-layout file
79+
layout := ocispecs.ImageLayout{
80+
Version: ocispecs.ImageLayoutVersion,
81+
}
82+
layoutData, err := json.Marshal(layout)
83+
if err != nil {
84+
return err
85+
}
86+
if err := os.WriteFile(s.layoutPath, layoutData, 0644); err != nil {
87+
return err
88+
}
89+
90+
// modify the index file
91+
idxFile, err := os.OpenFile(s.indexPath, os.O_RDWR|os.O_CREATE, 0644)
7592
if err != nil {
7693
return errors.Wrapf(err, "could not open %s", s.indexPath)
7794
}
78-
defer f.Close()
95+
defer idxFile.Close()
7996

8097
var idx ocispecs.Index
81-
b, err := io.ReadAll(f)
98+
idxData, err := io.ReadAll(idxFile)
8299
if err != nil {
83100
return errors.Wrapf(err, "could not read %s", s.indexPath)
84101
}
85-
if len(b) > 0 {
86-
if err := json.Unmarshal(b, &idx); err != nil {
87-
return errors.Wrapf(err, "could not unmarshal %s (%q)", s.indexPath, string(b))
102+
if len(idxData) > 0 {
103+
if err := json.Unmarshal(idxData, &idx); err != nil {
104+
return errors.Wrapf(err, "could not unmarshal %s (%q)", s.indexPath, string(idxData))
88105
}
89106
}
90107

91108
if err = insertDesc(&idx, desc, tag); err != nil {
92109
return err
93110
}
94111

95-
b, err = json.Marshal(idx)
112+
idxData, err = json.Marshal(idx)
96113
if err != nil {
97114
return err
98115
}
99-
if _, err = f.WriteAt(b, 0); err != nil {
100-
return err
116+
if _, err = idxFile.WriteAt(idxData, 0); err != nil {
117+
return errors.Wrapf(err, "could not write %s", s.indexPath)
101118
}
102-
if err = f.Truncate(int64(len(b))); err != nil {
103-
return err
119+
if err = idxFile.Truncate(int64(len(idxData))); err != nil {
120+
return errors.Wrapf(err, "could not truncate %s", s.indexPath)
104121
}
105122
return nil
106123
}

0 commit comments

Comments
 (0)