Skip to content

Commit 6b0f9f1

Browse files
committed
Replace LockFile with StagingLockfile for overlay staging
Signed-off-by: Jan Rodák <[email protected]>
1 parent 03595f9 commit 6b0f9f1

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

drivers/overlay/overlay.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
"github.com/containers/storage/drivers/overlayutils"
2424
"github.com/containers/storage/drivers/quota"
2525
"github.com/containers/storage/internal/dedup"
26+
"github.com/containers/storage/internal/staging_lockfile"
2627
"github.com/containers/storage/pkg/archive"
2728
"github.com/containers/storage/pkg/chrootarchive"
2829
"github.com/containers/storage/pkg/directory"
2930
"github.com/containers/storage/pkg/fileutils"
3031
"github.com/containers/storage/pkg/fsutils"
3132
"github.com/containers/storage/pkg/idmap"
3233
"github.com/containers/storage/pkg/idtools"
33-
"github.com/containers/storage/pkg/lockfile"
3434
"github.com/containers/storage/pkg/mount"
3535
"github.com/containers/storage/pkg/parsers"
3636
"github.com/containers/storage/pkg/system"
@@ -133,7 +133,7 @@ type Driver struct {
133133
stagingDirsLocksMutex sync.Mutex
134134
// stagingDirsLocks access is not thread safe, it is required that callers take
135135
// stagingDirsLocksMutex on each access to guard against concurrent map writes.
136-
stagingDirsLocks map[string]*lockfile.LockFile
136+
stagingDirsLocks map[string]*staging_lockfile.StagingLockFile
137137

138138
supportsIDMappedMounts *bool
139139
}
@@ -442,7 +442,7 @@ func Init(home string, options graphdriver.Options) (graphdriver.Driver, error)
442442
usingComposefs: opts.useComposefs,
443443
options: *opts,
444444
stagingDirsLocksMutex: sync.Mutex{},
445-
stagingDirsLocks: make(map[string]*lockfile.LockFile),
445+
stagingDirsLocks: make(map[string]*staging_lockfile.StagingLockFile),
446446
}
447447

448448
d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, graphdriver.NewNaiveLayerIDMapUpdater(d))
@@ -874,7 +874,9 @@ func (d *Driver) Cleanup() error {
874874
func (d *Driver) pruneStagingDirectories() bool {
875875
d.stagingDirsLocksMutex.Lock()
876876
for _, lock := range d.stagingDirsLocks {
877-
lock.Unlock()
877+
if err := lock.UnlockAndDelete(); err != nil {
878+
logrus.Warnf("Failed to unlock and delete staging lock file: %v", err)
879+
}
878880
}
879881
clear(d.stagingDirsLocks)
880882
d.stagingDirsLocksMutex.Unlock()
@@ -886,17 +888,15 @@ func (d *Driver) pruneStagingDirectories() bool {
886888
if err == nil {
887889
for _, dir := range dirs {
888890
stagingDirToRemove := filepath.Join(stagingDirBase, dir.Name())
889-
lock, err := lockfile.GetLockFile(filepath.Join(stagingDirToRemove, stagingLockFile))
891+
lock, err := staging_lockfile.TryLockPath(filepath.Join(stagingDirToRemove, stagingLockFile))
890892
if err != nil {
891893
anyPresent = true
892894
continue
893895
}
894-
if err := lock.TryLock(); err != nil {
895-
anyPresent = true
896-
continue
897-
}
898896
_ = os.RemoveAll(stagingDirToRemove)
899-
lock.Unlock()
897+
if err := lock.UnlockAndDelete(); err != nil {
898+
logrus.Warnf("Failed to unlock and delete staging lock file: %v", err)
899+
}
900900
}
901901
}
902902
return anyPresent
@@ -2178,7 +2178,10 @@ func (d *Driver) CleanupStagingDirectory(stagingDirectory string) error {
21782178
d.stagingDirsLocksMutex.Lock()
21792179
if lock, ok := d.stagingDirsLocks[parentStagingDir]; ok {
21802180
delete(d.stagingDirsLocks, parentStagingDir)
2181-
lock.Unlock()
2181+
if err := lock.UnlockAndDelete(); err != nil {
2182+
d.stagingDirsLocksMutex.Unlock()
2183+
return err
2184+
}
21822185
}
21832186
d.stagingDirsLocksMutex.Unlock()
21842187

@@ -2233,7 +2236,7 @@ func (d *Driver) ApplyDiffWithDiffer(options *graphdriver.ApplyDiffWithDifferOpt
22332236
return graphdriver.DriverWithDifferOutput{}, err
22342237
}
22352238

2236-
lock, err := lockfile.GetLockFile(filepath.Join(layerDir, stagingLockFile))
2239+
lock, err := staging_lockfile.TryLockPath(filepath.Join(layerDir, stagingLockFile))
22372240
if err != nil {
22382241
return graphdriver.DriverWithDifferOutput{}, err
22392242
}
@@ -2242,13 +2245,14 @@ func (d *Driver) ApplyDiffWithDiffer(options *graphdriver.ApplyDiffWithDifferOpt
22422245
d.stagingDirsLocksMutex.Lock()
22432246
delete(d.stagingDirsLocks, layerDir)
22442247
d.stagingDirsLocksMutex.Unlock()
2245-
lock.Unlock()
2248+
if err := lock.UnlockAndDelete(); err != nil {
2249+
errRet = errors.Join(errRet, err)
2250+
}
22462251
}
22472252
}()
22482253
d.stagingDirsLocksMutex.Lock()
22492254
d.stagingDirsLocks[layerDir] = lock
22502255
d.stagingDirsLocksMutex.Unlock()
2251-
lock.Lock()
22522256

22532257
logrus.Debugf("Applying differ in %s", applyDir)
22542258

@@ -2274,15 +2278,17 @@ func (d *Driver) ApplyDiffWithDiffer(options *graphdriver.ApplyDiffWithDifferOpt
22742278
}
22752279

22762280
// ApplyDiffFromStagingDirectory applies the changes using the specified staging directory.
2277-
func (d *Driver) ApplyDiffFromStagingDirectory(id, parent string, diffOutput *graphdriver.DriverWithDifferOutput, options *graphdriver.ApplyDiffWithDifferOpts) error {
2281+
func (d *Driver) ApplyDiffFromStagingDirectory(id, parent string, diffOutput *graphdriver.DriverWithDifferOutput, options *graphdriver.ApplyDiffWithDifferOpts) (errRet error) {
22782282
stagingDirectory := diffOutput.Target
22792283
parentStagingDir := filepath.Dir(stagingDirectory)
22802284

22812285
defer func() {
22822286
d.stagingDirsLocksMutex.Lock()
22832287
if lock, ok := d.stagingDirsLocks[parentStagingDir]; ok {
22842288
delete(d.stagingDirsLocks, parentStagingDir)
2285-
lock.Unlock()
2289+
if err := lock.UnlockAndDelete(); err != nil {
2290+
errRet = errors.Join(errRet, err)
2291+
}
22862292
}
22872293
d.stagingDirsLocksMutex.Unlock()
22882294
}()

0 commit comments

Comments
 (0)