From 62a11fa0329b12d7a708a0e88e9b0dc1aa1681f9 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Tue, 28 Apr 2015 22:09:32 -0400 Subject: [PATCH] merge cache_darwin and cache_linux --- cache.go | 40 ++++++++++++++++++++++++++++++++++++++++ cache_darwin.go | 49 ------------------------------------------------- cache_linux.go | 49 ------------------------------------------------- 3 files changed, 40 insertions(+), 98 deletions(-) delete mode 100644 cache_darwin.go delete mode 100644 cache_linux.go 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 -}