2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-14 09:14:07 +00:00
restic/internal/walker/walker.go

138 lines
3.6 KiB
Go
Raw Normal View History

2018-06-09 16:19:27 +00:00
package walker
import (
"context"
"path"
"sort"
"github.com/pkg/errors"
"github.com/restic/restic/internal/restic"
)
// ErrSkipNode is returned by WalkFunc when a dir node should not be walked.
var ErrSkipNode = errors.New("skip this node")
2018-06-09 16:19:27 +00:00
// WalkFunc is the type of the function called for each node visited by Walk.
// Path is the slash-separated path from the root node. If there was a problem
// loading a node, err is set to a non-nil error. WalkFunc can chose to ignore
// it by returning nil.
//
// When the special value ErrSkipNode is returned and node is a dir node, it is
2018-06-09 16:19:27 +00:00
// not walked. When the node is not a dir node, the remaining items in this
// tree are skipped.
//
// Setting ignore to true tells Walk that it should not visit the node again.
// For tree nodes, this means that the function is not called for the
// referenced tree. If the node is not a tree, and all nodes in the current
// tree have ignore set to true, the current tree will not be visited again.
// When err is not nil and different from ErrSkipNode, the value returned for
2018-06-09 16:19:27 +00:00
// ignore is ignored.
2018-08-19 10:28:06 +00:00
type WalkFunc func(parentTreeID restic.ID, path string, node *restic.Node, nodeErr error) (ignore bool, err error)
2018-06-09 16:19:27 +00:00
// Walk calls walkFn recursively for each node in root. If walkFn returns an
// error, it is passed up the call stack. The trees in ignoreTrees are not
// walked. If walkFn ignores trees, these are added to the set.
2020-02-22 20:21:36 +00:00
func Walk(ctx context.Context, repo restic.TreeLoader, root restic.ID, ignoreTrees restic.IDSet, walkFn WalkFunc) error {
2018-06-09 16:19:27 +00:00
tree, err := repo.LoadTree(ctx, root)
2018-08-19 10:28:06 +00:00
_, err = walkFn(root, "/", nil, err)
2018-06-09 16:19:27 +00:00
if err != nil {
if err == ErrSkipNode {
2018-06-09 16:19:27 +00:00
err = nil
}
return err
}
2018-06-09 21:31:31 +00:00
if ignoreTrees == nil {
ignoreTrees = restic.NewIDSet()
}
2018-08-19 10:28:06 +00:00
_, err = walk(ctx, repo, "/", root, tree, ignoreTrees, walkFn)
2018-06-09 16:19:27 +00:00
return err
}
// walk recursively traverses the tree, ignoring subtrees when the ID of the
// subtree is in ignoreTrees. If err is nil and ignore is true, the subtree ID
// will be added to ignoreTrees by walk.
2020-02-22 20:21:36 +00:00
func walk(ctx context.Context, repo restic.TreeLoader, prefix string, parentTreeID restic.ID, tree *restic.Tree, ignoreTrees restic.IDSet, walkFn WalkFunc) (ignore bool, err error) {
2018-06-09 16:19:27 +00:00
var allNodesIgnored = true
if len(tree.Nodes) == 0 {
allNodesIgnored = false
}
2018-06-09 16:19:27 +00:00
sort.Slice(tree.Nodes, func(i, j int) bool {
return tree.Nodes[i].Name < tree.Nodes[j].Name
})
for _, node := range tree.Nodes {
p := path.Join(prefix, node.Name)
if node.Type == "" {
return false, errors.Errorf("node type is empty for node %q", node.Name)
}
if node.Type != "dir" {
2018-08-19 10:28:06 +00:00
ignore, err := walkFn(parentTreeID, p, node, nil)
2018-06-09 16:19:27 +00:00
if err != nil {
if err == ErrSkipNode {
2018-06-09 16:19:27 +00:00
// skip the remaining entries in this tree
return allNodesIgnored, nil
}
return false, err
}
2020-03-06 22:35:09 +00:00
if !ignore {
2018-06-09 16:19:27 +00:00
allNodesIgnored = false
}
continue
}
if node.Subtree == nil {
return false, errors.Errorf("subtree for node %v in tree %v is nil", node.Name, p)
}
if ignoreTrees.Has(*node.Subtree) {
continue
}
subtree, err := repo.LoadTree(ctx, *node.Subtree)
2018-08-19 10:28:06 +00:00
ignore, err := walkFn(parentTreeID, p, node, err)
2018-06-09 16:19:27 +00:00
if err != nil {
if err == ErrSkipNode {
2018-06-09 16:19:27 +00:00
if ignore {
ignoreTrees.Insert(*node.Subtree)
}
continue
}
return false, err
}
if ignore {
ignoreTrees.Insert(*node.Subtree)
}
if !ignore {
allNodesIgnored = false
}
2018-08-19 10:28:06 +00:00
ignore, err = walk(ctx, repo, p, *node.Subtree, subtree, ignoreTrees, walkFn)
2018-06-09 16:19:27 +00:00
if err != nil {
return false, err
}
if ignore {
ignoreTrees.Insert(*node.Subtree)
}
if !ignore {
allNodesIgnored = false
}
}
return allNodesIgnored, nil
}