1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-03-16 00:00:05 +01:00

Refactor the node traversal in restoreTo into a reusable walk method

This commit is contained in:
Seb Patane 2016-11-14 03:05:23 +10:00
parent ff6495d17d
commit 28202edf33

View file

@ -39,7 +39,7 @@ func NewRestorer(repo Repository, id ID) (*Restorer, error) {
return r, nil
}
func (res *Restorer) restoreTo(dst string, dir string, treeID ID) error {
func (res *Restorer) walk(dir string, treeID ID, callback func(*Node, string) error) error {
tree, err := res.repo.LoadTree(treeID)
if err != nil {
return res.Error(dir, nil, err)
@ -50,7 +50,7 @@ func (res *Restorer) restoreTo(dst string, dir string, treeID ID) error {
debug.Log("SelectForRestore returned %v", selectedForRestore)
if selectedForRestore {
err := res.restoreNodeTo(node, dir, dst)
err := callback(node, dir)
if err != nil {
return err
}
@ -62,27 +62,45 @@ func (res *Restorer) restoreTo(dst string, dir string, treeID ID) error {
}
subp := filepath.Join(dir, node.Name)
err = res.restoreTo(dst, subp, *node.Subtree)
err = res.walk(subp, *node.Subtree, callback)
if err != nil {
err = res.Error(subp, node, err)
if err != nil {
return err
}
}
if selectedForRestore {
// Restore directory timestamp at the end. If we would do it earlier, restoring files within
// the directory would overwrite the timestamp of the directory they are in.
if err := node.RestoreTimestamps(filepath.Join(dst, dir, node.Name)); err != nil {
return err
}
}
}
}
return nil
}
func (res *Restorer) restoreTo(dst string, dir string, treeID ID) error {
err := res.walk(dir, treeID, func(node *Node, currentDir string) error {
return res.restoreNodeTo(node, currentDir, dst)
})
if err != nil {
return err
}
// Restore directory timestamps at the end. If we would do it earlier, restoring files within
// the directory would overwrite the timestamp of the directory they are in.
err = res.walk(dir, treeID, func(node *Node, currentDir string) error {
if node.Type == "dir" {
return node.RestoreTimestamps(filepath.Join(dst, currentDir, node.Name))
}
return nil
})
if err != nil {
return err
}
return nil
}
func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error {
debug.Log("node %v, dir %v, dst %v", node.Name, dir, dst)
dstPath := filepath.Join(dst, dir, node.Name)