@@ -23,14 +23,14 @@ import (
23
23
"github.com/containers/storage/drivers/overlayutils"
24
24
"github.com/containers/storage/drivers/quota"
25
25
"github.com/containers/storage/internal/dedup"
26
+ "github.com/containers/storage/internal/staging_lockfile"
26
27
"github.com/containers/storage/pkg/archive"
27
28
"github.com/containers/storage/pkg/chrootarchive"
28
29
"github.com/containers/storage/pkg/directory"
29
30
"github.com/containers/storage/pkg/fileutils"
30
31
"github.com/containers/storage/pkg/fsutils"
31
32
"github.com/containers/storage/pkg/idmap"
32
33
"github.com/containers/storage/pkg/idtools"
33
- "github.com/containers/storage/pkg/lockfile"
34
34
"github.com/containers/storage/pkg/mount"
35
35
"github.com/containers/storage/pkg/parsers"
36
36
"github.com/containers/storage/pkg/system"
@@ -133,7 +133,7 @@ type Driver struct {
133
133
stagingDirsLocksMutex sync.Mutex
134
134
// stagingDirsLocks access is not thread safe, it is required that callers take
135
135
// stagingDirsLocksMutex on each access to guard against concurrent map writes.
136
- stagingDirsLocks map [string ]* lockfile. LockFile
136
+ stagingDirsLocks map [string ]* staging_lockfile. StagingLockFile
137
137
138
138
supportsIDMappedMounts * bool
139
139
}
@@ -442,7 +442,7 @@ func Init(home string, options graphdriver.Options) (graphdriver.Driver, error)
442
442
usingComposefs : opts .useComposefs ,
443
443
options : * opts ,
444
444
stagingDirsLocksMutex : sync.Mutex {},
445
- stagingDirsLocks : make (map [string ]* lockfile. LockFile ),
445
+ stagingDirsLocks : make (map [string ]* staging_lockfile. StagingLockFile ),
446
446
}
447
447
448
448
d .naiveDiff = graphdriver .NewNaiveDiffDriver (d , graphdriver .NewNaiveLayerIDMapUpdater (d ))
@@ -874,7 +874,9 @@ func (d *Driver) Cleanup() error {
874
874
func (d * Driver ) pruneStagingDirectories () bool {
875
875
d .stagingDirsLocksMutex .Lock ()
876
876
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
+ }
878
880
}
879
881
clear (d .stagingDirsLocks )
880
882
d .stagingDirsLocksMutex .Unlock ()
@@ -886,17 +888,15 @@ func (d *Driver) pruneStagingDirectories() bool {
886
888
if err == nil {
887
889
for _ , dir := range dirs {
888
890
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 ))
890
892
if err != nil {
891
893
anyPresent = true
892
894
continue
893
895
}
894
- if err := lock .TryLock (); err != nil {
895
- anyPresent = true
896
- continue
897
- }
898
896
_ = 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
+ }
900
900
}
901
901
}
902
902
return anyPresent
@@ -2178,7 +2178,10 @@ func (d *Driver) CleanupStagingDirectory(stagingDirectory string) error {
2178
2178
d .stagingDirsLocksMutex .Lock ()
2179
2179
if lock , ok := d .stagingDirsLocks [parentStagingDir ]; ok {
2180
2180
delete (d .stagingDirsLocks , parentStagingDir )
2181
- lock .Unlock ()
2181
+ if err := lock .UnlockAndDelete (); err != nil {
2182
+ d .stagingDirsLocksMutex .Unlock ()
2183
+ return err
2184
+ }
2182
2185
}
2183
2186
d .stagingDirsLocksMutex .Unlock ()
2184
2187
@@ -2233,7 +2236,7 @@ func (d *Driver) ApplyDiffWithDiffer(options *graphdriver.ApplyDiffWithDifferOpt
2233
2236
return graphdriver.DriverWithDifferOutput {}, err
2234
2237
}
2235
2238
2236
- lock , err := lockfile . GetLockFile (filepath .Join (layerDir , stagingLockFile ))
2239
+ lock , err := staging_lockfile . TryLockPath (filepath .Join (layerDir , stagingLockFile ))
2237
2240
if err != nil {
2238
2241
return graphdriver.DriverWithDifferOutput {}, err
2239
2242
}
@@ -2242,13 +2245,14 @@ func (d *Driver) ApplyDiffWithDiffer(options *graphdriver.ApplyDiffWithDifferOpt
2242
2245
d .stagingDirsLocksMutex .Lock ()
2243
2246
delete (d .stagingDirsLocks , layerDir )
2244
2247
d .stagingDirsLocksMutex .Unlock ()
2245
- lock .Unlock ()
2248
+ if err := lock .UnlockAndDelete (); err != nil {
2249
+ errRet = errors .Join (errRet , err )
2250
+ }
2246
2251
}
2247
2252
}()
2248
2253
d .stagingDirsLocksMutex .Lock ()
2249
2254
d .stagingDirsLocks [layerDir ] = lock
2250
2255
d .stagingDirsLocksMutex .Unlock ()
2251
- lock .Lock ()
2252
2256
2253
2257
logrus .Debugf ("Applying differ in %s" , applyDir )
2254
2258
@@ -2274,15 +2278,17 @@ func (d *Driver) ApplyDiffWithDiffer(options *graphdriver.ApplyDiffWithDifferOpt
2274
2278
}
2275
2279
2276
2280
// 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 ) {
2278
2282
stagingDirectory := diffOutput .Target
2279
2283
parentStagingDir := filepath .Dir (stagingDirectory )
2280
2284
2281
2285
defer func () {
2282
2286
d .stagingDirsLocksMutex .Lock ()
2283
2287
if lock , ok := d .stagingDirsLocks [parentStagingDir ]; ok {
2284
2288
delete (d .stagingDirsLocks , parentStagingDir )
2285
- lock .Unlock ()
2289
+ if err := lock .UnlockAndDelete (); err != nil {
2290
+ errRet = errors .Join (errRet , err )
2291
+ }
2286
2292
}
2287
2293
d .stagingDirsLocksMutex .Unlock ()
2288
2294
}()
0 commit comments