Skip to content

Commit cb73550

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

File tree

10 files changed

+336
-7
lines changed

10 files changed

+336
-7
lines changed

drivers/aufs/aufs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,3 +781,13 @@ func (a *Driver) SupportsShifting() bool {
781781
func (a *Driver) Dedup(req graphdriver.DedupArgs) (graphdriver.DedupResult, error) {
782782
return graphdriver.DedupResult{}, nil
783783
}
784+
785+
func (a *Driver) InitTempDirectory() error {
786+
// InitTempDirectory not implemented
787+
return nil
788+
}
789+
790+
func (a *Driver) CleanupTempDirectory() error {
791+
// CleanupTempDirectory not implemented
792+
return nil
793+
}

drivers/btrfs/btrfs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,13 @@ func (d *Driver) AdditionalImageStores() []string {
678678
func (d *Driver) Dedup(req graphdriver.DedupArgs) (graphdriver.DedupResult, error) {
679679
return graphdriver.DedupResult{}, nil
680680
}
681+
682+
func (d *Driver) InitTempDirectory() error {
683+
// InitTempDirectory not implemented
684+
return nil
685+
}
686+
687+
func (d *Driver) CleanupTempDirectory() error {
688+
// CleanupTempDirectory not implemented
689+
return nil
690+
}

drivers/driver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ type ProtoDriver interface {
159159
AdditionalImageStores() []string
160160
// Dedup performs deduplication of the driver's storage.
161161
Dedup(DedupArgs) (DedupResult, error)
162+
163+
InitTempDirectory() error
164+
CleanupTempDirectory() error
162165
}
163166

164167
// DiffDriver is the interface to use to implement graph diffs

drivers/overlay/overlay.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strings"
1919
"sync"
2020
"syscall"
21+
"time"
2122

2223
graphdriver "github.com/containers/storage/drivers"
2324
"github.com/containers/storage/drivers/overlayutils"
@@ -34,6 +35,7 @@ import (
3435
"github.com/containers/storage/pkg/mount"
3536
"github.com/containers/storage/pkg/parsers"
3637
"github.com/containers/storage/pkg/system"
38+
"github.com/containers/storage/pkg/tempdir"
3739
"github.com/containers/storage/pkg/unshare"
3840
units "github.com/docker/go-units"
3941
digest "github.com/opencontainers/go-digest"
@@ -136,6 +138,8 @@ type Driver struct {
136138
stagingDirsLocks map[string]*lockfile.LockFile
137139

138140
supportsIDMappedMounts *bool
141+
142+
tempDirectory *tempdir.TempDir
139143
}
140144

141145
type additionalLayerStore struct {
@@ -1313,15 +1317,26 @@ func (d *Driver) Remove(id string) error {
13131317
dir := d.dir(id)
13141318
lid, err := os.ReadFile(path.Join(dir, "link"))
13151319
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)
1320+
if d.tempDirectory == nil {
1321+
if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil {
1322+
logrus.Debugf("Failed to remove link: %v", err)
1323+
}
1324+
} else {
1325+
if err := d.tempDirectory.Add(path.Join(d.home, linkDir, string(lid)), time.Now().Format("20060102-150405")); err != nil {
1326+
logrus.Debugf("Failed to Add to stage Directory link: %v", err)
1327+
}
13181328
}
13191329
}
13201330

13211331
d.releaseAdditionalLayerByID(id)
1322-
1323-
if err := system.EnsureRemoveAll(dir); err != nil && !os.IsNotExist(err) {
1324-
return err
1332+
if d.tempDirectory == nil {
1333+
if err := system.EnsureRemoveAll(dir); err != nil && !os.IsNotExist(err) {
1334+
return err
1335+
}
1336+
} else {
1337+
if err := d.tempDirectory.Add(dir, time.Now().Format("20060102-150405")); err != nil {
1338+
return fmt.Errorf("failed to add to stage directory: %w", err)
1339+
}
13251340
}
13261341
if d.quotaCtl != nil {
13271342
d.quotaCtl.ClearQuota(dir)
@@ -2771,3 +2786,22 @@ func (d *Driver) Dedup(req graphdriver.DedupArgs) (graphdriver.DedupResult, erro
27712786
}
27722787
return graphdriver.DedupResult{Deduped: r.Deduped}, nil
27732788
}
2789+
2790+
func (d *Driver) InitTempDirectory() error {
2791+
t, err := tempdir.NewTempDir(d.home)
2792+
if err != nil {
2793+
return err
2794+
}
2795+
d.tempDirectory = t
2796+
return nil
2797+
}
2798+
2799+
func (d *Driver) CleanupTempDirectory() error {
2800+
if d.tempDirectory != nil {
2801+
if err := d.tempDirectory.Cleanup(); err != nil {
2802+
logrus.Errorf("removing temp dir failed: %v", err)
2803+
}
2804+
d.tempDirectory = nil
2805+
}
2806+
return nil
2807+
}

drivers/vfs/driver.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
graphdriver "github.com/containers/storage/drivers"
1314
"github.com/containers/storage/internal/dedup"
@@ -17,6 +18,7 @@ import (
1718
"github.com/containers/storage/pkg/idtools"
1819
"github.com/containers/storage/pkg/parsers"
1920
"github.com/containers/storage/pkg/system"
21+
"github.com/containers/storage/pkg/tempdir"
2022
"github.com/opencontainers/selinux/go-selinux/label"
2123
"github.com/sirupsen/logrus"
2224
"github.com/vbatts/tar-split/tar/storage"
@@ -82,6 +84,8 @@ type Driver struct {
8284
naiveDiff graphdriver.DiffDriver
8385
updater graphdriver.LayerIDMapUpdater
8486
imageStore string
87+
88+
tempDirectory *tempdir.TempDir
8589
}
8690

8791
func (d *Driver) String() string {
@@ -241,6 +245,9 @@ func (d *Driver) dir(id string) string {
241245

242246
// Remove deletes the content from the directory for a given id.
243247
func (d *Driver) Remove(id string) error {
248+
if d.tempDirectory != nil {
249+
return d.tempDirectory.Add(d.dir(id), time.Now().Format("20060102-150405"))
250+
}
244251
return system.EnsureRemoveAll(d.dir(id))
245252
}
246253

@@ -365,3 +372,22 @@ func (d *Driver) Dedup(req graphdriver.DedupArgs) (graphdriver.DedupResult, erro
365372
}
366373
return graphdriver.DedupResult{Deduped: r.Deduped}, nil
367374
}
375+
376+
func (d *Driver) InitTempDirectory() error {
377+
t, err := tempdir.NewTempDir(d.home)
378+
if err != nil {
379+
return err
380+
}
381+
d.tempDirectory = t
382+
return nil
383+
}
384+
385+
func (d *Driver) CleanupTempDirectory() error {
386+
if d.tempDirectory != nil {
387+
if err := d.tempDirectory.Cleanup(); err != nil {
388+
logrus.Errorf("removing temp dir failed: %v", err)
389+
}
390+
d.tempDirectory = nil
391+
}
392+
return nil
393+
}

drivers/windows/windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,13 @@ func parseStorageOpt(storageOpt map[string]string) (*storageOptions, error) {
10141014
}
10151015
return &options, nil
10161016
}
1017+
1018+
func (d *Driver) InitTempDirectory() error {
1019+
// InitTempDirectory not implemented
1020+
return nil
1021+
}
1022+
1023+
func (d *Driver) CleanupTempDirectory() error {
1024+
// CleanupTempDirectory not implemented
1025+
return nil
1026+
}

drivers/zfs/zfs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,13 @@ func (d *Driver) AdditionalImageStores() []string {
516516
func (d *Driver) Dedup(req graphdriver.DedupArgs) (graphdriver.DedupResult, error) {
517517
return graphdriver.DedupResult{}, nil
518518
}
519+
520+
func (d *Driver) InitTempDirectory() error {
521+
// InitTempDirectory not implemented
522+
return nil
523+
}
524+
525+
func (d *Driver) CleanupTempDirectory() error {
526+
// CleanupTempDirectory not implemented
527+
return nil
528+
}

layers.go

Lines changed: 42 additions & 2 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,35 @@ 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+
tempDirectory *tempdir.TempDir
441+
}
442+
443+
func (r *layerStore) initTempDirs() error {
444+
t, err := tempdir.NewTempDir(r.rundir)
445+
if err != nil {
446+
return err
447+
}
448+
r.tempDirectory = t
449+
if err := r.driver.InitTempDirectory(); err != nil {
450+
return err
451+
}
452+
return nil
453+
}
454+
455+
func (r *layerStore) cleanTempDirs() error {
456+
// TODO: run cleanup in parallel
457+
if r.tempDirectory != nil {
458+
if err := r.tempDirectory.Cleanup(); err != nil {
459+
return err
460+
}
461+
r.tempDirectory = nil
462+
}
463+
464+
if err := r.driver.CleanupTempDirectory(); err != nil {
465+
return err
466+
}
467+
return nil
438468
}
439469

440470
func copyLayer(l *Layer) *Layer {
@@ -486,6 +516,10 @@ func (r *layerStore) startWritingWithReload(canReload bool) error {
486516
}
487517
}
488518

519+
if err := r.initTempDirs(); err != nil {
520+
return err
521+
}
522+
489523
succeeded = true
490524
return nil
491525
}
@@ -500,6 +534,10 @@ func (r *layerStore) startWriting() error {
500534
func (r *layerStore) stopWriting() {
501535
r.inProcessLock.Unlock()
502536
r.lockfile.Unlock()
537+
// Cleanup temporary directories needs to be done after unlocking the layer store.
538+
if err := r.cleanTempDirs(); err != nil {
539+
logrus.Errorf("Error cleaning up temporary directories: %v", err)
540+
}
503541
}
504542

505543
// startReadingWithReload makes sure the store is fresh if canReload, and locks it for reading.
@@ -1943,8 +1981,10 @@ func (r *layerStore) deleteInternal(id string) error {
19431981
if err := r.driver.Remove(id); err != nil && !errors.Is(err, os.ErrNotExist) {
19441982
return err
19451983
}
1946-
os.Remove(r.tspath(id))
1947-
os.RemoveAll(r.datadir(id))
1984+
_ = r.tempDirectory.Add(r.tspath(id), time.Now().Format("20060102-150405"))
1985+
// os.Remove(r.tspath(id))
1986+
_ = r.tempDirectory.Add(r.datadir(id), time.Now().Format("20060102-150405"))
1987+
// os.RemoveAll(r.datadir(id))
19481988
delete(r.byid, id)
19491989
for _, name := range layer.Names {
19501990
delete(r.byname, name)

0 commit comments

Comments
 (0)