2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

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

@ -353,13 +353,21 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
return s, nil
}
cache, err := cache.New(s.Config().ID, opts.CacheDir)
c, err := cache.New(s.Config().ID, opts.CacheDir)
if err != nil {
Warnf("unable to open cache: %v\n", err)
} else {
s.UseCache(cache)
return s, nil
}
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
}

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

View File

@ -4,6 +4,7 @@ import (
"io"
"os"
"path/filepath"
"time"
)
// File is an open file on a file system.
@ -120,3 +121,12 @@ func RemoveIfExists(filename string) error {
}
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)
}