@@ -90,24 +90,10 @@ func (lf *LocalFile) Create(ctx context.Context, cfg *types.SnapshotConfig, stre
9090 dataDir := lf .conf .SnapshotDataDir (id )
9191 now := time .Now ()
9292
93- if err = lf .store .Update (ctx , func (idx * snapshot.SnapshotIndex ) error {
94- if cfg .Name != "" {
95- if existingID , ok := idx .Names [cfg .Name ]; ok {
96- return fmt .Errorf ("snapshot name %q already in use by %s" , cfg .Name , existingID )
97- }
98- }
99- idx .Snapshots [id ] = & snapshot.SnapshotRecord {
100- Snapshot : types.Snapshot {
101- SnapshotConfig : * cfg ,
102- CreatedAt : now ,
103- },
104- Pending : true ,
105- DataDir : dataDir ,
106- }
107- if cfg .Name != "" {
108- idx .Names [cfg .Name ] = id
109- }
110- return nil
93+ if err = lf .insertRecord (ctx , id , cfg .Name , & snapshot.SnapshotRecord {
94+ Snapshot : types.Snapshot {SnapshotConfig : * cfg , CreatedAt : now },
95+ Pending : true ,
96+ DataDir : dataDir ,
11197 }); err != nil {
11298 return "" , err
11399 }
@@ -235,6 +221,22 @@ func (lf *LocalFile) deleteOne(ctx context.Context, id string) error {
235221 return nil
236222}
237223
224+ // insertRecord adds rec under id with name-collision check; both Create (Pending) and Import (finalized) go through here.
225+ func (lf * LocalFile ) insertRecord (ctx context.Context , id , name string , rec * snapshot.SnapshotRecord ) error {
226+ return lf .store .Update (ctx , func (idx * snapshot.SnapshotIndex ) error {
227+ if name != "" {
228+ if existingID , ok := idx .Names [name ]; ok {
229+ return fmt .Errorf ("snapshot name %q already in use by %s" , name , existingID )
230+ }
231+ }
232+ idx .Snapshots [id ] = rec
233+ if name != "" {
234+ idx .Names [name ] = id
235+ }
236+ return nil
237+ })
238+ }
239+
238240// rollbackCreate removes a placeholder snapshot record from the DB.
239241func (lf * LocalFile ) rollbackCreate (ctx context.Context , id , name string ) {
240242 if err := lf .store .Update (ctx , func (idx * snapshot.SnapshotIndex ) error {
0 commit comments