@@ -26,6 +26,7 @@ import (
26
26
"github.com/containers/storage/pkg/stringid"
27
27
"github.com/containers/storage/pkg/system"
28
28
"github.com/containers/storage/pkg/tarlog"
29
+ "github.com/containers/storage/pkg/tempdir"
29
30
"github.com/containers/storage/pkg/truncindex"
30
31
"github.com/klauspost/pgzip"
31
32
digest "github.com/opencontainers/go-digest"
@@ -345,6 +346,8 @@ type rwLayerStore interface {
345
346
346
347
// Dedup deduplicates layers in the store.
347
348
dedup (drivers.DedupArgs ) (drivers.DedupResult , error )
349
+
350
+ CleanupTemporaryDirectories () error
348
351
}
349
352
350
353
type multipleLockFile struct {
@@ -435,6 +438,9 @@ type layerStore struct {
435
438
// FIXME: This field is only set when constructing layerStore, but locking rules of the driver
436
439
// interface itself are not documented here.
437
440
driver drivers.Driver
441
+
442
+ // This fled is read-only. Should be save to access without any locking.
443
+ tempDirectory * tempdir.TempDir
438
444
}
439
445
440
446
func copyLayer (l * Layer ) * Layer {
@@ -794,6 +800,12 @@ func (r *layerStore) load(lockedForWriting bool) (bool, error) {
794
800
layers := []* Layer {}
795
801
ids := make (map [string ]* Layer )
796
802
803
+ if r .lockfile .IsReadWrite () {
804
+ if err := tempdir .RecoverStaleDirs (r .tempDirectory .RootDir ); err != nil {
805
+ return false , err
806
+ }
807
+ }
808
+
797
809
for locationIndex := range numLayerLocationIndex {
798
810
location := layerLocationFromIndex (locationIndex )
799
811
rpath := r .jsonPath [locationIndex ]
@@ -936,6 +948,11 @@ func (r *layerStore) load(lockedForWriting bool) (bool, error) {
936
948
for _ , layer := range layersToDelete {
937
949
logrus .Warnf ("Found incomplete layer %q, deleting it" , layer .ID )
938
950
err := r .deleteInternal (layer .ID )
951
+ defer func () {
952
+ if err := r .CleanupTemporaryDirectories (); err != nil {
953
+ logrus .Errorf ("Error cleaning up temporary directories: %v" , err )
954
+ }
955
+ }()
939
956
if err != nil {
940
957
// Don't return the error immediately, because deleteInternal does not saveLayers();
941
958
// Even if deleting one incomplete layer fails, call saveLayers() so that other possible successfully
@@ -1164,7 +1181,8 @@ func (s *store) newLayerStore(rundir, layerdir, imagedir string, driver drivers.
1164
1181
byname : make (map [string ]* Layer ),
1165
1182
bymount : make (map [string ]* Layer ),
1166
1183
1167
- driver : driver ,
1184
+ driver : driver ,
1185
+ tempDirectory : s .tempDirectory ,
1168
1186
}
1169
1187
if err := rlstore .startWritingWithReload (false ); err != nil {
1170
1188
return nil , err
@@ -1919,7 +1937,17 @@ func layerHasIncompleteFlag(layer *Layer) bool {
1919
1937
return false
1920
1938
}
1921
1939
1940
+ func (r * layerStore ) CleanupTemporaryDirectories () error {
1941
+ err := r .tempDirectory .Cleanup ()
1942
+ if errDriver := r .driver .Cleanup (); errDriver != nil {
1943
+ return errors .Join (err , errDriver )
1944
+ }
1945
+ return err
1946
+ }
1947
+
1922
1948
// Requires startWriting.
1949
+ // Caller MUST run CleanupTemporaryDirectories() after this. Ideally outside of
1950
+ // the startWriting.
1923
1951
func (r * layerStore ) deleteInternal (id string ) error {
1924
1952
if ! r .lockfile .IsReadWrite () {
1925
1953
return fmt .Errorf ("not allowed to delete layers at %q: %w" , r .layerdir , ErrStoreIsReadOnly )
@@ -1943,8 +1971,10 @@ func (r *layerStore) deleteInternal(id string) error {
1943
1971
if err := r .driver .Remove (id ); err != nil && ! errors .Is (err , os .ErrNotExist ) {
1944
1972
return err
1945
1973
}
1946
- os .Remove (r .tspath (id ))
1947
- os .RemoveAll (r .datadir (id ))
1974
+ _ = r .tempDirectory .Add (r .tspath (id ))
1975
+ // os.Remove(r.tspath(id))
1976
+ _ = r .tempDirectory .Add (r .datadir (id ))
1977
+ // os.RemoveAll(r.datadir(id))
1948
1978
delete (r .byid , id )
1949
1979
for _ , name := range layer .Names {
1950
1980
delete (r .byname , name )
@@ -1988,6 +2018,8 @@ func (r *layerStore) deleteInDigestMap(id string) {
1988
2018
}
1989
2019
1990
2020
// Requires startWriting.
2021
+ // Caller MUST run CleanupTemporaryDirectories() after this. Ideally outside of
2022
+ // the startWriting.
1991
2023
func (r * layerStore ) Delete (id string ) error {
1992
2024
layer , ok := r .lookup (id )
1993
2025
if ! ok {
0 commit comments