2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2024-05-09 16:24:28 +00:00
|
|
|
"fmt"
|
2017-06-10 11:10:08 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-06-24 13:22:51 +00:00
|
|
|
"runtime"
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-01-03 18:03:36 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2024-04-25 19:20:23 +00:00
|
|
|
"github.com/restic/restic/internal/backend/util"
|
2022-06-12 12:48:30 +00:00
|
|
|
"github.com/restic/restic/internal/crypto"
|
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"
|
|
|
|
)
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (c *Cache) filename(h backend.Handle) string {
|
2017-06-10 11:10:08 +00:00
|
|
|
if len(h.Name) < 2 {
|
|
|
|
panic("Name is empty or too short")
|
|
|
|
}
|
|
|
|
subdir := h.Name[:2]
|
2020-09-18 09:17:29 +00:00
|
|
|
return filepath.Join(c.path, cacheLayoutPaths[h.Type], subdir, h.Name)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (c *Cache) canBeCached(t backend.FileType) bool {
|
2017-06-10 11:10:08 +00:00
|
|
|
if c == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-03 07:54:55 +00:00
|
|
|
_, ok := cacheLayoutPaths[t]
|
|
|
|
return ok
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:07:21 +00:00
|
|
|
// load returns a reader that yields the contents of the file with the
|
2017-06-10 11:10:08 +00:00
|
|
|
// given handle. rd must be closed after use. If an error is returned, the
|
2024-05-09 16:21:53 +00:00
|
|
|
// ReadCloser is nil. The bool return value indicates whether the requested
|
|
|
|
// file exists in the cache. It can be true even when no reader is returned
|
|
|
|
// because length or offset are out of bounds
|
|
|
|
func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser, bool, error) {
|
2022-08-19 18:59:06 +00:00
|
|
|
debug.Log("Load(%v, %v, %v) from cache", h, length, offset)
|
2017-06-10 11:10:08 +00:00
|
|
|
if !c.canBeCached(h.Type) {
|
2024-05-09 16:21:53 +00:00
|
|
|
return nil, false, errors.New("cannot be cached")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := fs.Open(c.filename(h))
|
|
|
|
if err != nil {
|
2024-05-09 16:21:53 +00:00
|
|
|
return nil, false, errors.WithStack(err)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 21:11:01 +00:00
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
2024-05-09 16:21:53 +00:00
|
|
|
return nil, true, errors.WithStack(err)
|
2017-09-24 21:11:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 12:32:06 +00:00
|
|
|
size := fi.Size()
|
|
|
|
if size <= int64(crypto.CiphertextLength(0)) {
|
2017-09-24 21:11:01 +00:00
|
|
|
_ = f.Close()
|
2024-05-09 16:21:53 +00:00
|
|
|
return nil, true, errors.Errorf("cached file %v is truncated", h)
|
2017-09-24 21:11:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 12:32:06 +00:00
|
|
|
if size < offset+int64(length) {
|
2018-08-11 20:47:01 +00:00
|
|
|
_ = f.Close()
|
2024-05-09 16:21:53 +00:00
|
|
|
return nil, true, errors.Errorf("cached file %v is too short", h)
|
2018-08-11 20:47:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 11:10:08 +00:00
|
|
|
if offset > 0 {
|
|
|
|
if _, err = f.Seek(offset, io.SeekStart); err != nil {
|
2017-10-25 16:03:55 +00:00
|
|
|
_ = f.Close()
|
2024-05-09 16:21:53 +00:00
|
|
|
return nil, true, err
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 18:03:36 +00:00
|
|
|
if length <= 0 {
|
2024-05-09 16:21:53 +00:00
|
|
|
return f, true, nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
2024-05-09 16:21:53 +00:00
|
|
|
return util.LimitReadCloser(f, int64(length)), true, nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:07:21 +00:00
|
|
|
// save saves a file in the cache.
|
|
|
|
func (c *Cache) save(h backend.Handle, rd io.Reader) error {
|
2017-06-10 11:10:08 +00:00
|
|
|
debug.Log("Save to cache: %v", h)
|
|
|
|
if rd == nil {
|
|
|
|
return errors.New("Save() called with nil reader")
|
|
|
|
}
|
2020-06-24 13:22:51 +00:00
|
|
|
if !c.canBeCached(h.Type) {
|
|
|
|
return errors.New("cannot be cached")
|
|
|
|
}
|
|
|
|
|
|
|
|
finalname := c.filename(h)
|
|
|
|
dir := filepath.Dir(finalname)
|
|
|
|
err := fs.Mkdir(dir, 0700)
|
2021-06-16 12:51:30 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrExist) {
|
2020-06-24 13:22:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2020-06-24 13:22:51 +00:00
|
|
|
// First save to a temporary location. This allows multiple concurrent
|
|
|
|
// restics to use a single cache dir.
|
2022-12-02 18:36:43 +00:00
|
|
|
f, err := os.CreateTemp(dir, "tmp-")
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-05 18:40:02 +00:00
|
|
|
n, err := io.Copy(f, rd)
|
|
|
|
if err != nil {
|
2017-06-10 11:10:08 +00:00
|
|
|
_ = f.Close()
|
2020-06-24 13:22:51 +00:00
|
|
|
_ = fs.Remove(f.Name())
|
2017-06-10 11:10:08 +00:00
|
|
|
return errors.Wrap(err, "Copy")
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:48:30 +00:00
|
|
|
if n <= int64(crypto.CiphertextLength(0)) {
|
2017-10-05 18:40:02 +00:00
|
|
|
_ = f.Close()
|
2020-06-24 13:22:51 +00:00
|
|
|
_ = fs.Remove(f.Name())
|
2018-11-25 13:18:02 +00:00
|
|
|
debug.Log("trying to cache truncated file %v, removing", h)
|
|
|
|
return nil
|
2017-10-05 18:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:22:51 +00:00
|
|
|
// Close, then rename. Windows doesn't like the reverse order.
|
2017-06-10 11:10:08 +00:00
|
|
|
if err = f.Close(); err != nil {
|
2020-06-24 13:22:51 +00:00
|
|
|
_ = fs.Remove(f.Name())
|
2020-10-03 12:01:58 +00:00
|
|
|
return errors.WithStack(err)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:22:51 +00:00
|
|
|
err = fs.Rename(f.Name(), finalname)
|
|
|
|
if err != nil {
|
|
|
|
_ = fs.Remove(f.Name())
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" && errors.Is(err, os.ErrPermission) {
|
|
|
|
// On Windows, renaming over an existing file is ok
|
|
|
|
// (os.Rename is MoveFileExW with MOVEFILE_REPLACE_EXISTING
|
|
|
|
// since Go 1.5), but not when someone else has the file open.
|
|
|
|
//
|
|
|
|
// When we get Access denied, we assume that's the case
|
|
|
|
// and the other process has written the desired contents to f.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.WithStack(err)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 13:26:41 +00:00
|
|
|
func (c *Cache) Forget(h backend.Handle) error {
|
2024-05-09 16:24:28 +00:00
|
|
|
h.IsMetadata = false
|
|
|
|
|
|
|
|
if _, ok := c.forgotten.Load(h); ok {
|
|
|
|
// Delete a file at most once while restic runs.
|
|
|
|
// This prevents repeatedly caching and forgetting broken files
|
|
|
|
return fmt.Errorf("circuit breaker prevents repeated deletion of cached file %v", h)
|
|
|
|
}
|
|
|
|
|
|
|
|
removed, err := c.remove(h)
|
|
|
|
if removed {
|
|
|
|
c.forgotten.Store(h, struct{}{})
|
|
|
|
}
|
|
|
|
return err
|
2024-05-09 13:26:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:07:21 +00:00
|
|
|
// remove deletes a file. When the file is not cached, no error is returned.
|
2024-05-09 16:24:28 +00:00
|
|
|
func (c *Cache) remove(h backend.Handle) (bool, error) {
|
2024-05-09 11:07:21 +00:00
|
|
|
if !c.canBeCached(h.Type) {
|
2024-05-09 16:24:28 +00:00
|
|
|
return false, nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:07:21 +00:00
|
|
|
err := fs.Remove(c.filename(h))
|
2024-05-09 16:24:28 +00:00
|
|
|
removed := err == nil
|
2024-05-09 11:07:21 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
err = nil
|
|
|
|
}
|
2024-05-09 16:24:28 +00:00
|
|
|
return removed, err
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes all files of type t from the cache that are not contained in
|
|
|
|
// the set valid.
|
|
|
|
func (c *Cache) Clear(t restic.FileType, valid restic.IDSet) error {
|
|
|
|
debug.Log("Clearing cache for %v: %v valid files", t, len(valid))
|
|
|
|
if !c.canBeCached(t) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := c.list(t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for id := range list {
|
|
|
|
if valid.Has(id) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-10 17:17:25 +00:00
|
|
|
// ignore ErrNotExist to gracefully handle multiple processes running Clear() concurrently
|
|
|
|
if err = fs.Remove(c.filename(backend.Handle{Type: t, Name: id.String()})); err != nil && !errors.Is(err, os.ErrNotExist) {
|
2017-06-10 11:10:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFile(fi os.FileInfo) bool {
|
|
|
|
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// list returns a list of all files of type T in the cache.
|
|
|
|
func (c *Cache) list(t restic.FileType) (restic.IDSet, error) {
|
|
|
|
if !c.canBeCached(t) {
|
|
|
|
return nil, errors.New("cannot be cached")
|
|
|
|
}
|
|
|
|
|
|
|
|
list := restic.NewIDSet()
|
2020-09-18 09:17:29 +00:00
|
|
|
dir := filepath.Join(c.path, cacheLayoutPaths[t])
|
2017-06-10 11:10:08 +00:00
|
|
|
err := filepath.Walk(dir, func(name string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Walk")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isFile(fi) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := restic.ParseID(filepath.Base(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Insert(id)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns true if the file is cached.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (c *Cache) Has(h backend.Handle) bool {
|
2017-06-10 11:10:08 +00:00
|
|
|
if !c.canBeCached(h.Type) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := fs.Stat(c.filename(h))
|
2019-06-30 20:34:47 +00:00
|
|
|
return err == nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|