mirror of
https://github.com/restic/restic.git
synced 2024-08-25 05:37:25 +02:00
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
|
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
|
||
|
}
|