From e1fc17aeb1d39f93844ff0bd1a18d8180b84adc5 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 14 Mar 2015 12:10:08 +0100 Subject: [PATCH] Implement per-repository cache --- archiver.go | 2 +- cache.go | 23 +++++++++++++++-------- cmd/restic/cmd_cache.go | 2 +- cmd/restic/main.go | 2 +- key_test.go | 8 +++++++- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/archiver.go b/archiver.go index ebbbc307e..26be67427 100644 --- a/archiver.go +++ b/archiver.go @@ -53,7 +53,7 @@ func NewArchiver(s Server) (*Archiver, error) { arch.m = NewMap() // init cache - arch.c, err = NewCache() + arch.c, err = NewCache(s) if err != nil { return nil, err } diff --git a/cache.go b/cache.go index 341c9446e..7a670e183 100644 --- a/cache.go +++ b/cache.go @@ -7,22 +7,29 @@ import ( "path/filepath" "github.com/restic/restic/backend" + "github.com/restic/restic/debug" ) -// for testing -var getCacheDir = GetCacheDir - type Cache struct { base string } -func NewCache() (*Cache, error) { - dir, err := getCacheDir() - if err != nil { - return nil, err +func NewCache(be backend.IDer) (c *Cache, err error) { + // 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 + } } - return &Cache{base: dir}, nil + basedir := filepath.Join(dir, be.ID().String()) + debug.Log("Cache.New", "opened cache at %v", basedir) + + return &Cache{base: basedir}, nil } func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error) { diff --git a/cmd/restic/cmd_cache.go b/cmd/restic/cmd_cache.go index ab8ea0a5a..2b145e349 100644 --- a/cmd/restic/cmd_cache.go +++ b/cmd/restic/cmd_cache.go @@ -42,7 +42,7 @@ func (cmd CmdCache) Execute(args []string) error { return err } - cache, err := restic.NewCache() + cache, err := restic.NewCache(s) if err != nil { return err } diff --git a/cmd/restic/main.go b/cmd/restic/main.go index df6402640..3132508e9 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -79,7 +79,7 @@ func (cmd CmdInit) Execute(args []string) error { os.Exit(1) } - fmt.Printf("created restic backend at %s\n", opts.Repo) + fmt.Printf("created restic backend %v at %s\n", s.ID().Str(), opts.Repo) return nil } diff --git a/key_test.go b/key_test.go index 8ef704d0d..6c22e7e87 100644 --- a/key_test.go +++ b/key_test.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "testing" "github.com/restic/restic" @@ -22,7 +23,12 @@ func setupBackend(t testing.TB) restic.Server { tempdir, err := ioutil.TempDir(*testTempDir, "restic-test-") ok(t, err) - b, err := backend.CreateLocal(tempdir) + // create repository below temp dir + b, err := backend.CreateLocal(filepath.Join(tempdir, "repo")) + ok(t, err) + + // set cache dir + err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache")) ok(t, err) return restic.NewServer(b)