2015-02-21 23:09:57 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2015-03-14 17:15:47 +00:00
|
|
|
"encoding/json"
|
2015-02-21 23:09:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-03-14 11:30:47 +00:00
|
|
|
"strings"
|
2015-03-14 17:15:47 +00:00
|
|
|
"sync"
|
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-02-21 23:09:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
|
|
|
base string
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
func NewCache(be backend.Identifier) (c *Cache, err error) {
|
2015-03-14 11:10:08 +00:00
|
|
|
// try to get explicit cache dir from environment
|
|
|
|
dir := os.Getenv("RESTIC_CACHE")
|
|
|
|
|
|
|
|
// otherwise try OS specific default
|
|
|
|
if dir == "" {
|
|
|
|
dir, err = GetCacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
basedir := filepath.Join(dir, be.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-03-09 21:26:39 +00:00
|
|
|
func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error) {
|
2015-02-21 23:09:57 +00:00
|
|
|
// try to open file
|
2015-03-09 21:26:39 +00:00
|
|
|
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()
|
|
|
|
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-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-03-09 21:26:39 +00:00
|
|
|
func (c *Cache) Load(t backend.Type, subtype string, id backend.ID) (io.ReadCloser, error) {
|
2015-02-21 23:09:57 +00:00
|
|
|
// try to open file
|
2015-03-09 21:26:39 +00:00
|
|
|
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-03-14 11:30:47 +00:00
|
|
|
func (c *Cache) Purge(t backend.Type, subtype string, id backend.ID) error {
|
|
|
|
filename, err := c.filename(t, subtype, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(filename)
|
|
|
|
debug.Log("Cache.Purge", "Remove file %v: %v", filename, err)
|
|
|
|
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-28 14:07:08 +00:00
|
|
|
func (c *Cache) Clear(s Server) error {
|
2015-03-14 11:30:47 +00:00
|
|
|
list, err := c.List(backend.Snapshot)
|
|
|
|
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)
|
|
|
|
|
|
|
|
err = c.Purge(backend.Snapshot, entry.Subtype, entry.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CacheEntry struct {
|
|
|
|
ID backend.ID
|
|
|
|
Subtype string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CacheEntry) String() string {
|
|
|
|
if c.Subtype != "" {
|
|
|
|
return c.ID.Str() + "." + c.Subtype
|
|
|
|
}
|
|
|
|
return c.ID.Str()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) List(t backend.Type) ([]CacheEntry, error) {
|
|
|
|
var dir string
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case backend.Snapshot:
|
|
|
|
dir = filepath.Join(c.base, "snapshots")
|
|
|
|
case backend.Tree:
|
|
|
|
dir = filepath.Join(c.base, "trees")
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("cache not supported for type %v", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return []CacheEntry{}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
fis, err := fd.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := make([]CacheEntry, 0, len(fis))
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
e := CacheEntry{ID: id}
|
|
|
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
e.Subtype = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = append(entries, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
2015-02-21 23:09:57 +00:00
|
|
|
// Construct file name for given Type.
|
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
|
|
|
case backend.Tree:
|
2015-03-09 21:26:39 +00:00
|
|
|
return filepath.Join(c.base, "trees", 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
|
|
|
|
|
|
|
// high-level functions
|
|
|
|
|
2015-03-22 15:09:35 +00:00
|
|
|
// RefreshSnapshots loads the maps for all snapshots and saves them to the
|
|
|
|
// local cache. Old cache entries are purged.
|
2015-03-22 13:41:51 +00:00
|
|
|
func (c *Cache) RefreshSnapshots(s Server, p *Progress) error {
|
|
|
|
defer p.Done()
|
|
|
|
|
2015-03-22 15:09:35 +00:00
|
|
|
// list cache entries
|
|
|
|
entries, err := c.List(backend.Snapshot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-22 13:41:51 +00:00
|
|
|
// list snapshots first
|
2015-03-28 10:50:23 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
2015-03-22 13:41:51 +00:00
|
|
|
|
|
|
|
// check that snapshot blobs are cached
|
2015-03-28 10:50:23 +00:00
|
|
|
for name := range s.List(backend.Snapshot, done) {
|
|
|
|
id, err := backend.ParseID(name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-22 15:09:35 +00:00
|
|
|
// remove snapshot from list of entries
|
|
|
|
for i, e := range entries {
|
|
|
|
if e.ID.Equal(id) {
|
|
|
|
entries = append(entries[:i], entries[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 13:41:51 +00:00
|
|
|
has, err := c.Has(backend.Snapshot, "blobs", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// else start progress reporting
|
|
|
|
p.Start()
|
|
|
|
|
|
|
|
// build new cache
|
|
|
|
_, err = cacheSnapshotBlobs(p, s, c, id)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("Cache.RefreshSnapshots", "unable to cache snapshot blobs for %v: %v", id.Str(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 15:09:35 +00:00
|
|
|
// remove other entries
|
|
|
|
for _, e := range entries {
|
|
|
|
debug.Log("Cache.RefreshSnapshots", "remove entry %v", e)
|
|
|
|
err = c.Purge(backend.Snapshot, e.Subtype, e.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 13:41:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cacheSnapshotBlobs creates a cache of all the blobs used within the
|
2015-03-14 17:15:47 +00:00
|
|
|
// snapshot. It collects all blobs from all trees and saves the resulting map
|
|
|
|
// to the cache and returns the map.
|
2015-03-22 13:41:51 +00:00
|
|
|
func cacheSnapshotBlobs(p *Progress, s Server, c *Cache, id backend.ID) (*Map, error) {
|
2015-03-14 17:15:47 +00:00
|
|
|
debug.Log("CacheSnapshotBlobs", "create cache for snapshot %v", id.Str())
|
|
|
|
|
|
|
|
sn, err := LoadSnapshot(s, id)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("CacheSnapshotBlobs", "unable to load snapshot %v: %v", id.Str(), err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := NewMap()
|
|
|
|
|
|
|
|
// add top-level node
|
|
|
|
m.Insert(sn.Tree)
|
|
|
|
|
2015-03-22 13:41:51 +00:00
|
|
|
p.Report(Stat{Trees: 1})
|
|
|
|
|
2015-03-14 17:15:47 +00:00
|
|
|
// start walker
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ch := make(chan WalkTreeJob)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2015-03-28 14:07:08 +00:00
|
|
|
WalkTree(s, sn.Tree, nil, ch)
|
2015-03-14 17:15:47 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < maxConcurrencyPreload; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
for job := range ch {
|
|
|
|
if job.Tree == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-22 13:41:51 +00:00
|
|
|
p.Report(Stat{Trees: 1})
|
2015-03-14 17:15:47 +00:00
|
|
|
debug.Log("CacheSnapshotBlobs", "got job %v", job)
|
|
|
|
m.Merge(job.Tree.Map)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// save blob list for snapshot
|
|
|
|
return m, c.StoreMap(id, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) StoreMap(snid backend.ID, m *Map) error {
|
|
|
|
wr, err := c.Store(backend.Snapshot, "blobs", snid)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer wr.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(wr)
|
|
|
|
err = enc.Encode(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) LoadMap(s Server, snid backend.ID) (*Map, error) {
|
|
|
|
rd, err := c.Load(backend.Snapshot, "blobs", snid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &Map{}
|
|
|
|
err = json.NewDecoder(rd).Decode(m)
|
|
|
|
return m, err
|
|
|
|
}
|