mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +00:00
Merge pull request #2981 from greatroar/mkdircachedir
Simplify cache directory creation
This commit is contained in:
commit
c84643c6a9
3
internal/cache/cache.go
vendored
3
internal/cache/cache.go
vendored
@ -96,7 +96,7 @@ func New(id string, basedir string) (c *Cache, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
created, err := mkdirCacheDir(basedir)
|
||||
err = fs.MkdirAll(basedir, 0700)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -119,6 +119,7 @@ func New(id string, basedir string) (c *Cache, err error) {
|
||||
}
|
||||
|
||||
// create the repo cache dir if it does not exist yet
|
||||
var created bool
|
||||
_, err = fs.Lstat(cachedir)
|
||||
if os.IsNotExist(err) {
|
||||
err = fs.MkdirAll(cachedir, dirMode)
|
||||
|
33
internal/cache/dir.go
vendored
33
internal/cache/dir.go
vendored
@ -4,10 +4,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/fs"
|
||||
)
|
||||
|
||||
// DefaultDir returns $RESTIC_CACHE_DIR, or the default cache directory
|
||||
@ -25,32 +21,3 @@ func DefaultDir() (cachedir string, err error) {
|
||||
|
||||
return filepath.Join(cachedir, "restic"), nil
|
||||
}
|
||||
|
||||
// mkdirCacheDir ensures that the cache directory exists. It it didn't, created
|
||||
// is set to true.
|
||||
func mkdirCacheDir(cachedir string) (created bool, err error) {
|
||||
var newCacheDir bool
|
||||
|
||||
fi, err := fs.Stat(cachedir)
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
err = fs.MkdirAll(cachedir, 0700)
|
||||
if err != nil {
|
||||
return true, errors.Wrap(err, "MkdirAll")
|
||||
}
|
||||
|
||||
fi, err = fs.Stat(cachedir)
|
||||
debug.Log("create cache dir %v", cachedir)
|
||||
|
||||
newCacheDir = true
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return newCacheDir, errors.Wrap(err, "Stat")
|
||||
}
|
||||
|
||||
if !fi.IsDir() {
|
||||
return newCacheDir, errors.Errorf("cache dir %v is not a directory", cachedir)
|
||||
}
|
||||
|
||||
return newCacheDir, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user