2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 06:07:44 +00:00

cache: Simplify cache dir creation

This commit is contained in:
Alexander Neumann 2017-11-20 21:58:38 +01:00
parent fa893ee477
commit f4bab789b8
2 changed files with 22 additions and 23 deletions

View File

@ -85,10 +85,12 @@ func writeCachedirTag(dir string) error {
// performReadahead returns true. // performReadahead returns true.
func New(id string, basedir string) (c *Cache, err error) { func New(id string, basedir string) (c *Cache, err error) {
if basedir == "" { if basedir == "" {
basedir, err = defaultCacheDir() basedir, err = DefaultDir()
if err != nil { }
return nil, err
} err = mkdirCacheDir(basedir)
if err != nil {
return nil, err
} }
// create base dir and tag it as a cache directory // create base dir and tag it as a cache directory
@ -108,13 +110,14 @@ func New(id string, basedir string) (c *Cache, err error) {
return nil, errors.New("cache version is newer") return nil, errors.New("cache version is newer")
} }
err = updateTimestamp(cachedir) // create the repo cache dir if it does not exist yet
if err != nil { if err = fs.MkdirAll(cachedir, dirMode); err != nil {
return nil, err return nil, err
} }
// create the repo cache dir if it does not exist yet // update the timestamp so that we can detect old cache dirs
if err = fs.MkdirAll(cachedir, dirMode); err != nil { err = updateTimestamp(cachedir)
if err != nil {
return nil, err return nil, err
} }

26
internal/cache/dir.go vendored
View File

@ -49,29 +49,25 @@ func darwinCacheDir() (string, error) {
return filepath.Join(home, "Library", "Caches", "restic"), nil return filepath.Join(home, "Library", "Caches", "restic"), nil
} }
// defaultCacheDir determines and creates the default cache directory for this // DefaultDir returns the default cache directory for the current OS.
// system. func DefaultDir() (cachedir string, err error) {
func defaultCacheDir() (string, error) {
var cachedir string
var err error
switch runtime.GOOS { switch runtime.GOOS {
case "darwin": case "darwin":
cachedir, err = darwinCacheDir() cachedir, err = darwinCacheDir()
case "windows": case "windows":
cachedir, err = windowsCacheDir() cachedir, err = windowsCacheDir()
default:
// Default to XDG for Linux and any other OSes.
cachedir, err = xdgCacheDir()
}
if err != nil {
return "", err
} }
// Default to XDG for Linux and any other OSes.
return xdgCacheDir()
}
func mkdirCacheDir(cachedir string) error {
fi, err := fs.Stat(cachedir) fi, err := fs.Stat(cachedir)
if os.IsNotExist(errors.Cause(err)) { if os.IsNotExist(errors.Cause(err)) {
err = fs.MkdirAll(cachedir, 0700) err = fs.MkdirAll(cachedir, 0700)
if err != nil { if err != nil {
return "", errors.Wrap(err, "MkdirAll") return errors.Wrap(err, "MkdirAll")
} }
fi, err = fs.Stat(cachedir) fi, err = fs.Stat(cachedir)
@ -79,12 +75,12 @@ func defaultCacheDir() (string, error) {
} }
if err != nil { if err != nil {
return "", errors.Wrap(err, "Stat") return errors.Wrap(err, "Stat")
} }
if !fi.IsDir() { if !fi.IsDir() {
return "", errors.Errorf("cache dir %v is not a directory", cachedir) return errors.Errorf("cache dir %v is not a directory", cachedir)
} }
return cachedir, nil return nil
} }