mirror of
https://github.com/octoleo/restic.git
synced 2024-12-25 20:11:06 +00:00
cache: Simplify cache dir creation
This commit is contained in:
parent
fa893ee477
commit
f4bab789b8
19
internal/cache/cache.go
vendored
19
internal/cache/cache.go
vendored
@ -85,10 +85,12 @@ func writeCachedirTag(dir string) error {
|
||||
// performReadahead returns true.
|
||||
func New(id string, basedir string) (c *Cache, err error) {
|
||||
if basedir == "" {
|
||||
basedir, err = defaultCacheDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
basedir, err = DefaultDir()
|
||||
}
|
||||
|
||||
err = mkdirCacheDir(basedir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
err = updateTimestamp(cachedir)
|
||||
if err != nil {
|
||||
// create the repo cache dir if it does not exist yet
|
||||
if err = fs.MkdirAll(cachedir, dirMode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create the repo cache dir if it does not exist yet
|
||||
if err = fs.MkdirAll(cachedir, dirMode); err != nil {
|
||||
// update the timestamp so that we can detect old cache dirs
|
||||
err = updateTimestamp(cachedir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
26
internal/cache/dir.go
vendored
26
internal/cache/dir.go
vendored
@ -49,29 +49,25 @@ func darwinCacheDir() (string, error) {
|
||||
return filepath.Join(home, "Library", "Caches", "restic"), nil
|
||||
}
|
||||
|
||||
// defaultCacheDir determines and creates the default cache directory for this
|
||||
// system.
|
||||
func defaultCacheDir() (string, error) {
|
||||
var cachedir string
|
||||
var err error
|
||||
// DefaultDir returns the default cache directory for the current OS.
|
||||
func DefaultDir() (cachedir string, err error) {
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
cachedir, err = darwinCacheDir()
|
||||
case "windows":
|
||||
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)
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
err = fs.MkdirAll(cachedir, 0700)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "MkdirAll")
|
||||
return errors.Wrap(err, "MkdirAll")
|
||||
}
|
||||
|
||||
fi, err = fs.Stat(cachedir)
|
||||
@ -79,12 +75,12 @@ func defaultCacheDir() (string, error) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Stat")
|
||||
return errors.Wrap(err, "Stat")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user