2016-08-14 15:59:20 +00:00
|
|
|
package list
|
2016-08-14 14:01:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"restic/backend"
|
|
|
|
"restic/pack"
|
|
|
|
"restic/worker"
|
|
|
|
)
|
|
|
|
|
|
|
|
const listPackWorkers = 10
|
|
|
|
|
|
|
|
// Lister combines lists packs in a repo and blobs in a pack.
|
|
|
|
type Lister interface {
|
|
|
|
List(backend.Type, <-chan struct{}) <-chan backend.ID
|
|
|
|
ListPack(backend.ID) ([]pack.Blob, int64, error)
|
|
|
|
}
|
|
|
|
|
2016-08-14 15:59:20 +00:00
|
|
|
// Result is returned in the channel from LoadBlobsFromAllPacks.
|
|
|
|
type Result struct {
|
2016-08-14 14:11:59 +00:00
|
|
|
packID backend.ID
|
|
|
|
size int64
|
|
|
|
entries []pack.Blob
|
|
|
|
}
|
|
|
|
|
|
|
|
// PackID returns the pack ID of this result.
|
2016-08-14 15:59:20 +00:00
|
|
|
func (l Result) PackID() backend.ID {
|
2016-08-14 14:11:59 +00:00
|
|
|
return l.packID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size ruturns the size of the pack.
|
2016-08-14 15:59:20 +00:00
|
|
|
func (l Result) Size() int64 {
|
2016-08-14 14:11:59 +00:00
|
|
|
return l.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entries returns a list of all blobs saved in the pack.
|
2016-08-14 15:59:20 +00:00
|
|
|
func (l Result) Entries() []pack.Blob {
|
2016-08-14 14:11:59 +00:00
|
|
|
return l.entries
|
2016-08-14 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 15:59:20 +00:00
|
|
|
// AllPacks sends the contents of all packs to ch.
|
|
|
|
func AllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) {
|
2016-08-14 14:01:42 +00:00
|
|
|
f := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
|
|
|
|
packID := job.Data.(backend.ID)
|
|
|
|
entries, size, err := repo.ListPack(packID)
|
|
|
|
|
2016-08-14 15:59:20 +00:00
|
|
|
return Result{
|
2016-08-14 14:11:59 +00:00
|
|
|
packID: packID,
|
|
|
|
size: size,
|
|
|
|
entries: entries,
|
2016-08-14 14:01:42 +00:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jobCh := make(chan worker.Job)
|
|
|
|
wp := worker.New(listPackWorkers, f, jobCh, ch)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(jobCh)
|
|
|
|
for id := range repo.List(backend.Data, done) {
|
|
|
|
select {
|
|
|
|
case jobCh <- worker.Job{Data: id}:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wp.Wait()
|
|
|
|
}
|