Skip to content

Commit

Permalink
Merge pull request #1603 from giuseppe/update-lock-before-write
Browse files Browse the repository at this point in the history
store: ensure lockfile is updated before writes
  • Loading branch information
rhatdan authored May 19, 2023
2 parents 561b148 + ed76ab9 commit ca646a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
12 changes: 7 additions & 5 deletions containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,13 @@ 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()
// This must be done before we write the file, because the process could be terminated
// after the file is written but before the lock file is updated.
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 +560,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
8 changes: 5 additions & 3 deletions images.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,16 @@ func (r *imageStore) Save() error {
if err != nil {
return err
}
if err := ioutils.AtomicWriteFile(rpath, jdata, 0600); err != nil {
return err
}
// This must be done before we write the file, because the process could be terminated
// after the file is written but before the lock file is updated.
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
23 changes: 15 additions & 8 deletions layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,14 @@ func (r *layerStore) saveLayers(saveLocations layerLocations) error {
}
r.lockfile.AssertLockedForWriting()

// This must be done before we write the file, because the process could be terminated
// after the file is written but before the lock file is updated.
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 +957,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 +985,18 @@ func (r *layerStore) saveMounts() error {
if err != nil {
return err
}
if err = ioutils.AtomicWriteFile(mpath, jmdata, 0600); err != nil {
return err
}

// This must be done before we write the file, because the process could be terminated
// after the file is written but before the lock file is updated.
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 ca646a3

Please sign in to comment.