cache: Add detection code for old cache dirs

This commit is contained in:
Alexander Neumann
2017-11-20 21:32:25 +01:00
parent 06bd606d85
commit fa893ee477
3 changed files with 74 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strconv"
"time"
"github.com/pkg/errors"
"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")
}
err = updateTimestamp(cachedir)
if err != nil {
return nil, err
}
// create the repo cache dir if it does not exist yet
if err = fs.MkdirAll(cachedir, dirMode); err != nil {
return nil, err
@@ -137,6 +143,53 @@ func New(id string, basedir string) (c *Cache, err error) {
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.
type errNoSuchFile struct {
Type string