package cache import ( "os" "path/filepath" "github.com/pkg/errors" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/fs" ) // 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) { 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 := fs.Stat(cachedir) if os.IsNotExist(errors.Cause(err)) { err = fs.MkdirAll(cachedir, 0700) if err != nil { return "", errors.Wrap(err, "MkdirAll") } fi, err = fs.Stat(cachedir) debug.Log("create cache dir %v", cachedir) } if err != nil { return "", errors.Wrap(err, "Stat") } if !fi.IsDir() { return "", errors.Errorf("cache dir %v is not a directory", cachedir) } return cachedir, nil }