1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 08:16:36 +00:00

local: Create directories below data/ if it exists

This commit is contained in:
Alexander Neumann 2017-08-27 20:38:00 +02:00
parent ea017a49c3
commit 3686b1ffe5

View file

@ -25,6 +25,25 @@ var _ restic.Backend = &Local{}
const defaultLayout = "default" const defaultLayout = "default"
// dirExists returns true if the name exists and is a directory.
func dirExists(name string) bool {
f, err := fs.Open(name)
if err != nil {
return false
}
fi, err := f.Stat()
if err != nil {
return false
}
if err = f.Close(); err != nil {
return false
}
return fi.IsDir()
}
// Open opens the local backend as specified by config. // Open opens the local backend as specified by config.
func Open(cfg Config) (*Local, error) { func Open(cfg Config) (*Local, error) {
debug.Log("open local backend at %v (layout %q)", cfg.Path, cfg.Layout) debug.Log("open local backend at %v (layout %q)", cfg.Path, cfg.Layout)
@ -36,10 +55,17 @@ func Open(cfg Config) (*Local, error) {
be := &Local{Config: cfg, Layout: l} be := &Local{Config: cfg, Layout: l}
// if data dir exists, make sure that all subdirs also exist // if data dir exists, make sure that all subdirs also exist
for _, d := range be.Paths() { datadir := be.Dirname(restic.Handle{Type: restic.DataFile})
err := fs.MkdirAll(d, backend.Modes.Dir) if dirExists(datadir) {
if err != nil { for _, d := range be.Paths() {
return nil, errors.Wrap(err, "MkdirAll") if _, err := filepath.Rel(datadir, d); err != nil {
continue
}
err := fs.MkdirAll(d, backend.Modes.Dir)
if err != nil {
return nil, errors.Wrap(err, "MkdirAll")
}
} }
} }