Skip to content

Commit ce89bc4

Browse files
committed
Prototype temporary directory
Signed-off-by: Jan Rodák <[email protected]>
1 parent 36d562d commit ce89bc4

File tree

6 files changed

+378
-12
lines changed

6 files changed

+378
-12
lines changed

drivers/overlay/overlay.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/containers/storage/pkg/mount"
3535
"github.com/containers/storage/pkg/parsers"
3636
"github.com/containers/storage/pkg/system"
37+
"github.com/containers/storage/pkg/tempdir"
3738
"github.com/containers/storage/pkg/unshare"
3839
units "github.com/docker/go-units"
3940
digest "github.com/opencontainers/go-digest"
@@ -82,6 +83,7 @@ const (
8283
const (
8384
linkDir = "l"
8485
stagingDir = "staging"
86+
tempDir = "tmp"
8587
lowerFile = "lower"
8688
maxDepth = 500
8789

@@ -136,6 +138,9 @@ type Driver struct {
136138
stagingDirsLocks map[string]*lockfile.LockFile
137139

138140
supportsIDMappedMounts *bool
141+
142+
// This fled is read-only. Should be save to access without any locking.
143+
tempDirectory *tempdir.TempDir
139144
}
140145

141146
type additionalLayerStore struct {
@@ -458,6 +463,12 @@ func Init(home string, options graphdriver.Options) (graphdriver.Driver, error)
458463
return nil, fmt.Errorf("storage option overlay.size and overlay.inodes only supported for backingFS XFS. Found %v", backingFs)
459464
}
460465

466+
t, err := tempdir.NewTempDir(filepath.Join(d.home, tempDir))
467+
if err != nil {
468+
return nil, err
469+
}
470+
d.tempDirectory = t
471+
461472
logrus.Debugf("backingFs=%s, projectQuotaSupported=%v, useNativeDiff=%v, usingMetacopy=%v", backingFs, projectQuotaSupported, !d.useNaiveDiff(), d.usingMetacopy)
462473

463474
return d, nil
@@ -862,6 +873,9 @@ func (d *Driver) Metadata(id string) (map[string]string, error) {
862873
// the storage is being shutdown. The only state created by the driver
863874
// is the bind mount on the home directory.
864875
func (d *Driver) Cleanup() error {
876+
if err := d.tempDirectory.Cleanup(); err != nil {
877+
return err
878+
}
865879
anyPresent := d.pruneStagingDirectories()
866880
if anyPresent {
867881
return nil
@@ -1313,16 +1327,17 @@ func (d *Driver) Remove(id string) error {
13131327
dir := d.dir(id)
13141328
lid, err := os.ReadFile(path.Join(dir, "link"))
13151329
if err == nil {
1316-
if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil {
1317-
logrus.Debugf("Failed to remove link: %v", err)
1330+
if err := d.tempDirectory.Add(path.Join(d.home, linkDir, string(lid))); err != nil {
1331+
logrus.Debugf("Failed to Add to stage Directory link: %v", err)
13181332
}
13191333
}
13201334

13211335
d.releaseAdditionalLayerByID(id)
13221336

1323-
if err := system.EnsureRemoveAll(dir); err != nil && !os.IsNotExist(err) {
1324-
return err
1337+
if err := d.tempDirectory.Add(dir); err != nil {
1338+
return fmt.Errorf("failed to add to stage directory: %w", err)
13251339
}
1340+
13261341
if d.quotaCtl != nil {
13271342
d.quotaCtl.ClearQuota(dir)
13281343
if d.imageStore != "" {
@@ -1358,8 +1373,8 @@ func (d *Driver) recreateSymlinks() error {
13581373
// Check that for each layer, there's a link in "l" with the name in
13591374
// the layer's "link" file that points to the layer's "diff" directory.
13601375
for _, dir := range dirs {
1361-
// Skip over the linkDir and anything that is not a directory
1362-
if dir.Name() == linkDir || !dir.IsDir() {
1376+
// Skip over the linkDir and anything that is not a directory or tempDir
1377+
if dir.Name() == linkDir || !dir.IsDir() || dir.Name() == tempDir {
13631378
continue
13641379
}
13651380
// Read the "link" file under each layer to get the name of the symlink
@@ -2027,7 +2042,7 @@ func (d *Driver) ListLayers() ([]string, error) {
20272042
for _, entry := range entries {
20282043
id := entry.Name()
20292044
switch id {
2030-
case linkDir, stagingDir, quota.BackingFsBlockDeviceLink, mountProgramFlagFile:
2045+
case linkDir, stagingDir, tempDir, quota.BackingFsBlockDeviceLink, mountProgramFlagFile:
20312046
// expected, but not a layer. skip it
20322047
continue
20332048
default:

drivers/vfs/driver.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/containers/storage/pkg/idtools"
1818
"github.com/containers/storage/pkg/parsers"
1919
"github.com/containers/storage/pkg/system"
20+
"github.com/containers/storage/pkg/tempdir"
2021
"github.com/opencontainers/selinux/go-selinux/label"
2122
"github.com/sirupsen/logrus"
2223
"github.com/vbatts/tar-split/tar/storage"
@@ -67,6 +68,12 @@ func Init(home string, options graphdriver.Options) (graphdriver.Driver, error)
6768
d.updater = graphdriver.NewNaiveLayerIDMapUpdater(d)
6869
d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, d.updater)
6970

71+
t, err := tempdir.NewTempDir(d.home)
72+
if err != nil {
73+
return nil, err
74+
}
75+
d.tempDirectory = t
76+
7077
return d, nil
7178
}
7279

@@ -82,6 +89,9 @@ type Driver struct {
8289
naiveDiff graphdriver.DiffDriver
8390
updater graphdriver.LayerIDMapUpdater
8491
imageStore string
92+
93+
// This fled is read-only. Should be save to access without any locking.
94+
tempDirectory *tempdir.TempDir
8595
}
8696

8797
func (d *Driver) String() string {
@@ -100,7 +110,8 @@ func (d *Driver) Metadata(id string) (map[string]string, error) {
100110

101111
// Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
102112
func (d *Driver) Cleanup() error {
103-
return nil
113+
// TODO: UPDATE comment
114+
return d.tempDirectory.Cleanup()
104115
}
105116

106117
type fileGetNilCloser struct {
@@ -241,7 +252,7 @@ func (d *Driver) dir(id string) string {
241252

242253
// Remove deletes the content from the directory for a given id.
243254
func (d *Driver) Remove(id string) error {
244-
return system.EnsureRemoveAll(d.dir(id))
255+
return d.tempDirectory.Add(d.dir(id))
245256
}
246257

247258
// Get returns the directory for the given id.

layers.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/containers/storage/pkg/stringid"
2727
"github.com/containers/storage/pkg/system"
2828
"github.com/containers/storage/pkg/tarlog"
29+
"github.com/containers/storage/pkg/tempdir"
2930
"github.com/containers/storage/pkg/truncindex"
3031
"github.com/klauspost/pgzip"
3132
digest "github.com/opencontainers/go-digest"
@@ -435,6 +436,9 @@ type layerStore struct {
435436
// FIXME: This field is only set when constructing layerStore, but locking rules of the driver
436437
// interface itself are not documented here.
437438
driver drivers.Driver
439+
440+
// This fled is read-only. Should be save to access without any locking.
441+
tempDirectory *tempdir.TempDir
438442
}
439443

440444
func copyLayer(l *Layer) *Layer {
@@ -1164,7 +1168,8 @@ func (s *store) newLayerStore(rundir, layerdir, imagedir string, driver drivers.
11641168
byname: make(map[string]*Layer),
11651169
bymount: make(map[string]*Layer),
11661170

1167-
driver: driver,
1171+
driver: driver,
1172+
tempDirectory: s.tempDirectory,
11681173
}
11691174
if err := rlstore.startWritingWithReload(false); err != nil {
11701175
return nil, err
@@ -1943,8 +1948,10 @@ func (r *layerStore) deleteInternal(id string) error {
19431948
if err := r.driver.Remove(id); err != nil && !errors.Is(err, os.ErrNotExist) {
19441949
return err
19451950
}
1946-
os.Remove(r.tspath(id))
1947-
os.RemoveAll(r.datadir(id))
1951+
_ = r.tempDirectory.Add(r.tspath(id))
1952+
// os.Remove(r.tspath(id))
1953+
_ = r.tempDirectory.Add(r.datadir(id))
1954+
// os.RemoveAll(r.datadir(id))
19481955
delete(r.byid, id)
19491956
for _, name := range layer.Names {
19501957
delete(r.byname, name)

0 commit comments

Comments
 (0)