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 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, diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 61bbd07ff..6d50dc96b 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -10,6 +10,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" @@ -158,6 +159,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/cache.go b/internal/cache/cache.go index bd88ed369..83013603b 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -167,7 +167,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) } 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 }