mirror of
https://github.com/restic/restic.git
synced 2024-12-24 08:44:52 +00:00
5cdcc99eba
Since backend.ID is always a slice of constant length, use an array instead of a slice. Mostly, arrays behave as slices, except that an array cannot be nil, so use `*backend.ID` insteaf of `backend.ID` in places where the absence of an ID is possible (e.g. for the Subtree of a Node, which may not present when the node is a file node). This change allows to directly use backend.ID as the the key for a map, so that arbitrary data structures (e.g. a Set implemented as a map[backend.ID]struct{}) can easily be formed.
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package restic
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/restic/restic/backend"
|
|
"github.com/restic/restic/debug"
|
|
"github.com/restic/restic/repository"
|
|
)
|
|
|
|
type WalkTreeJob struct {
|
|
Path string
|
|
Error error
|
|
|
|
Node *Node
|
|
Tree *Tree
|
|
}
|
|
|
|
func walkTree(repo *repository.Repository, path string, treeID backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
|
|
debug.Log("walkTree", "start on %q (%v)", path, treeID.Str())
|
|
|
|
t, err := LoadTree(repo, treeID)
|
|
if err != nil {
|
|
select {
|
|
case jobCh <- WalkTreeJob{Path: path, Error: err}:
|
|
case <-done:
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
for _, node := range t.Nodes {
|
|
p := filepath.Join(path, node.Name)
|
|
if node.Type == "dir" {
|
|
walkTree(repo, p, *node.Subtree, done, jobCh)
|
|
} else {
|
|
select {
|
|
case jobCh <- WalkTreeJob{Path: p, Node: node}:
|
|
case <-done:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
select {
|
|
case jobCh <- WalkTreeJob{Path: path, Tree: t}:
|
|
case <-done:
|
|
return
|
|
}
|
|
|
|
debug.Log("walkTree", "done for %q (%v)", path, treeID.Str())
|
|
}
|
|
|
|
// WalkTree walks the tree specified by id recursively and sends a job for each
|
|
// file and directory it finds. When the channel done is closed, processing
|
|
// stops.
|
|
func WalkTree(repo *repository.Repository, id backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
|
|
debug.Log("WalkTree", "start on %v", id.Str())
|
|
walkTree(repo, "", id, done, jobCh)
|
|
close(jobCh)
|
|
debug.Log("WalkTree", "done")
|
|
}
|