Skip to content

Commit 6875d3d

Browse files
committed
Always check exists on commit error
Signed-off-by: Derek McGowan <[email protected]>
1 parent c0cb2f2 commit 6875d3d

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

content/local/writer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ func (w *writer) Commit(ctx context.Context, size int64, expected digest.Digest,
132132
// clean up!!
133133
defer os.RemoveAll(w.path)
134134

135+
if _, err := os.Stat(target); err == nil {
136+
// collision with the target file!
137+
return errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", dgst)
138+
}
135139
if err := os.Rename(ingest, target); err != nil {
136-
if os.IsExist(err) {
137-
// collision with the target file!
138-
return errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", dgst)
139-
}
140140
return err
141141
}
142142
commitTime := time.Now()

diff/walking/differ.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
139139

140140
dgst := cw.Digest()
141141
if err := cw.Commit(ctx, 0, dgst, commitopts...); err != nil {
142-
return errors.Wrap(err, "failed to commit")
142+
if !errdefs.IsAlreadyExists(err) {
143+
return errors.Wrap(err, "failed to commit")
144+
}
143145
}
144146

145147
info, err := s.store.Info(ctx, dgst)

metadata/buckets.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ func getSnapshotterBucket(tx *bolt.Tx, namespace, snapshotter string) *bolt.Buck
164164
}
165165

166166
func createBlobBucket(tx *bolt.Tx, namespace string, dgst digest.Digest) (*bolt.Bucket, error) {
167-
bkt, err := createBucketIfNotExists(tx, bucketKeyVersion, []byte(namespace), bucketKeyObjectContent, bucketKeyObjectBlob, []byte(dgst.String()))
167+
bkt, err := createBucketIfNotExists(tx, bucketKeyVersion, []byte(namespace), bucketKeyObjectContent, bucketKeyObjectBlob)
168168
if err != nil {
169169
return nil, err
170170
}
171-
return bkt, nil
171+
return bkt.CreateBucket([]byte(dgst.String()))
172172
}
173173

174174
func getBlobsBucket(tx *bolt.Tx, namespace string) *bolt.Bucket {

metadata/content.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,6 @@ func (nw *namespacedWriter) commit(ctx context.Context, tx *bolt.Tx, size int64,
592592
}
593593
size = nw.desc.Size
594594
actual = nw.desc.Digest
595-
if getBlobBucket(tx, nw.namespace, actual) != nil {
596-
return "", errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", actual)
597-
}
598595
} else {
599596
status, err := nw.w.Status()
600597
if err != nil {
@@ -606,18 +603,16 @@ func (nw *namespacedWriter) commit(ctx context.Context, tx *bolt.Tx, size int64,
606603
size = status.Offset
607604
actual = nw.w.Digest()
608605

609-
if err := nw.w.Commit(ctx, size, expected); err != nil {
610-
if !errdefs.IsAlreadyExists(err) {
611-
return "", err
612-
}
613-
if getBlobBucket(tx, nw.namespace, actual) != nil {
614-
return "", errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", actual)
615-
}
606+
if err := nw.w.Commit(ctx, size, expected); err != nil && !errdefs.IsAlreadyExists(err) {
607+
return "", err
616608
}
617609
}
618610

619611
bkt, err := createBlobBucket(tx, nw.namespace, actual)
620612
if err != nil {
613+
if err == bolt.ErrBucketExists {
614+
return "", errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", actual)
615+
}
621616
return "", err
622617
}
623618

services/content/service.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (s *service) Read(req *api.ReadContentRequest, session api.Content_ReadServ
213213
_, err = io.CopyBuffer(
214214
&readResponseWriter{session: session},
215215
io.NewSectionReader(ra, offset, size), *p)
216-
return err
216+
return errdefs.ToGRPC(err)
217217
}
218218

219219
// readResponseWriter is a writer that places the output into ReadContentRequest messages.
@@ -420,7 +420,7 @@ func (s *service) Write(session api.Content_WriteServer) (err error) {
420420
// maintain the offset as append only, we just issue the write.
421421
n, err := wr.Write(req.Data)
422422
if err != nil {
423-
return err
423+
return errdefs.ToGRPC(err)
424424
}
425425

426426
if n != len(req.Data) {
@@ -438,7 +438,7 @@ func (s *service) Write(session api.Content_WriteServer) (err error) {
438438
opts = append(opts, content.WithLabels(req.Labels))
439439
}
440440
if err := wr.Commit(ctx, total, expected, opts...); err != nil {
441-
return err
441+
return errdefs.ToGRPC(err)
442442
}
443443
}
444444

task.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,11 @@ func writeContent(ctx context.Context, store content.Ingester, mediaType, ref st
607607
if err != nil {
608608
return d, err
609609
}
610+
610611
if err := writer.Commit(ctx, size, "", opts...); err != nil {
611-
return d, err
612+
if !errdefs.IsAlreadyExists(err) {
613+
return d, err
614+
}
612615
}
613616
return v1.Descriptor{
614617
MediaType: mediaType,

0 commit comments

Comments
 (0)