2015-03-02 13:48:47 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2015-10-27 21:44:10 +00:00
|
|
|
"sync"
|
2015-03-02 13:48:47 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
"github.com/restic/restic/backend"
|
2015-03-02 13:48:47 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2015-10-27 21:44:10 +00:00
|
|
|
"github.com/restic/restic/pack"
|
2015-03-02 13:48:47 +00:00
|
|
|
)
|
|
|
|
|
2015-10-27 21:44:10 +00:00
|
|
|
// WalkTreeJob is a job sent from the tree walker.
|
2015-03-02 13:48:47 +00:00
|
|
|
type WalkTreeJob struct {
|
|
|
|
Path string
|
|
|
|
Error error
|
|
|
|
|
|
|
|
Node *Node
|
|
|
|
Tree *Tree
|
|
|
|
}
|
|
|
|
|
2015-10-27 21:44:10 +00:00
|
|
|
// TreeWalker traverses a tree in the repository depth-first and sends a job
|
|
|
|
// for each item (file or dir) that it encounters.
|
|
|
|
type TreeWalker struct {
|
|
|
|
ch chan<- loadTreeJob
|
|
|
|
out chan<- WalkTreeJob
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTreeWalker uses ch to load trees from the repository and sends jobs to
|
|
|
|
// out.
|
|
|
|
func NewTreeWalker(ch chan<- loadTreeJob, out chan<- WalkTreeJob) *TreeWalker {
|
|
|
|
return &TreeWalker{ch: ch, out: out}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk starts walking the tree given by id. When the channel done is closed,
|
|
|
|
// processing stops.
|
|
|
|
func (tw *TreeWalker) Walk(path string, id backend.ID, done chan struct{}) {
|
|
|
|
debug.Log("TreeWalker.Walk", "starting on tree %v for %v", id.Str(), path)
|
|
|
|
defer debug.Log("TreeWalker.Walk", "done walking tree %v for %v", id.Str(), path)
|
2015-04-30 00:59:06 +00:00
|
|
|
|
2015-10-27 21:44:10 +00:00
|
|
|
resCh := make(chan loadTreeResult, 1)
|
|
|
|
tw.ch <- loadTreeJob{
|
|
|
|
id: id,
|
|
|
|
res: resCh,
|
|
|
|
}
|
|
|
|
|
|
|
|
res := <-resCh
|
|
|
|
if res.err != nil {
|
2015-07-11 13:51:18 +00:00
|
|
|
select {
|
2015-10-27 21:44:10 +00:00
|
|
|
case tw.out <- WalkTreeJob{Path: path, Error: res.err}:
|
2015-07-11 13:51:18 +00:00
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2015-03-02 13:48:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-27 21:44:10 +00:00
|
|
|
tw.walk(path, res.tree, done)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case tw.out <- WalkTreeJob{Path: path, Tree: res.tree}:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tw *TreeWalker) walk(path string, tree *Tree, done chan struct{}) {
|
|
|
|
debug.Log("TreeWalker.walk", "start on %q", path)
|
|
|
|
defer debug.Log("TreeWalker.walk", "done for %q", path)
|
|
|
|
|
|
|
|
// load all subtrees in parallel
|
|
|
|
results := make([]<-chan loadTreeResult, len(tree.Nodes))
|
|
|
|
for i, node := range tree.Nodes {
|
|
|
|
if node.Type == "dir" {
|
|
|
|
resCh := make(chan loadTreeResult, 1)
|
|
|
|
tw.ch <- loadTreeJob{
|
|
|
|
id: *node.Subtree,
|
|
|
|
res: resCh,
|
|
|
|
}
|
|
|
|
|
|
|
|
results[i] = resCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, node := range tree.Nodes {
|
2015-03-02 13:48:47 +00:00
|
|
|
p := filepath.Join(path, node.Name)
|
2015-10-27 21:44:10 +00:00
|
|
|
var job WalkTreeJob
|
|
|
|
|
2015-03-02 13:48:47 +00:00
|
|
|
if node.Type == "dir" {
|
2015-10-27 21:44:10 +00:00
|
|
|
if results[i] == nil {
|
|
|
|
panic("result chan should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
res := <-results[i]
|
|
|
|
tw.walk(p, res.tree, done)
|
|
|
|
|
|
|
|
job = WalkTreeJob{Path: p, Tree: res.tree, Error: res.err}
|
2015-03-02 13:48:47 +00:00
|
|
|
} else {
|
2015-10-27 21:44:10 +00:00
|
|
|
job = WalkTreeJob{Path: p, Node: node}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case tw.out <- job:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type loadTreeResult struct {
|
|
|
|
tree *Tree
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type loadTreeJob struct {
|
|
|
|
id backend.ID
|
|
|
|
res chan<- loadTreeResult
|
|
|
|
}
|
|
|
|
|
|
|
|
type treeLoader func(backend.ID) (*Tree, error)
|
|
|
|
|
|
|
|
func loadTreeWorker(wg *sync.WaitGroup, in <-chan loadTreeJob, load treeLoader, done <-chan struct{}) {
|
|
|
|
debug.Log("loadTreeWorker", "start")
|
|
|
|
defer debug.Log("loadTreeWorker", "exit")
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
debug.Log("loadTreeWorker", "done channel closed")
|
|
|
|
return
|
|
|
|
case job, ok := <-in:
|
|
|
|
if !ok {
|
|
|
|
debug.Log("loadTreeWorker", "input channel closed, exiting")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("loadTreeWorker", "received job to load tree %v", job.id.Str())
|
|
|
|
tree, err := load(job.id)
|
|
|
|
|
|
|
|
debug.Log("loadTreeWorker", "tree %v loaded, error %v", job.id.Str(), err)
|
|
|
|
|
2015-07-11 13:51:18 +00:00
|
|
|
select {
|
2015-10-27 21:44:10 +00:00
|
|
|
case job.res <- loadTreeResult{tree, err}:
|
|
|
|
debug.Log("loadTreeWorker", "job result sent")
|
2015-07-11 13:51:18 +00:00
|
|
|
case <-done:
|
2015-10-27 21:44:10 +00:00
|
|
|
debug.Log("loadTreeWorker", "done channel closed before result could be sent")
|
2015-07-11 13:51:18 +00:00
|
|
|
return
|
|
|
|
}
|
2015-03-02 13:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 21:44:10 +00:00
|
|
|
const loadTreeWorkers = 10
|
|
|
|
|
2015-04-30 00:59:06 +00:00
|
|
|
// WalkTree walks the tree specified by id recursively and sends a job for each
|
2015-03-02 13:48:47 +00:00
|
|
|
// file and directory it finds. When the channel done is closed, processing
|
|
|
|
// stops.
|
2015-10-26 19:49:01 +00:00
|
|
|
func WalkTree(repo TreeLoader, id backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
|
2015-10-27 21:44:10 +00:00
|
|
|
debug.Log("WalkTree", "start on %v, start workers", id.Str())
|
|
|
|
|
|
|
|
load := func(id backend.ID) (*Tree, error) {
|
|
|
|
tree := &Tree{}
|
|
|
|
err := repo.LoadJSONPack(pack.Tree, id, tree)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan loadTreeJob)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < loadTreeWorkers; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go loadTreeWorker(&wg, ch, load, done)
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := NewTreeWalker(ch, jobCh)
|
|
|
|
tw.Walk("", id, done)
|
2015-03-02 13:48:47 +00:00
|
|
|
close(jobCh)
|
2015-10-27 21:44:10 +00:00
|
|
|
|
|
|
|
close(ch)
|
|
|
|
wg.Wait()
|
|
|
|
|
2015-03-08 20:21:31 +00:00
|
|
|
debug.Log("WalkTree", "done")
|
2015-03-02 13:48:47 +00:00
|
|
|
}
|