From 8a2d7ff2bcf062b4063030e31de7fd9e6d2e029b Mon Sep 17 00:00:00 2001 From: klauspost Date: Mon, 17 Aug 2015 14:17:54 +0200 Subject: [PATCH] Add default cache location for Windows. Primary place for Windows cache is %APPDATA%\restic. If that environment variable isn't set, we create a 'restic' folder in the 'temp' directory. --- cache.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cache.go b/cache.go index 4cdde46f0..8dfd9ff84 100644 --- a/cache.go +++ b/cache.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "runtime" "strings" "github.com/restic/restic/backend" @@ -212,10 +213,40 @@ func getCacheDir() (string, error) { if dir := os.Getenv("RESTIC_CACHE"); dir != "" { return dir, nil } + if runtime.GOOS == "windows" { + return getWindowsCacheDir() + } return getXDGCacheDir() } +// getWindowsCacheDir will return %APPDATA%\restic or create +// a folder in the temporary folder called "restic". +func getWindowsCacheDir() (string, error) { + cachedir := os.Getenv("APPDATA") + if cachedir == "" { + cachedir = os.TempDir() + } + cachedir = filepath.Join(cachedir, "restic") + fi, err := os.Stat(cachedir) + + if os.IsNotExist(err) { + err = os.MkdirAll(cachedir, 0700) + if err != nil { + return "", err + } + } + + if err != nil { + return "", err + } + + if !fi.IsDir() { + return "", fmt.Errorf("cache dir %v is not a directory", cachedir) + } + return cachedir, nil +} + // getXDGCacheDir returns the cache directory according to XDG basedir spec, see // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html func getXDGCacheDir() (string, error) {