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:
parent
fa893ee477
commit
f4bab789b8
15
internal/cache/cache.go
vendored
15
internal/cache/cache.go
vendored
@ -85,11 +85,13 @@ func writeCachedirTag(dir string) error {
|
||||
// performReadahead returns true.
|
||||
func New(id string, basedir string) (c *Cache, err error) {
|
||||
if basedir == "" {
|
||||
basedir, err = defaultCacheDir()
|
||||
basedir, err = DefaultDir()
|
||||
}
|
||||
|
||||
err = mkdirCacheDir(basedir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// create base dir and tag it as a cache directory
|
||||
if err = writeCachedirTag(basedir); err != nil {
|
||||
@ -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