Skip to content

Commit

Permalink
Ensure content writer only unlocks resource once
Browse files Browse the repository at this point in the history
Make close a no-op after commit and disallow committing after close.

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
  • Loading branch information
dmcgowan committed Aug 21, 2017
1 parent 8095244 commit c710662
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions content/local/writer.go
Expand Up @@ -55,6 +55,10 @@ func (w *writer) Write(p []byte) (n int, err error) {
}

func (w *writer) Commit(size int64, expected digest.Digest, opts ...content.Opt) error {
if w.fp == nil {
return errors.Wrap(errdefs.ErrFailedPrecondition, "cannot commit on closed writer")
}

if err := w.fp.Sync(); err != nil {
return errors.Wrap(err, "sync failed")
}
Expand Down Expand Up @@ -115,8 +119,8 @@ func (w *writer) Commit(size int64, expected digest.Digest, opts ...content.Opt)
return err
}

unlock(w.ref)
w.fp = nil
unlock(w.ref)

return nil
}
Expand All @@ -130,12 +134,13 @@ func (w *writer) Commit(size int64, expected digest.Digest, opts ...content.Opt)
//
// To abandon a transaction completely, first call close then `Store.Remove` to
// clean up the associated resources.
func (cw *writer) Close() (err error) {
unlock(cw.ref)

if cw.fp != nil {
cw.fp.Sync()
return cw.fp.Close()
func (w *writer) Close() (err error) {
if w.fp != nil {
w.fp.Sync()
err = w.fp.Close()
w.fp = nil
unlock(w.ref)
return
}

return nil
Expand Down

0 comments on commit c710662

Please sign in to comment.