2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-05-01 14:22:12 +00:00
|
|
|
"regexp"
|
2017-06-10 11:10:08 +00:00
|
|
|
"strconv"
|
2017-11-20 20:32:25 +00:00
|
|
|
"time"
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-06-10 11:10:08 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cache manages a local cache.
|
|
|
|
type Cache struct {
|
2020-07-28 08:13:11 +00:00
|
|
|
path string
|
|
|
|
Base string
|
|
|
|
Created bool
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const dirMode = 0700
|
2019-06-30 21:26:00 +00:00
|
|
|
const fileMode = 0644
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
func readVersion(dir string) (v uint, err error) {
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err := os.ReadFile(filepath.Join(dir, "version"))
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
2020-10-03 12:01:58 +00:00
|
|
|
return 0, errors.Wrap(err, "readVersion")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ver, err := strconv.ParseUint(string(buf), 10, 32)
|
|
|
|
if err != nil {
|
2020-10-03 12:01:58 +00:00
|
|
|
return 0, errors.Wrap(err, "readVersion")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return uint(ver), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheVersion = 1
|
|
|
|
|
|
|
|
var cacheLayoutPaths = map[restic.FileType]string{
|
2020-08-16 09:16:38 +00:00
|
|
|
restic.PackFile: "data",
|
2017-06-10 11:10:08 +00:00
|
|
|
restic.SnapshotFile: "snapshots",
|
|
|
|
restic.IndexFile: "index",
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:59:19 +00:00
|
|
|
const cachedirTagSignature = "Signature: 8a477f597d28d172789f06886806bc55\n"
|
|
|
|
|
|
|
|
func writeCachedirTag(dir string) error {
|
2018-07-08 10:05:12 +00:00
|
|
|
tagfile := filepath.Join(dir, "CACHEDIR.TAG")
|
2019-06-30 21:26:00 +00:00
|
|
|
f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode)
|
2017-09-11 16:59:19 +00:00
|
|
|
if err != nil {
|
2021-06-16 12:51:30 +00:00
|
|
|
if errors.Is(err, os.ErrExist) {
|
2017-09-11 16:59:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:01:58 +00:00
|
|
|
return errors.WithStack(err)
|
2017-09-11 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Create CACHEDIR.TAG at %v", dir)
|
|
|
|
if _, err := f.Write([]byte(cachedirTagSignature)); err != nil {
|
2017-10-25 16:03:55 +00:00
|
|
|
_ = f.Close()
|
2020-10-03 12:01:58 +00:00
|
|
|
return errors.WithStack(err)
|
2017-09-11 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 12:01:58 +00:00
|
|
|
return errors.WithStack(f.Close())
|
2017-09-11 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new cache for the repo ID at basedir. If basedir is the empty
|
|
|
|
// string, the default cache location (according to the XDG standard) is used.
|
2017-09-24 20:24:11 +00:00
|
|
|
//
|
|
|
|
// For partial files, the complete file is loaded and stored in the cache when
|
|
|
|
// performReadahead returns true.
|
2017-09-11 16:59:19 +00:00
|
|
|
func New(id string, basedir string) (c *Cache, err error) {
|
|
|
|
if basedir == "" {
|
2017-11-20 20:58:38 +00:00
|
|
|
basedir, err = DefaultDir()
|
2017-11-20 05:10:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-20 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 08:30:31 +00:00
|
|
|
err = fs.MkdirAll(basedir, dirMode)
|
2017-11-20 20:58:38 +00:00
|
|
|
if err != nil {
|
2020-10-03 12:01:58 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:59:19 +00:00
|
|
|
// create base dir and tag it as a cache directory
|
|
|
|
if err = writeCachedirTag(basedir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedir := filepath.Join(basedir, id)
|
2017-06-10 11:10:08 +00:00
|
|
|
debug.Log("using cache dir %v", cachedir)
|
|
|
|
|
2023-05-27 08:30:31 +00:00
|
|
|
created := false
|
2017-06-10 11:10:08 +00:00
|
|
|
v, err := readVersion(cachedir)
|
2023-05-27 08:30:31 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
if v > cacheVersion {
|
|
|
|
return nil, errors.New("cache version is newer")
|
|
|
|
}
|
|
|
|
// Update the timestamp so that we can detect old cache dirs.
|
|
|
|
err = updateTimestamp(cachedir)
|
2018-08-28 20:03:47 +00:00
|
|
|
if err != nil {
|
2023-05-27 08:30:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
// Create the repo cache dir. The parent exists, so Mkdir suffices.
|
|
|
|
err := fs.Mkdir(cachedir, dirMode)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
created = true
|
|
|
|
case errors.Is(err, os.ErrExist):
|
|
|
|
default:
|
2020-10-03 12:01:58 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-08-28 20:03:47 +00:00
|
|
|
}
|
2017-11-20 20:32:25 +00:00
|
|
|
|
2023-05-27 08:30:31 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Wrap(err, "readVersion")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v < cacheVersion {
|
2022-12-02 18:36:43 +00:00
|
|
|
err = os.WriteFile(filepath.Join(cachedir, "version"), []byte(fmt.Sprintf("%d", cacheVersion)), fileMode)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
2020-10-03 12:01:58 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range cacheLayoutPaths {
|
|
|
|
if err = fs.MkdirAll(filepath.Join(cachedir, p), dirMode); err != nil {
|
2020-10-03 12:01:58 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = &Cache{
|
2020-09-18 09:17:29 +00:00
|
|
|
path: cachedir,
|
2018-08-28 20:03:47 +00:00
|
|
|
Base: basedir,
|
|
|
|
Created: created,
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:32:25 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
// MaxCacheAge is the default age (30 days) after which cache directories are considered old.
|
|
|
|
const MaxCacheAge = 30 * 24 * time.Hour
|
2017-11-20 20:32:25 +00:00
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
func validCacheDirName(s string) bool {
|
2021-11-04 13:48:01 +00:00
|
|
|
r := regexp.MustCompile(`^[a-fA-F0-9]{64}$|^restic-check-cache-[0-9]+$`)
|
2019-06-30 20:34:47 +00:00
|
|
|
return r.MatchString(s)
|
2018-05-01 14:22:12 +00:00
|
|
|
}
|
2017-11-20 20:32:25 +00:00
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
// listCacheDirs returns the list of cache directories.
|
|
|
|
func listCacheDirs(basedir string) ([]os.FileInfo, error) {
|
2017-11-20 20:32:25 +00:00
|
|
|
f, err := fs.Open(basedir)
|
|
|
|
if err != nil {
|
2021-06-16 12:51:30 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
err = nil
|
|
|
|
}
|
2017-11-20 20:32:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := f.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]os.FileInfo, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
|
|
|
if !entry.IsDir() {
|
2017-11-20 20:32:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
if !validCacheDirName(entry.Name()) {
|
2017-11-20 20:32:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
result = append(result, entry)
|
2017-11-20 20:32:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// All returns a list of cache directories.
|
|
|
|
func All(basedir string) (dirs []os.FileInfo, err error) {
|
|
|
|
return listCacheDirs(basedir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OlderThan returns the list of cache directories older than max.
|
|
|
|
func OlderThan(basedir string, max time.Duration) ([]os.FileInfo, error) {
|
|
|
|
entries, err := listCacheDirs(basedir)
|
2017-11-20 20:32:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
var oldCacheDirs []os.FileInfo
|
|
|
|
for _, fi := range entries {
|
|
|
|
if !IsOld(fi.ModTime(), max) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCacheDirs = append(oldCacheDirs, fi)
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:32:25 +00:00
|
|
|
debug.Log("%d old cache dirs found", len(oldCacheDirs))
|
|
|
|
|
|
|
|
return oldCacheDirs, nil
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:12 +00:00
|
|
|
// Old returns a list of cache directories with a modification time of more
|
|
|
|
// than 30 days ago.
|
|
|
|
func Old(basedir string) ([]os.FileInfo, error) {
|
|
|
|
return OlderThan(basedir, MaxCacheAge)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOld returns true if the timestamp is considered old.
|
|
|
|
func IsOld(t time.Time, maxAge time.Duration) bool {
|
|
|
|
oldest := time.Now().Add(-maxAge)
|
|
|
|
return t.Before(oldest)
|
|
|
|
}
|
|
|
|
|
2017-06-10 11:10:08 +00:00
|
|
|
// Wrap returns a backend with a cache.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (c *Cache) Wrap(be backend.Backend) backend.Backend {
|
2017-09-25 13:21:16 +00:00
|
|
|
return newBackend(be, c)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
2017-09-11 19:37:10 +00:00
|
|
|
|
|
|
|
// BaseDir returns the base directory.
|
|
|
|
func (c *Cache) BaseDir() string {
|
|
|
|
return c.Base
|
|
|
|
}
|