2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Load returns a reader that yields the contents of the file with the
|
|
|
|
// given handle. rd must be closed after use. If an error is returned, the
|
|
|
|
// ReadCloser is nil.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser, 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) {
|
|
|
|
return nil, errors.New("cannot be cached")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := fs.Open(c.filename(h))
|
|
|
|
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-24 21:11:01 +00:00
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
2020-10-03 12:01:58 +00:00
|
|
|
return nil, 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()
|
2020-03-02 17:27:52 +00:00
|
|
|
_ = c.remove(h)
|
2017-10-05 17:30:56 +00:00
|
|
|
return nil, errors.Errorf("cached file %v is truncated, removing", 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()
|
2020-03-02 17:27:52 +00:00
|
|
|
_ = c.remove(h)
|
2018-08-11 20:47:01 +00:00
|
|
|
return nil, errors.Errorf("cached file %v is too small, removing", h)
|
|
|
|
}
|
|
|
|
|
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()
|
2017-06-10 11:10:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 18:03:36 +00:00
|
|
|
if length <= 0 {
|
|
|
|
return f, nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
2023-01-03 18:03:36 +00:00
|
|
|
return backend.LimitReadCloser(f, int64(length)), nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save saves a file in the cache.
|
2023-10-01 09:40:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a file. When the file is not cache, no error is returned.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (c *Cache) remove(h backend.Handle) error {
|
2017-06-10 11:10:08 +00:00
|
|
|
if !c.Has(h) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.Remove(c.filename(h))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
if err = fs.Remove(c.filename(backend.Handle{Type: t, Name: id.String()})); err != nil {
|
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
|
|
|
}
|