diff --git a/cache.go b/cache.go index 0d0a0c756..19b32cb75 100644 --- a/cache.go +++ b/cache.go @@ -2,6 +2,7 @@ package restic import ( "encoding/json" + "errors" "fmt" "io" "os" @@ -349,3 +350,42 @@ func (c *Cache) LoadMap(s *server.Server, snid backend.ID) (*Map, error) { err = json.NewDecoder(rd).Decode(m) return m, err } + +// GetCacheDir returns the cache directory according to XDG basedir spec, see +// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html +func GetCacheDir() (string, error) { + xdgcache := os.Getenv("XDG_CACHE_HOME") + home := os.Getenv("HOME") + + if xdgcache == "" && home == "" { + return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)") + } + + cachedir := "" + if xdgcache != "" { + cachedir = filepath.Join(xdgcache, "restic") + } else if home != "" { + cachedir = filepath.Join(home, ".cache", "restic") + } + + fi, err := os.Stat(cachedir) + if os.IsNotExist(err) { + err = os.MkdirAll(cachedir, 0700) + if err != nil { + return "", err + } + + fi, err = os.Stat(cachedir) + debug.Log("getCacheDir", "create cache dir %v", cachedir) + } + + if err != nil { + return "", err + } + + if !fi.IsDir() { + return "", fmt.Errorf("cache dir %v is not a directory", cachedir) + } + + return cachedir, nil +} diff --git a/cache_darwin.go b/cache_darwin.go deleted file mode 100644 index 816fa2416..000000000 --- a/cache_darwin.go +++ /dev/null @@ -1,49 +0,0 @@ -package restic - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/restic/restic/debug" -) - -// GetCacheDir returns the cache directory according to XDG basedir spec, see -// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html -func GetCacheDir() (string, error) { - xdgcache := os.Getenv("XDG_CACHE_HOME") - home := os.Getenv("HOME") - - if xdgcache == "" && home == "" { - return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)") - } - - cachedir := "" - if xdgcache != "" { - cachedir = filepath.Join(xdgcache, "restic") - } else if home != "" { - cachedir = filepath.Join(home, ".cache", "restic") - } - - fi, err := os.Stat(cachedir) - if os.IsNotExist(err) { - err = os.MkdirAll(cachedir, 0700) - if err != nil { - return "", err - } - - fi, err = os.Stat(cachedir) - debug.Log("getCacheDir", "create cache dir %v", cachedir) - } - - if err != nil { - return "", err - } - - if !fi.IsDir() { - return "", fmt.Errorf("cache dir %v is not a directory", cachedir) - } - - return cachedir, nil -} diff --git a/cache_linux.go b/cache_linux.go deleted file mode 100644 index 816fa2416..000000000 --- a/cache_linux.go +++ /dev/null @@ -1,49 +0,0 @@ -package restic - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/restic/restic/debug" -) - -// GetCacheDir returns the cache directory according to XDG basedir spec, see -// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html -func GetCacheDir() (string, error) { - xdgcache := os.Getenv("XDG_CACHE_HOME") - home := os.Getenv("HOME") - - if xdgcache == "" && home == "" { - return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)") - } - - cachedir := "" - if xdgcache != "" { - cachedir = filepath.Join(xdgcache, "restic") - } else if home != "" { - cachedir = filepath.Join(home, ".cache", "restic") - } - - fi, err := os.Stat(cachedir) - if os.IsNotExist(err) { - err = os.MkdirAll(cachedir, 0700) - if err != nil { - return "", err - } - - fi, err = os.Stat(cachedir) - debug.Log("getCacheDir", "create cache dir %v", cachedir) - } - - if err != nil { - return "", err - } - - if !fi.IsDir() { - return "", fmt.Errorf("cache dir %v is not a directory", cachedir) - } - - return cachedir, nil -}