1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-22 07:43:03 +00:00

Pass "source" path to restorer filter function

The source path is probably more useful here as the user, when
restoring, probably wants to filter for /usr/local/foo instead of
/tmp/restore-whatever/{usr/local/,}foo.
This commit is contained in:
Sebastian Schmidt 2015-01-01 15:20:21 +01:00
parent ccada7d89a
commit 10b99e53e4

View file

@ -16,7 +16,7 @@ type Restorer struct {
sn *Snapshot
Error func(dir string, node *Node, err error) error
Filter func(item string, node *Node) bool
Filter func(item string, dstpath string, node *Node) bool
}
// NewRestorer creates a restorer preloaded with the content from the snapshot snid.
@ -37,12 +37,12 @@ func NewRestorer(s Server, snid backend.ID) (*Restorer, error) {
// abort on all errors
r.Error = func(string, *Node, error) error { return err }
// allow all files
r.Filter = func(string, *Node) bool { return true }
r.Filter = func(string, string, *Node) bool { return true }
return r, nil
}
func (res *Restorer) to(dir string, tree_id backend.ID) error {
func (res *Restorer) to(dst string, dir string, tree_id backend.ID) error {
tree := Tree{}
err := res.ch.LoadJSON(backend.Tree, tree_id, &tree)
if err != nil {
@ -50,12 +50,12 @@ func (res *Restorer) to(dir string, tree_id backend.ID) error {
}
for _, node := range tree {
p := filepath.Join(dir, node.Name)
dstpath := filepath.Join(dst, dir, node.Name)
if res.Filter(p, node) {
err := node.CreateAt(res.ch, p)
if res.Filter(filepath.Join(res.sn.Dir, dir, node.Name), dstpath, node) {
err := node.CreateAt(res.ch, dstpath)
if err != nil {
err = res.Error(p, node, arrar.Annotate(err, "create node"))
err = res.Error(dstpath, node, arrar.Annotate(err, "create node"))
if err != nil {
return err
}
@ -67,9 +67,10 @@ func (res *Restorer) to(dir string, tree_id backend.ID) error {
return errors.New(fmt.Sprintf("Dir without subtree in tree %s", tree_id))
}
err = res.to(p, node.Subtree)
subp := filepath.Join(dir, node.Name)
err = res.to(dst, subp, node.Subtree)
if err != nil {
err = res.Error(p, node, arrar.Annotate(err, "restore subtree"))
err = res.Error(subp, node, arrar.Annotate(err, "restore subtree"))
if err != nil {
return err
}
@ -88,7 +89,7 @@ func (res *Restorer) RestoreTo(dir string) error {
return err
}
return res.to(dir, res.sn.Tree)
return res.to(dir, "", res.sn.Tree)
}
func (res *Restorer) Snapshot() *Snapshot {