forked from TrueCloudLab/restic
cache: Add detection code for old cache dirs
This commit is contained in:
parent
06bd606d85
commit
fa893ee477
3 changed files with 74 additions and 3 deletions
|
@ -353,13 +353,21 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cache, err := cache.New(s.Config().ID, opts.CacheDir)
|
c, err := cache.New(s.Config().ID, opts.CacheDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Warnf("unable to open cache: %v\n", err)
|
Warnf("unable to open cache: %v\n", err)
|
||||||
} else {
|
return s, nil
|
||||||
s.UseCache(cache)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldCacheDirs, err := cache.Old(c.Base)
|
||||||
|
if err != nil {
|
||||||
|
Warnf("unable to find old cache directories: %v", err)
|
||||||
|
} else {
|
||||||
|
Verbosef("found %d old cache directories in %v remove them with 'restic cache --cleanup'\n",
|
||||||
|
len(oldCacheDirs), c.Base)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.UseCache(c)
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
53
internal/cache/cache.go
vendored
53
internal/cache/cache.go
vendored
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
|
@ -107,6 +108,11 @@ func New(id string, basedir string) (c *Cache, err error) {
|
||||||
return nil, errors.New("cache version is newer")
|
return nil, errors.New("cache version is newer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = updateTimestamp(cachedir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// create the repo cache dir if it does not exist yet
|
// create the repo cache dir if it does not exist yet
|
||||||
if err = fs.MkdirAll(cachedir, dirMode); err != nil {
|
if err = fs.MkdirAll(cachedir, dirMode); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -137,6 +143,53 @@ func New(id string, basedir string) (c *Cache, err error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updateTimestamp sets the modification timestamp (mtime and atime) for the
|
||||||
|
// directory d to the current time.
|
||||||
|
func updateTimestamp(d string) error {
|
||||||
|
t := time.Now()
|
||||||
|
return fs.Chtimes(d, t, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxCacheAge = 30 * 24 * time.Hour
|
||||||
|
|
||||||
|
// Old returns a list of cache directories with a modification time of more
|
||||||
|
// than 30 days ago.
|
||||||
|
func Old(basedir string) ([]string, error) {
|
||||||
|
var oldCacheDirs []string
|
||||||
|
|
||||||
|
f, err := fs.Open(basedir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := f.Readdir(-1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldest := time.Now().Add(-maxCacheAge)
|
||||||
|
for _, fi := range entries {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fi.ModTime().Before(oldest) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
oldCacheDirs = append(oldCacheDirs, fi.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
debug.Log("%d old cache dirs found", len(oldCacheDirs))
|
||||||
|
|
||||||
|
return oldCacheDirs, nil
|
||||||
|
}
|
||||||
|
|
||||||
// errNoSuchFile is returned when a file is not cached.
|
// errNoSuchFile is returned when a file is not cached.
|
||||||
type errNoSuchFile struct {
|
type errNoSuchFile struct {
|
||||||
Type string
|
Type string
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// File is an open file on a file system.
|
// File is an open file on a file system.
|
||||||
|
@ -120,3 +121,12 @@ func RemoveIfExists(filename string) error {
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Chtimes changes the access and modification times of the named file,
|
||||||
|
// similar to the Unix utime() or utimes() functions.
|
||||||
|
//
|
||||||
|
// The underlying filesystem may truncate or round the values to a less
|
||||||
|
// precise time unit. If there is an error, it will be of type *PathError.
|
||||||
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
||||||
|
return os.Chtimes(fixpath(name), atime, mtime)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue