2015-02-21 23:09:57 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2015-04-29 02:09:32 +00:00
|
|
|
"errors"
|
2015-02-21 23:09:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-03-14 11:30:47 +00:00
|
|
|
"strings"
|
2015-02-21 23:09:57 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/backend"
|
2015-03-14 11:10:08 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2015-04-26 12:46:15 +00:00
|
|
|
"github.com/restic/restic/server"
|
2015-02-21 23:09:57 +00:00
|
|
|
)
|
|
|
|
|
2015-05-02 15:01:31 +00:00
|
|
|
// Cache is used to locally cache items from a server.
|
2015-02-21 23:09:57 +00:00
|
|
|
type Cache struct {
|
|
|
|
base string
|
|
|
|
}
|
|
|
|
|
2015-05-03 14:43:27 +00:00
|
|
|
func NewCache(s *server.Server) (*Cache, error) {
|
2015-04-30 01:30:53 +00:00
|
|
|
cacheDir, err := getCacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 15:37:12 +00:00
|
|
|
basedir := filepath.Join(cacheDir, s.Config.ID)
|
2015-03-14 11:10:08 +00:00
|
|
|
debug.Log("Cache.New", "opened cache at %v", basedir)
|
|
|
|
|
|
|
|
return &Cache{base: basedir}, nil
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 13:51:40 +00:00
|
|
|
// Has checks if the local cache has the id.
|
2015-03-09 21:26:39 +00:00
|
|
|
func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error) {
|
|
|
|
filename, err := c.filename(t, subtype, id)
|
2015-02-21 23:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
fd, err := os.Open(filename)
|
|
|
|
defer fd.Close()
|
2015-04-30 01:30:53 +00:00
|
|
|
|
2015-02-21 23:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2015-03-14 11:30:47 +00:00
|
|
|
debug.Log("Cache.Has", "test for file %v: not cached", filename)
|
2015-02-21 23:09:57 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-03-14 11:30:47 +00:00
|
|
|
debug.Log("Cache.Has", "test for file %v: error %v", filename, err)
|
2015-02-21 23:09:57 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-03-14 11:30:47 +00:00
|
|
|
debug.Log("Cache.Has", "test for file %v: is cached", filename)
|
2015-02-21 23:09:57 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-05-02 15:01:31 +00:00
|
|
|
// Store returns an io.WriteCloser that is used to save new information to the
|
|
|
|
// cache. The returned io.WriteCloser must be closed by the caller after all
|
|
|
|
// data has been written.
|
2015-03-09 21:26:39 +00:00
|
|
|
func (c *Cache) Store(t backend.Type, subtype string, id backend.ID) (io.WriteCloser, error) {
|
|
|
|
filename, err := c.filename(t, subtype, id)
|
2015-02-21 23:09:57 +00:00
|
|
|
if err != nil {
|
2015-03-09 21:26:39 +00:00
|
|
|
return nil, err
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dirname := filepath.Dir(filename)
|
|
|
|
err = os.MkdirAll(dirname, 0700)
|
|
|
|
if err != nil {
|
2015-03-09 21:26:39 +00:00
|
|
|
return nil, err
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
if err != nil {
|
2015-03-14 11:30:47 +00:00
|
|
|
debug.Log("Cache.Store", "error creating file %v: %v", filename, err)
|
2015-03-09 21:26:39 +00:00
|
|
|
return nil, err
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 11:30:47 +00:00
|
|
|
debug.Log("Cache.Store", "created file %v", filename)
|
2015-03-09 21:26:39 +00:00
|
|
|
return file, nil
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 15:01:31 +00:00
|
|
|
// Load returns information from the cache. The returned io.ReadCloser must be
|
|
|
|
// closed by the caller.
|
2015-03-09 21:26:39 +00:00
|
|
|
func (c *Cache) Load(t backend.Type, subtype string, id backend.ID) (io.ReadCloser, error) {
|
|
|
|
filename, err := c.filename(t, subtype, id)
|
2015-02-21 23:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Open(filename)
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:30:53 +00:00
|
|
|
func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error {
|
2015-03-14 11:30:47 +00:00
|
|
|
filename, err := c.filename(t, subtype, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(filename)
|
2015-04-30 01:30:53 +00:00
|
|
|
debug.Log("Cache.purge", "Remove file %v: %v", filename, err)
|
2015-03-14 11:30:47 +00:00
|
|
|
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:51:40 +00:00
|
|
|
// Clear removes information from the cache that isn't present in the server any more.
|
2015-04-26 12:46:15 +00:00
|
|
|
func (c *Cache) Clear(s *server.Server) error {
|
2015-05-02 13:49:14 +00:00
|
|
|
list, err := c.list(backend.Snapshot)
|
2015-03-14 11:30:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range list {
|
|
|
|
debug.Log("Cache.Clear", "found entry %v", entry)
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
if ok, err := s.Test(backend.Snapshot, entry.ID.String()); !ok || err != nil {
|
2015-03-14 11:30:47 +00:00
|
|
|
debug.Log("Cache.Clear", "snapshot %v doesn't exist any more, removing %v", entry.ID, entry)
|
|
|
|
|
2015-04-30 01:30:53 +00:00
|
|
|
err = c.purge(backend.Snapshot, entry.Subtype, entry.ID)
|
2015-03-14 11:30:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:49:14 +00:00
|
|
|
type cacheEntry struct {
|
2015-03-14 11:30:47 +00:00
|
|
|
ID backend.ID
|
|
|
|
Subtype string
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:49:14 +00:00
|
|
|
func (c cacheEntry) String() string {
|
2015-03-14 11:30:47 +00:00
|
|
|
if c.Subtype != "" {
|
|
|
|
return c.ID.Str() + "." + c.Subtype
|
|
|
|
}
|
|
|
|
return c.ID.Str()
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:49:14 +00:00
|
|
|
func (c *Cache) list(t backend.Type) ([]cacheEntry, error) {
|
2015-03-14 11:30:47 +00:00
|
|
|
var dir string
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case backend.Snapshot:
|
|
|
|
dir = filepath.Join(c.base, "snapshots")
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("cache not supported for type %v", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2015-05-02 13:49:14 +00:00
|
|
|
return []cacheEntry{}, nil
|
2015-03-14 11:30:47 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
fis, err := fd.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:49:14 +00:00
|
|
|
entries := make([]cacheEntry, 0, len(fis))
|
2015-03-14 11:30:47 +00:00
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
parts := strings.SplitN(fi.Name(), ".", 2)
|
|
|
|
|
|
|
|
id, err := backend.ParseID(parts[0])
|
|
|
|
// ignore invalid cache entries for now
|
|
|
|
if err != nil {
|
2015-03-28 10:50:23 +00:00
|
|
|
debug.Log("Cache.List", "unable to parse name %v as id: %v", parts[0], err)
|
2015-03-14 11:30:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:49:14 +00:00
|
|
|
e := cacheEntry{ID: id}
|
2015-03-14 11:30:47 +00:00
|
|
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
e.Subtype = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = append(entries, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
2015-03-09 21:26:39 +00:00
|
|
|
func (c *Cache) filename(t backend.Type, subtype string, id backend.ID) (string, error) {
|
|
|
|
filename := id.String()
|
|
|
|
if subtype != "" {
|
|
|
|
filename += "." + subtype
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case backend.Snapshot:
|
2015-03-09 21:26:39 +00:00
|
|
|
return filepath.Join(c.base, "snapshots", filename), nil
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("cache not supported for type %v", t)
|
|
|
|
}
|
2015-03-14 17:15:47 +00:00
|
|
|
|
2015-04-30 01:30:53 +00:00
|
|
|
func getCacheDir() (string, error) {
|
|
|
|
if dir := os.Getenv("RESTIC_CACHE"); dir != "" {
|
|
|
|
return dir, nil
|
|
|
|
}
|
2015-03-14 17:15:47 +00:00
|
|
|
|
2015-04-30 01:30:53 +00:00
|
|
|
return getXDGCacheDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// getXDGCacheDir returns the cache directory according to XDG basedir spec, see
|
2015-04-29 02:09:32 +00:00
|
|
|
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
2015-04-30 01:30:53 +00:00
|
|
|
func getXDGCacheDir() (string, error) {
|
2015-04-29 02:09:32 +00:00
|
|
|
xdgcache := os.Getenv("XDG_CACHE_HOME")
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
|
|
|
|
if xdgcache == "" && home == "" {
|
|
|
|
return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)")
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedir := ""
|
|
|
|
if xdgcache != "" {
|
|
|
|
cachedir = filepath.Join(xdgcache, "restic")
|
|
|
|
} else if home != "" {
|
|
|
|
cachedir = filepath.Join(home, ".cache", "restic")
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(cachedir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(cachedir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err = os.Stat(cachedir)
|
|
|
|
debug.Log("getCacheDir", "create cache dir %v", cachedir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return "", fmt.Errorf("cache dir %v is not a directory", cachedir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedir, nil
|
|
|
|
}
|