Skip to content

Commit

Permalink
store: ensure lockfile is updated before writes
Browse files Browse the repository at this point in the history
The lockfile's write record is now updated prior to the actual write
operation.  This ensures that, in the event of an unexpected
termination, other processes are correctly notified of an initiated
write operation.

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed May 18, 2023
1 parent 561b148 commit 89ddbb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
10 changes: 5 additions & 5 deletions containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ func (r *containerStore) load(lockedForWriting bool) (bool, error) {
// The caller must hold r.inProcessLock for reading (but usually holds it for writing in order to make the desired changes).
func (r *containerStore) save(saveLocations containerLocations) error {
r.lockfile.AssertLockedForWriting()
lw, err := r.lockfile.RecordWrite()
if err != nil {
return err
}
r.lastWrite = lw
for locationIndex := 0; locationIndex < numContainerLocationIndex; locationIndex++ {
location := containerLocationFromIndex(locationIndex)
if location&saveLocations == 0 {
Expand Down Expand Up @@ -553,11 +558,6 @@ func (r *containerStore) save(saveLocations containerLocations) error {
return err
}
}
lw, err := r.lockfile.RecordWrite()
if err != nil {
return err
}
r.lastWrite = lw
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions images.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,14 @@ func (r *imageStore) Save() error {
if err != nil {
return err
}
if err := ioutils.AtomicWriteFile(rpath, jdata, 0600); err != nil {
return err
}
lw, err := r.lockfile.RecordWrite()
if err != nil {
return err
}
r.lastWrite = lw
if err := ioutils.AtomicWriteFile(rpath, jdata, 0600); err != nil {
return err
}
return nil
}

Expand Down
19 changes: 11 additions & 8 deletions layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,12 @@ func (r *layerStore) saveLayers(saveLocations layerLocations) error {
}
r.lockfile.AssertLockedForWriting()

lw, err := r.lockfile.RecordWrite()
if err != nil {
return err
}
r.lastWrite = lw

for locationIndex := 0; locationIndex < numLayerLocationIndex; locationIndex++ {
location := layerLocationFromIndex(locationIndex)
if location&saveLocations == 0 {
Expand Down Expand Up @@ -949,11 +955,6 @@ func (r *layerStore) saveLayers(saveLocations layerLocations) error {
}
r.layerspathsModified[locationIndex] = opts.ModTime
}
lw, err := r.lockfile.RecordWrite()
if err != nil {
return err
}
r.lastWrite = lw
return nil
}

Expand Down Expand Up @@ -982,14 +983,16 @@ func (r *layerStore) saveMounts() error {
if err != nil {
return err
}
if err = ioutils.AtomicWriteFile(mpath, jmdata, 0600); err != nil {
return err
}

lw, err := r.mountsLockfile.RecordWrite()
if err != nil {
return err
}
r.mountsLastWrite = lw

if err = ioutils.AtomicWriteFile(mpath, jmdata, 0600); err != nil {
return err
}
return r.loadMounts()
}

Expand Down

0 comments on commit 89ddbb8

Please sign in to comment.