@@ -20,15 +20,18 @@ const (
20
20
)
21
21
22
22
type StoreIndex struct {
23
- indexPath string
24
- lockPath string
23
+ indexPath string
24
+ lockPath string
25
+ layoutPath string
25
26
}
26
27
27
28
func NewStoreIndex (storePath string ) StoreIndex {
28
29
indexPath := path .Join (storePath , indexFile )
30
+ layoutPath := path .Join (storePath , ocispecs .ImageLayoutFile )
29
31
return StoreIndex {
30
- indexPath : indexPath ,
31
- lockPath : indexPath + lockFileSuffix ,
32
+ indexPath : indexPath ,
33
+ lockPath : indexPath + lockFileSuffix ,
34
+ layoutPath : layoutPath ,
32
35
}
33
36
}
34
37
@@ -58,6 +61,7 @@ func (s StoreIndex) Read() (*ocispecs.Index, error) {
58
61
}
59
62
60
63
func (s StoreIndex ) Put (tag string , desc ocispecs.Descriptor ) error {
64
+ // lock the store to prevent concurrent access
61
65
lock := flock .New (s .lockPath )
62
66
locked , err := lock .TryLock ()
63
67
if err != nil {
@@ -71,36 +75,49 @@ func (s StoreIndex) Put(tag string, desc ocispecs.Descriptor) error {
71
75
os .RemoveAll (s .lockPath )
72
76
}()
73
77
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 )
75
92
if err != nil {
76
93
return errors .Wrapf (err , "could not open %s" , s .indexPath )
77
94
}
78
- defer f .Close ()
95
+ defer idxFile .Close ()
79
96
80
97
var idx ocispecs.Index
81
- b , err := io .ReadAll (f )
98
+ idxData , err := io .ReadAll (idxFile )
82
99
if err != nil {
83
100
return errors .Wrapf (err , "could not read %s" , s .indexPath )
84
101
}
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 ))
88
105
}
89
106
}
90
107
91
108
if err = insertDesc (& idx , desc , tag ); err != nil {
92
109
return err
93
110
}
94
111
95
- b , err = json .Marshal (idx )
112
+ idxData , err = json .Marshal (idx )
96
113
if err != nil {
97
114
return err
98
115
}
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 )
101
118
}
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 )
104
121
}
105
122
return nil
106
123
}
0 commit comments