From 0e897ef7b8ddbf0db376e5793645ca963907f411 Mon Sep 17 00:00:00 2001 From: Stephane Thiell Date: Thu, 26 Sep 2019 15:48:52 -0700 Subject: [PATCH] Add support for $RESTIC_CACHE_DIR Add support for restic-specific $RESTIC_CACHE_DIR environment variable to override the cache directory like --cache-dir would have. --- doc/manual_rest.rst | 7 ++++--- internal/cache/dir.go | 8 ++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/doc/manual_rest.rst b/doc/manual_rest.rst index 07b99a79a..e2d9a1b35 100644 --- a/doc/manual_rest.rst +++ b/doc/manual_rest.rst @@ -376,9 +376,10 @@ OS-specific cache folder: * macOS: ``~/Library/Caches/restic`` * Windows: ``%LOCALAPPDATA%/restic`` -The command line parameter ``--cache-dir`` can each be used to override the -default cache location. The parameter ``--no-cache`` disables the cache -entirely. In this case, all data is loaded from the repo. +The command line parameter ``--cache-dir`` or the environment variable +``$RESTIC_CACHE_DIR`` can be used to override the default cache location. The +parameter ``--no-cache`` disables the cache entirely. In this case, all data +is loaded from the repo. The cache is ephemeral: When a file cannot be read from the cache, it is loaded from the repository. diff --git a/internal/cache/dir.go b/internal/cache/dir.go index 0b427b8e3..e9c7278b6 100644 --- a/internal/cache/dir.go +++ b/internal/cache/dir.go @@ -12,17 +12,21 @@ import ( // xdgCacheDir returns the cache directory according to XDG basedir spec, see // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html +// unless RESTIC_CACHE_DIR is defined func xdgCacheDir() (string, error) { + cachedir := os.Getenv("RESTIC_CACHE_DIR") xdgcache := os.Getenv("XDG_CACHE_HOME") home := os.Getenv("HOME") - if xdgcache != "" { + if cachedir != "" { + return cachedir, nil + } else if xdgcache != "" { return filepath.Join(xdgcache, "restic"), nil } else if home != "" { return filepath.Join(home, ".cache", "restic"), nil } - return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)") + return "", errors.New("unable to locate cache directory (RESTIC_CACHE_DIR, XDG_CACHE_HOME and HOME unset)") } // windowsCacheDir returns the cache directory for Windows.