1
0
mirror of https://github.com/restic/restic.git synced 2024-07-06 09:20:53 +02:00
restic/src/restic/list/list.go

67 lines
1.3 KiB
Go
Raw Normal View History

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