mirror of
https://github.com/octoleo/restic.git
synced 2024-11-25 22:27:35 +00:00
Merge pull request #2607 from greatroar/cachedir
Honor RESTIC_CACHE_DIR on Mac and Windows
This commit is contained in:
commit
694b7a17e7
7
changelog/unreleased/pull-2607
Normal file
7
changelog/unreleased/pull-2607
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
Bugfix: Honor RESTIC_CACHE_DIR environment variable on Mac and Windows
|
||||||
|
|
||||||
|
On Mac and Windows, the RESTIC_CACHE_DIR environment variable was ignored.
|
||||||
|
This variable can now be used on all platforms to set the directory where
|
||||||
|
restic stores caches.
|
||||||
|
|
||||||
|
https://github.com/restic/restic/pull/2607
|
62
internal/cache/dir.go
vendored
62
internal/cache/dir.go
vendored
@ -1,75 +1,29 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/fs"
|
"github.com/restic/restic/internal/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// xdgCacheDir returns the cache directory according to XDG basedir spec, see
|
// DefaultDir returns $RESTIC_CACHE_DIR, or the default cache directory
|
||||||
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
// for the current OS if that variable is not set.
|
||||||
// unless RESTIC_CACHE_DIR is defined
|
func DefaultDir() (cachedir string, err error) {
|
||||||
func xdgCacheDir() (string, error) {
|
cachedir = os.Getenv("RESTIC_CACHE_DIR")
|
||||||
cachedir := os.Getenv("RESTIC_CACHE_DIR")
|
|
||||||
xdgcache := os.Getenv("XDG_CACHE_HOME")
|
|
||||||
home := os.Getenv("HOME")
|
|
||||||
|
|
||||||
if cachedir != "" {
|
if cachedir != "" {
|
||||||
return cachedir, nil
|
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 (RESTIC_CACHE_DIR, XDG_CACHE_HOME and HOME unset)")
|
|
||||||
}
|
|
||||||
|
|
||||||
// windowsCacheDir returns the cache directory for Windows.
|
|
||||||
//
|
|
||||||
// Uses LOCALAPPDATA, where application data not synchronized between machines
|
|
||||||
// is stored. (Browser caches stored here).
|
|
||||||
func windowsCacheDir() (string, error) {
|
|
||||||
appdata := os.Getenv("LOCALAPPDATA")
|
|
||||||
if appdata == "" {
|
|
||||||
return "", errors.New("unable to locate cache directory (LOCALAPPDATA unset)")
|
|
||||||
}
|
|
||||||
return filepath.Join(appdata, "restic"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// darwinCacheDir returns the cache directory for darwin.
|
|
||||||
//
|
|
||||||
// Uses ~/Library/Caches/, which is recommended by Apple, see
|
|
||||||
// https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
|
|
||||||
func darwinCacheDir() (string, error) {
|
|
||||||
home := os.Getenv("HOME")
|
|
||||||
if home == "" {
|
|
||||||
return "", errors.New("unable to locate cache directory (HOME unset)")
|
|
||||||
}
|
|
||||||
return filepath.Join(home, "Library", "Caches", "restic"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultDir returns the default cache directory for the current OS.
|
|
||||||
func DefaultDir() (cachedir string, err error) {
|
|
||||||
switch runtime.GOOS {
|
|
||||||
case "darwin":
|
|
||||||
cachedir, err = darwinCacheDir()
|
|
||||||
case "windows":
|
|
||||||
cachedir, err = windowsCacheDir()
|
|
||||||
default:
|
|
||||||
// Default to XDG for Linux and any other OSes.
|
|
||||||
cachedir, err = xdgCacheDir()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cachedir, err = os.UserCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", fmt.Errorf("unable to locate cache directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cachedir, nil
|
return filepath.Join(cachedir, "restic"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// mkdirCacheDir ensures that the cache directory exists. It it didn't, created
|
// mkdirCacheDir ensures that the cache directory exists. It it didn't, created
|
||||||
|
26
internal/cache/dir_test.go
vendored
Normal file
26
internal/cache/dir_test.go
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
rtest "github.com/restic/restic/internal/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultDir should honor RESTIC_CACHE_DIR on all platforms.
|
||||||
|
func TestCacheDirEnv(t *testing.T) {
|
||||||
|
cachedir := os.Getenv("RESTIC_CACHE_DIR")
|
||||||
|
|
||||||
|
if cachedir == "" {
|
||||||
|
cachedir = "/doesnt/exist"
|
||||||
|
err := os.Setenv("RESTIC_CACHE_DIR", cachedir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Unsetenv("RESTIC_CACHE_DIR")
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, err := DefaultDir()
|
||||||
|
rtest.Equals(t, cachedir, dir)
|
||||||
|
rtest.OK(t, err)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user