restic/walk.go

63 lines
1.4 KiB
Go
Raw Normal View History

2015-03-02 14:48:47 +01:00
package restic
import (
"path/filepath"
"github.com/restic/restic/backend"
2015-03-02 14:48:47 +01:00
"github.com/restic/restic/debug"
"github.com/restic/restic/repository"
2015-03-02 14:48:47 +01:00
)
type WalkTreeJob struct {
Path string
Error error
Node *Node
Tree *Tree
}
2015-05-09 23:59:58 +02:00
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())
2015-05-09 13:32:52 +02:00
t, err := LoadTree(repo, treeID)
2015-03-02 14:48:47 +01:00
if err != nil {
2015-07-11 15:51:18 +02:00
select {
case jobCh <- WalkTreeJob{Path: path, Error: err}:
case <-done:
return
}
2015-03-02 14:48:47 +01:00
return
}
for _, node := range t.Nodes {
p := filepath.Join(path, node.Name)
if node.Type == "dir" {
walkTree(repo, p, *node.Subtree, done, jobCh)
2015-03-02 14:48:47 +01:00
} else {
2015-07-11 15:51:18 +02:00
select {
case jobCh <- WalkTreeJob{Path: p, Node: node}:
case <-done:
return
}
2015-03-02 14:48:47 +01:00
}
}
2015-07-11 15:51:18 +02:00
select {
case jobCh <- WalkTreeJob{Path: path, Tree: t}:
case <-done:
return
}
debug.Log("walkTree", "done for %q (%v)", path, treeID.Str())
2015-03-02 14:48:47 +01:00
}
// WalkTree walks the tree specified by id recursively and sends a job for each
2015-03-02 14:48:47 +01:00
// file and directory it finds. When the channel done is closed, processing
// stops.
2015-05-09 23:59:58 +02:00
func WalkTree(repo *repository.Repository, id backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
debug.Log("WalkTree", "start on %v", id.Str())
2015-05-09 13:32:52 +02:00
walkTree(repo, "", id, done, jobCh)
2015-03-02 14:48:47 +01:00
close(jobCh)
2015-03-08 21:21:31 +01:00
debug.Log("WalkTree", "done")
2015-03-02 14:48:47 +01:00
}