From 95eb859b546091a7f72d25a6a6bcdba09d5f8b36 Mon Sep 17 00:00:00 2001 From: kitone Date: Sun, 8 Aug 2021 19:30:07 +0200 Subject: [PATCH 1/4] Honor RESTIC_CACHE_DIR environment variable Fix #3382: restic check doesn't obey the RESTIC_CACHE_DIR environment variable --- cmd/restic/cmd_check.go | 4 ++++ internal/cache/dir.go | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 2b5017ead..71629dbf0 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" + "github.com/restic/restic/internal/cache" "github.com/restic/restic/internal/checker" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/fs" @@ -146,6 +147,9 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions) (cleanup func()) } cachedir := gopts.CacheDir + if cachedir == "" { + cachedir = cache.EnvDir() + } // use a cache in a temporary directory tempdir, err := ioutil.TempDir(cachedir, "restic-check-cache-") diff --git a/internal/cache/dir.go b/internal/cache/dir.go index 98019036a..5abdf2313 100644 --- a/internal/cache/dir.go +++ b/internal/cache/dir.go @@ -6,10 +6,15 @@ import ( "path/filepath" ) +// EnvDir return $RESTIC_CACHE_DIR env +func EnvDir() string { + return os.Getenv("RESTIC_CACHE_DIR") +} + // DefaultDir returns $RESTIC_CACHE_DIR, or the default cache directory // for the current OS if that variable is not set. func DefaultDir() (cachedir string, err error) { - cachedir = os.Getenv("RESTIC_CACHE_DIR") + cachedir = EnvDir() if cachedir != "" { return cachedir, nil } From 1b23675f215a113d522033582f573fc5ab079638 Mon Sep 17 00:00:00 2001 From: kitone Date: Thu, 4 Nov 2021 14:48:01 +0100 Subject: [PATCH 2/4] cache --cleanup should handle directories created by restic check. Because there is no guarantee that a cleanup of these directories will occur after the "restic check", we extend the behavior to detect and manage these specific cache directories and allow their cleanup too. --- internal/cache/cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 48b175df1..bc82675d3 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -172,7 +172,7 @@ func updateTimestamp(d string) error { const MaxCacheAge = 30 * 24 * time.Hour func validCacheDirName(s string) bool { - r := regexp.MustCompile(`^[a-fA-F0-9]{64}$`) + r := regexp.MustCompile(`^[a-fA-F0-9]{64}$|^restic-check-cache-[0-9]+$`) return r.MatchString(s) } From 0425a30420764c57ef46c662d891c4bb60457c5b Mon Sep 17 00:00:00 2001 From: kitone Date: Thu, 4 Nov 2021 15:10:14 +0100 Subject: [PATCH 3/4] add changelog issue-3382 --- changelog/unreleased/issue-3382 | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 changelog/unreleased/issue-3382 diff --git a/changelog/unreleased/issue-3382 b/changelog/unreleased/issue-3382 new file mode 100644 index 000000000..04dcfca05 --- /dev/null +++ b/changelog/unreleased/issue-3382 @@ -0,0 +1,9 @@ +Change: honor RESTIC_CACHE_DIR environment variable in the check command + +--cache-dir option doesn't honor the RESTIC_CACHE_DIR environment variable by +default. This fixes an issue with the restic check command that doesn't obey the +RESTIC_CACHE_DIR environment variable. Extend the behavior of cache command to +manage directories created by restic check. + +https://github.com/restic/restic/issues/3382 +https://github.com/restic/restic/pull/3474 From 5904f80cfa077f68422f289e9fdc0007f68b6217 Mon Sep 17 00:00:00 2001 From: kitone Date: Sat, 6 Nov 2021 20:18:51 +0100 Subject: [PATCH 4/4] restic cache should display the name of the cache without shortening it in the case of the restic check --- cmd/restic/cmd_cache.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/restic/cmd_cache.go b/cmd/restic/cmd_cache.go index 1b36219eb..50a738d5c 100644 --- a/cmd/restic/cmd_cache.go +++ b/cmd/restic/cmd_cache.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "sort" + "strings" "time" "github.com/restic/restic/internal/cache" @@ -140,8 +141,13 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { size = fmt.Sprintf("%11s", formatBytes(uint64(bytes))) } + name := entry.Name() + if !strings.HasPrefix(name, "restic-check-cache-") { + name = name[:10] + } + tab.AddRow(data{ - entry.Name()[:10], + name, fmt.Sprintf("%d days ago", uint(time.Since(entry.ModTime()).Hours()/24)), old, size,