2018-06-09 16:19:27 +00:00
|
|
|
package walker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
2020-07-28 20:32:57 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2020-07-28 20:32:57 +00:00
|
|
|
// 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.
|
2024-01-06 12:59:47 +00:00
|
|
|
type WalkFunc func(parentTreeID restic.ID, path string, node *restic.Node, nodeErr error) (err error)
|
2018-06-09 16:19:27 +00:00
|
|
|
|
2024-01-20 22:36:08 +00:00
|
|
|
type WalkVisitor struct {
|
|
|
|
// If the node is a `dir`, it will be entered afterwards unless `ErrSkipNode`
|
|
|
|
// was returned. This function is mandatory
|
|
|
|
ProcessNode WalkFunc
|
|
|
|
// Optional callback
|
|
|
|
LeaveDir func(path string)
|
|
|
|
}
|
|
|
|
|
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.
|
2024-01-20 22:36:08 +00:00
|
|
|
func Walk(ctx context.Context, repo restic.BlobLoader, root restic.ID, visitor WalkVisitor) error {
|
2022-06-12 12:38:19 +00:00
|
|
|
tree, err := restic.LoadTree(ctx, repo, root)
|
2024-01-20 22:36:08 +00:00
|
|
|
err = visitor.ProcessNode(root, "/", nil, err)
|
2018-06-09 16:19:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-28 20:32:57 +00:00
|
|
|
if err == ErrSkipNode {
|
2018-06-09 16:19:27 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:36:08 +00:00
|
|
|
return walk(ctx, repo, "/", root, tree, visitor)
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2024-01-20 22:36:08 +00:00
|
|
|
func walk(ctx context.Context, repo restic.BlobLoader, prefix string, parentTreeID restic.ID, tree *restic.Tree, visitor WalkVisitor) (err error) {
|
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 == "" {
|
2024-01-06 12:59:47 +00:00
|
|
|
return errors.Errorf("node type is empty for node %q", node.Name)
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node.Type != "dir" {
|
2024-01-20 22:36:08 +00:00
|
|
|
err := visitor.ProcessNode(parentTreeID, p, node, nil)
|
2018-06-09 16:19:27 +00:00
|
|
|
if err != nil {
|
2020-07-28 20:32:57 +00:00
|
|
|
if err == ErrSkipNode {
|
2018-06-09 16:19:27 +00:00
|
|
|
// skip the remaining entries in this tree
|
2024-01-21 15:43:32 +00:00
|
|
|
break
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 12:59:47 +00:00
|
|
|
return err
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Subtree == nil {
|
2024-01-06 12:59:47 +00:00
|
|
|
return errors.Errorf("subtree for node %v in tree %v is nil", node.Name, p)
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
subtree, err := restic.LoadTree(ctx, repo, *node.Subtree)
|
2024-01-20 22:36:08 +00:00
|
|
|
err = visitor.ProcessNode(parentTreeID, p, node, err)
|
2018-06-09 16:19:27 +00:00
|
|
|
if err != nil {
|
2020-07-28 20:32:57 +00:00
|
|
|
if err == ErrSkipNode {
|
2018-06-09 16:19:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:36:08 +00:00
|
|
|
err = walk(ctx, repo, p, *node.Subtree, subtree, visitor)
|
2018-06-09 16:19:27 +00:00
|
|
|
if err != nil {
|
2024-01-06 12:59:47 +00:00
|
|
|
return err
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:36:08 +00:00
|
|
|
if visitor.LeaveDir != nil {
|
|
|
|
visitor.LeaveDir(prefix)
|
|
|
|
}
|
|
|
|
|
2024-01-06 12:59:47 +00:00
|
|
|
return nil
|
2018-06-09 16:19:27 +00:00
|
|
|
}
|