2015-06-28 22:22:25 +00:00
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2015-07-11 14:00:49 +00:00
|
|
|
"fmt"
|
2017-02-06 20:17:52 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2015-07-11 22:25:42 +00:00
|
|
|
"sync"
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2018-10-28 20:12:15 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/pack"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2018-10-28 20:12:15 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2015-06-28 22:22:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Checker runs various checks on a repository. It is advisable to create an
|
|
|
|
// exclusive Lock in the repository before running any checks.
|
|
|
|
//
|
|
|
|
// A Checker only tests for internal errors within the data structures of the
|
|
|
|
// repository (e.g. missing blobs), and needs a valid Repository to work on.
|
|
|
|
type Checker struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
packs restic.IDSet
|
2015-07-12 14:42:22 +00:00
|
|
|
blobRefs struct {
|
|
|
|
sync.Mutex
|
2019-07-13 16:34:55 +00:00
|
|
|
// see flags below
|
2020-04-18 17:46:33 +00:00
|
|
|
M map[restic.BlobHandle]blobStatus
|
2015-07-12 14:42:22 +00:00
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
masterIndex *repository.MasterIndex
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
repo restic.Repository
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 16:34:55 +00:00
|
|
|
type blobStatus uint8
|
|
|
|
|
|
|
|
const (
|
2020-04-18 17:46:33 +00:00
|
|
|
blobStatusExists blobStatus = 1 << iota
|
|
|
|
blobStatusReferenced
|
2019-07-13 16:34:55 +00:00
|
|
|
)
|
|
|
|
|
2015-06-28 22:22:25 +00:00
|
|
|
// New returns a new checker which runs on repo.
|
2016-08-31 21:07:50 +00:00
|
|
|
func New(repo restic.Repository) *Checker {
|
2015-07-12 14:42:22 +00:00
|
|
|
c := &Checker{
|
2016-08-31 20:39:36 +00:00
|
|
|
packs: restic.NewIDSet(),
|
2015-10-12 20:34:12 +00:00
|
|
|
masterIndex: repository.NewMasterIndex(),
|
2015-06-28 22:22:25 +00:00
|
|
|
repo: repo,
|
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
|
2020-04-18 17:46:33 +00:00
|
|
|
c.blobRefs.M = make(map[restic.BlobHandle]blobStatus)
|
2015-07-12 14:42:22 +00:00
|
|
|
|
|
|
|
return c
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 20:49:30 +00:00
|
|
|
const defaultParallelism = 5
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2015-10-25 15:00:06 +00:00
|
|
|
// ErrDuplicatePacks is returned when a pack is found in more than one index.
|
|
|
|
type ErrDuplicatePacks struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
PackID restic.ID
|
|
|
|
Indexes restic.IDSet
|
2015-10-25 15:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrDuplicatePacks) Error() string {
|
|
|
|
return fmt.Sprintf("pack %v contained in several indexes: %v", e.PackID.Str(), e.Indexes)
|
|
|
|
}
|
|
|
|
|
2015-11-08 20:50:48 +00:00
|
|
|
// ErrOldIndexFormat is returned when an index with the old format is
|
|
|
|
// found.
|
|
|
|
type ErrOldIndexFormat struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
restic.ID
|
2015-11-08 20:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrOldIndexFormat) Error() string {
|
|
|
|
return fmt.Sprintf("index %v has old format", err.ID.Str())
|
|
|
|
}
|
|
|
|
|
2015-06-28 22:22:25 +00:00
|
|
|
// LoadIndex loads all index files.
|
2017-06-04 09:16:55 +00:00
|
|
|
func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Start")
|
2019-03-24 20:27:28 +00:00
|
|
|
|
|
|
|
// track spawned goroutines using wg, create a new context which is
|
|
|
|
// cancelled as soon as an error occurs.
|
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
type FileInfo struct {
|
|
|
|
restic.ID
|
|
|
|
Size int64
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
type Result struct {
|
|
|
|
*repository.Index
|
|
|
|
restic.ID
|
|
|
|
Err error
|
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
ch := make(chan FileInfo)
|
|
|
|
resultCh := make(chan Result)
|
|
|
|
|
|
|
|
// send list of index files through ch, which is closed afterwards
|
|
|
|
wg.Go(func() error {
|
|
|
|
defer close(ch)
|
|
|
|
return c.repo.List(ctx, restic.IndexFile, func(id restic.ID, size int64) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case ch <- FileInfo{id, size}:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
2015-11-08 20:50:48 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
// a worker receives an index ID from ch, loads the index, and sends it to indexCh
|
|
|
|
worker := func() error {
|
2019-03-24 21:12:38 +00:00
|
|
|
var buf []byte
|
2019-03-24 20:27:28 +00:00
|
|
|
for fi := range ch {
|
|
|
|
debug.Log("worker got file %v", fi.ID.Str())
|
2019-03-24 21:12:38 +00:00
|
|
|
var err error
|
|
|
|
var idx *repository.Index
|
2020-10-13 18:39:54 +00:00
|
|
|
oldFormat := false
|
|
|
|
|
|
|
|
buf, err = c.repo.LoadAndDecrypt(ctx, buf[:0], restic.IndexFile, fi.ID)
|
|
|
|
if err == nil {
|
2020-10-17 07:06:10 +00:00
|
|
|
idx, oldFormat, err = repository.DecodeIndex(buf, fi.ID)
|
2020-10-13 18:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if oldFormat {
|
2019-03-24 20:27:28 +00:00
|
|
|
debug.Log("index %v has old format", fi.ID.Str())
|
|
|
|
hints = append(hints, ErrOldIndexFormat{fi.ID})
|
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
err = errors.Wrapf(err, "error loading index %v", fi.ID.Str())
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
select {
|
|
|
|
case resultCh <- Result{idx, fi.ID, err}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
// run workers on ch
|
|
|
|
wg.Go(func() error {
|
2020-10-14 12:23:06 +00:00
|
|
|
defer close(resultCh)
|
|
|
|
return repository.RunWorkers(defaultParallelism, worker)
|
2019-03-24 20:27:28 +00:00
|
|
|
})
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
// receive decoded indexes
|
2016-08-31 20:39:36 +00:00
|
|
|
packToIndex := make(map[restic.ID]restic.IDSet)
|
2019-03-24 20:27:28 +00:00
|
|
|
wg.Go(func() error {
|
|
|
|
for res := range resultCh {
|
|
|
|
debug.Log("process index %v, err %v", res.ID, res.Err)
|
2015-10-25 15:00:06 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
errs = append(errs, res.Err)
|
|
|
|
continue
|
|
|
|
}
|
2015-10-25 15:00:06 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
c.masterIndex.Insert(res.Index)
|
|
|
|
|
|
|
|
debug.Log("process blobs")
|
|
|
|
cnt := 0
|
|
|
|
for blob := range res.Index.Each(ctx) {
|
|
|
|
c.packs.Insert(blob.PackID)
|
2020-04-18 17:46:33 +00:00
|
|
|
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
|
|
|
|
c.blobRefs.M[h] = blobStatusExists
|
2019-03-24 20:27:28 +00:00
|
|
|
cnt++
|
|
|
|
|
|
|
|
if _, ok := packToIndex[blob.PackID]; !ok {
|
|
|
|
packToIndex[blob.PackID] = restic.NewIDSet()
|
|
|
|
}
|
|
|
|
packToIndex[blob.PackID].Insert(res.ID)
|
2015-10-25 15:00:06 +00:00
|
|
|
}
|
2019-03-24 20:27:28 +00:00
|
|
|
|
|
|
|
debug.Log("%d blobs processed", cnt)
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
2019-03-24 20:27:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
err := wg.Wait()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("checking for duplicate packs")
|
2015-10-25 15:00:06 +00:00
|
|
|
for packID := range c.packs {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log(" check pack %v: contained in %d indexes", packID, len(packToIndex[packID]))
|
2015-10-25 15:00:06 +00:00
|
|
|
if len(packToIndex[packID]) > 1 {
|
2015-10-25 15:26:50 +00:00
|
|
|
hints = append(hints, ErrDuplicatePacks{
|
2015-10-25 15:00:06 +00:00
|
|
|
PackID: packID,
|
|
|
|
Indexes: packToIndex[packID],
|
2015-10-25 15:26:50 +00:00
|
|
|
})
|
2015-10-25 15:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 05:06:14 +00:00
|
|
|
c.masterIndex.MergeFinalIndexes()
|
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
err = c.repo.SetIndex(c.masterIndex)
|
2018-03-31 08:02:09 +00:00
|
|
|
if err != nil {
|
|
|
|
debug.Log("SetIndex returned error: %v", err)
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-10-25 15:26:50 +00:00
|
|
|
return hints, errs
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 14:00:49 +00:00
|
|
|
// PackError describes an error with a specific pack.
|
|
|
|
type PackError struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
ID restic.ID
|
2015-07-12 15:09:48 +00:00
|
|
|
Orphaned bool
|
|
|
|
Err error
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e PackError) Error() string {
|
2018-04-07 08:07:54 +00:00
|
|
|
return "pack " + e.ID.Str() + ": " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOrphanedPack returns true if the error describes a pack which is not
|
|
|
|
// contained in any index.
|
|
|
|
func IsOrphanedPack(err error) bool {
|
|
|
|
if e, ok := errors.Cause(err).(PackError); ok && e.Orphaned {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 22:25:42 +00:00
|
|
|
// Packs checks that all packs referenced in the index are still available and
|
2015-07-11 23:44:19 +00:00
|
|
|
// there are no packs that aren't in an index. errChan is closed after all
|
|
|
|
// packs have been checked.
|
2017-06-04 09:16:55 +00:00
|
|
|
func (c *Checker) Packs(ctx context.Context, errChan chan<- error) {
|
2015-07-11 23:44:19 +00:00
|
|
|
defer close(errChan)
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("checking for %d packs", len(c.packs))
|
2015-07-11 22:25:42 +00:00
|
|
|
|
2018-01-12 02:00:48 +00:00
|
|
|
debug.Log("listing repository packs")
|
|
|
|
repoPacks := restic.NewIDSet()
|
2018-01-21 16:25:36 +00:00
|
|
|
|
2020-08-16 09:16:38 +00:00
|
|
|
err := c.repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
|
2018-01-12 02:00:48 +00:00
|
|
|
repoPacks.Insert(id)
|
2018-01-21 16:25:36 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
2015-07-11 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 02:00:48 +00:00
|
|
|
// orphaned: present in the repo but not in c.packs
|
|
|
|
for orphanID := range repoPacks.Sub(c.packs) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case errChan <- PackError{ID: orphanID, Orphaned: true, Err: errors.New("not referenced in any index")}:
|
|
|
|
}
|
2015-07-11 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 02:00:48 +00:00
|
|
|
// missing: present in c.packs but not in the repo
|
|
|
|
for missingID := range c.packs.Sub(repoPacks) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case errChan <- PackError{ID: missingID, Err: errors.New("does not exist")}:
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error is an error that occurred while checking a repository.
|
|
|
|
type Error struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
TreeID restic.ID
|
|
|
|
BlobID restic.ID
|
2015-07-11 14:00:49 +00:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Error) Error() string {
|
2016-02-26 22:06:25 +00:00
|
|
|
if !e.BlobID.IsNull() && !e.TreeID.IsNull() {
|
2015-10-11 17:25:02 +00:00
|
|
|
msg := "tree " + e.TreeID.Str()
|
|
|
|
msg += ", blob " + e.BlobID.Str()
|
2015-07-11 14:00:49 +00:00
|
|
|
msg += ": " + e.Err.Error()
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:06:25 +00:00
|
|
|
if !e.TreeID.IsNull() {
|
2015-10-11 17:25:02 +00:00
|
|
|
return "tree " + e.TreeID.Str() + ": " + e.Err.Error()
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
func loadTreeFromSnapshot(ctx context.Context, repo restic.Repository, id restic.ID) (restic.ID, error) {
|
|
|
|
sn, err := restic.LoadSnapshot(ctx, repo, id)
|
2015-07-11 14:00:49 +00:00
|
|
|
if err != nil {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("error loading snapshot %v: %v", id, err)
|
2016-08-31 20:39:36 +00:00
|
|
|
return restic.ID{}, err
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sn.Tree == nil {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("snapshot %v has no tree", id)
|
2016-08-31 20:39:36 +00:00
|
|
|
return restic.ID{}, errors.Errorf("snapshot %v has no tree", id)
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
return *sn.Tree, nil
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
// loadSnapshotTreeIDs loads all snapshots from backend and returns the tree IDs.
|
2017-06-04 09:16:55 +00:00
|
|
|
func loadSnapshotTreeIDs(ctx context.Context, repo restic.Repository) (restic.IDs, []error) {
|
2015-07-12 14:42:22 +00:00
|
|
|
var trees struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
IDs restic.IDs
|
2015-07-12 14:42:22 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs struct {
|
|
|
|
errs []error
|
|
|
|
sync.Mutex
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
// track spawned goroutines using wg, create a new context which is
|
|
|
|
// cancelled as soon as an error occurs.
|
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
ch := make(chan restic.ID)
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
// send list of index files through ch, which is closed afterwards
|
|
|
|
wg.Go(func() error {
|
|
|
|
defer close(ch)
|
|
|
|
return repo.List(ctx, restic.SnapshotFile, func(id restic.ID, size int64) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case ch <- id:
|
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
return nil
|
2019-03-24 20:27:28 +00:00
|
|
|
})
|
|
|
|
})
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
// a worker receives an index ID from ch, loads the snapshot and the tree,
|
|
|
|
// and adds the result to errs and trees.
|
|
|
|
worker := func() error {
|
|
|
|
for id := range ch {
|
|
|
|
debug.Log("load snapshot %v", id)
|
|
|
|
|
|
|
|
treeID, err := loadTreeFromSnapshot(ctx, repo, id)
|
|
|
|
if err != nil {
|
|
|
|
errs.Lock()
|
|
|
|
errs.errs = append(errs.errs, err)
|
|
|
|
errs.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
debug.Log("snapshot %v has tree %v", id, treeID)
|
|
|
|
trees.Lock()
|
|
|
|
trees.IDs = append(trees.IDs, treeID)
|
|
|
|
trees.Unlock()
|
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
return nil
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 20:27:28 +00:00
|
|
|
for i := 0; i < defaultParallelism; i++ {
|
|
|
|
wg.Go(worker)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := wg.Wait()
|
2015-07-12 14:42:22 +00:00
|
|
|
if err != nil {
|
|
|
|
errs.errs = append(errs.errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return trees.IDs, errs.errs
|
|
|
|
}
|
|
|
|
|
2015-10-11 17:13:35 +00:00
|
|
|
// TreeError collects several errors that occurred while processing a tree.
|
2015-07-12 14:42:22 +00:00
|
|
|
type TreeError struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
ID restic.ID
|
2015-07-12 14:42:22 +00:00
|
|
|
Errors []error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e TreeError) Error() string {
|
2015-12-02 21:40:36 +00:00
|
|
|
return fmt.Sprintf("tree %v: %v", e.ID.Str(), e.Errors)
|
2015-07-12 14:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type treeJob struct {
|
2016-08-31 20:39:36 +00:00
|
|
|
restic.ID
|
2015-07-12 14:42:22 +00:00
|
|
|
error
|
|
|
|
*restic.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadTreeWorker loads trees from repo and sends them to out.
|
2017-06-04 09:16:55 +00:00
|
|
|
func loadTreeWorker(ctx context.Context, repo restic.Repository,
|
2016-08-31 20:39:36 +00:00
|
|
|
in <-chan restic.ID, out chan<- treeJob,
|
2017-06-04 09:16:55 +00:00
|
|
|
wg *sync.WaitGroup) {
|
2015-07-12 14:42:22 +00:00
|
|
|
|
|
|
|
defer func() {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("exiting")
|
2015-07-12 14:42:22 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
inCh = in
|
|
|
|
outCh = out
|
|
|
|
job treeJob
|
|
|
|
)
|
|
|
|
|
|
|
|
outCh = nil
|
|
|
|
for {
|
2015-07-11 23:44:19 +00:00
|
|
|
select {
|
2017-06-04 09:16:55 +00:00
|
|
|
case <-ctx.Done():
|
2015-07-11 23:44:19 +00:00
|
|
|
return
|
2015-07-12 14:42:22 +00:00
|
|
|
|
|
|
|
case treeID, ok := <-inCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("load tree %v", treeID)
|
2015-07-12 14:42:22 +00:00
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
tree, err := repo.LoadTree(ctx, treeID)
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("load tree %v (%v) returned err: %v", tree, treeID, err)
|
2015-07-12 14:42:22 +00:00
|
|
|
job = treeJob{ID: treeID, error: err, Tree: tree}
|
|
|
|
outCh = out
|
|
|
|
inCh = nil
|
|
|
|
|
|
|
|
case outCh <- job:
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("sent tree %v", job.ID)
|
2015-07-12 14:42:22 +00:00
|
|
|
outCh = nil
|
|
|
|
inCh = in
|
2015-07-11 23:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
// checkTreeWorker checks the trees received and sends out errors to errChan.
|
2017-06-04 09:16:55 +00:00
|
|
|
func (c *Checker) checkTreeWorker(ctx context.Context, in <-chan treeJob, out chan<- error, wg *sync.WaitGroup) {
|
2015-07-12 14:42:22 +00:00
|
|
|
defer func() {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("exiting")
|
2015-07-12 14:42:22 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
var (
|
|
|
|
inCh = in
|
|
|
|
outCh = out
|
|
|
|
treeError TreeError
|
|
|
|
)
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
outCh = nil
|
|
|
|
for {
|
|
|
|
select {
|
2017-06-04 09:16:55 +00:00
|
|
|
case <-ctx.Done():
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("done channel closed, exiting")
|
2015-07-12 14:42:22 +00:00
|
|
|
return
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
case job, ok := <-inCh:
|
|
|
|
if !ok {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("input channel closed, exiting")
|
2015-07-12 14:42:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("check tree %v (tree %v, err %v)", job.ID, job.Tree, job.error)
|
2015-10-11 17:13:35 +00:00
|
|
|
|
|
|
|
var errs []error
|
|
|
|
if job.error != nil {
|
|
|
|
errs = append(errs, job.error)
|
|
|
|
} else {
|
|
|
|
errs = c.checkTree(job.ID, job.Tree)
|
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("checked tree %v: %v errors", job.ID, len(errs))
|
2015-07-12 14:42:22 +00:00
|
|
|
treeError = TreeError{ID: job.ID, Errors: errs}
|
|
|
|
outCh = out
|
|
|
|
inCh = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case outCh <- treeError:
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("tree %v: sent %d errors", treeError.ID, len(treeError.Errors))
|
2015-07-12 14:42:22 +00:00
|
|
|
outCh = nil
|
|
|
|
inCh = in
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:34:55 +00:00
|
|
|
func (c *Checker) filterTrees(ctx context.Context, backlog restic.IDs, loaderChan chan<- restic.ID, in <-chan treeJob, out chan<- treeJob) {
|
2015-07-12 14:42:22 +00:00
|
|
|
defer func() {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("closing output channels")
|
2015-07-12 14:42:22 +00:00
|
|
|
close(loaderChan)
|
|
|
|
close(out)
|
|
|
|
}()
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
var (
|
|
|
|
inCh = in
|
|
|
|
outCh = out
|
|
|
|
loadCh = loaderChan
|
|
|
|
job treeJob
|
2016-08-31 20:39:36 +00:00
|
|
|
nextTreeID restic.ID
|
2015-07-12 14:42:22 +00:00
|
|
|
outstandingLoadTreeJobs = 0
|
|
|
|
)
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
outCh = nil
|
|
|
|
loadCh = nil
|
|
|
|
|
|
|
|
for {
|
|
|
|
if loadCh == nil && len(backlog) > 0 {
|
2019-07-06 18:01:48 +00:00
|
|
|
// process last added ids first, that is traverse the tree in depth-first order
|
|
|
|
ln := len(backlog) - 1
|
|
|
|
nextTreeID, backlog = backlog[ln], backlog[:ln]
|
2019-07-13 16:34:55 +00:00
|
|
|
|
|
|
|
// use a separate flag for processed trees to ensure that check still processes trees
|
|
|
|
// even when a file references a tree blob
|
|
|
|
c.blobRefs.Lock()
|
2020-04-18 17:46:33 +00:00
|
|
|
h := restic.BlobHandle{ID: nextTreeID, Type: restic.TreeBlob}
|
|
|
|
status := c.blobRefs.M[h]
|
2019-07-13 16:34:55 +00:00
|
|
|
c.blobRefs.Unlock()
|
2020-04-18 17:46:33 +00:00
|
|
|
if (status & blobStatusReferenced) != 0 {
|
2019-07-06 16:35:03 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-07-13 16:34:55 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
loadCh = loaderChan
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
if loadCh == nil && outCh == nil && outstandingLoadTreeJobs == 0 {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("backlog is empty, all channels nil, exiting")
|
2015-07-12 14:42:22 +00:00
|
|
|
return
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
select {
|
2017-06-04 09:16:55 +00:00
|
|
|
case <-ctx.Done():
|
2015-07-12 14:42:22 +00:00
|
|
|
return
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
case loadCh <- nextTreeID:
|
|
|
|
outstandingLoadTreeJobs++
|
|
|
|
loadCh = nil
|
2019-07-13 16:34:55 +00:00
|
|
|
c.blobRefs.Lock()
|
2020-04-18 17:46:33 +00:00
|
|
|
h := restic.BlobHandle{ID: nextTreeID, Type: restic.TreeBlob}
|
|
|
|
c.blobRefs.M[h] |= blobStatusReferenced
|
2019-07-13 16:34:55 +00:00
|
|
|
c.blobRefs.Unlock()
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
case j, ok := <-inCh:
|
|
|
|
if !ok {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("input channel closed")
|
2015-07-12 14:42:22 +00:00
|
|
|
inCh = nil
|
|
|
|
in = nil
|
|
|
|
continue
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
|
|
|
|
outstandingLoadTreeJobs--
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("input job tree %v", j.ID)
|
2015-10-11 16:45:16 +00:00
|
|
|
|
|
|
|
if j.error != nil {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("received job with error: %v (tree %v, ID %v)", j.error, j.Tree, j.ID)
|
2015-10-11 16:45:16 +00:00
|
|
|
} else if j.Tree == nil {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("received job with nil tree pointer: %v (ID %v)", j.error, j.ID)
|
2020-03-07 13:10:34 +00:00
|
|
|
// send a new job with the new error instead of the old one
|
|
|
|
j = treeJob{ID: j.ID, error: errors.New("tree is nil and error is nil")}
|
2015-10-11 16:45:16 +00:00
|
|
|
} else {
|
2019-07-06 16:12:48 +00:00
|
|
|
subtrees := j.Tree.Subtrees()
|
|
|
|
debug.Log("subtrees for tree %v: %v", j.ID, subtrees)
|
2019-07-06 18:01:48 +00:00
|
|
|
// iterate backwards over subtree to compensate backwards traversal order of nextTreeID selection
|
|
|
|
for i := len(subtrees) - 1; i >= 0; i-- {
|
|
|
|
id := subtrees[i]
|
2015-10-11 16:45:16 +00:00
|
|
|
if id.IsNull() {
|
2015-10-11 17:13:35 +00:00
|
|
|
// We do not need to raise this error here, it is
|
|
|
|
// checked when the tree is checked. Just make sure
|
|
|
|
// that we do not add any null IDs to the backlog.
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("tree %v has nil subtree", j.ID)
|
2015-10-11 16:45:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
backlog = append(backlog, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
job = j
|
|
|
|
outCh = out
|
|
|
|
inCh = nil
|
|
|
|
|
|
|
|
case outCh <- job:
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("tree sent to check: %v", job.ID)
|
2015-07-12 14:42:22 +00:00
|
|
|
outCh = nil
|
|
|
|
inCh = in
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
2015-07-12 14:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
// Structure checks that for all snapshots all referenced data blobs and
|
|
|
|
// subtrees are available in the index. errChan is closed after all trees have
|
|
|
|
// been traversed.
|
2017-06-04 09:16:55 +00:00
|
|
|
func (c *Checker) Structure(ctx context.Context, errChan chan<- error) {
|
2015-07-12 14:42:22 +00:00
|
|
|
defer close(errChan)
|
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
trees, errs := loadSnapshotTreeIDs(ctx, c.repo)
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("need to check %d trees from snapshots, %d errs returned", len(trees), len(errs))
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
for _, err := range errs {
|
|
|
|
select {
|
2017-06-04 09:16:55 +00:00
|
|
|
case <-ctx.Done():
|
2015-07-12 14:42:22 +00:00
|
|
|
return
|
|
|
|
case errChan <- err:
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
treeIDChan := make(chan restic.ID)
|
2015-07-12 14:42:22 +00:00
|
|
|
treeJobChan1 := make(chan treeJob)
|
|
|
|
treeJobChan2 := make(chan treeJob)
|
2015-07-11 14:00:49 +00:00
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < defaultParallelism; i++ {
|
|
|
|
wg.Add(2)
|
2017-06-04 09:16:55 +00:00
|
|
|
go loadTreeWorker(ctx, c.repo, treeIDChan, treeJobChan1, &wg)
|
|
|
|
go c.checkTreeWorker(ctx, treeJobChan2, errChan, &wg)
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 16:34:55 +00:00
|
|
|
c.filterTrees(ctx, trees, treeIDChan, treeJobChan1, treeJobChan2)
|
2015-07-12 14:42:22 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("checking tree %v", id)
|
2015-07-12 14:42:22 +00:00
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
var blobs []restic.ID
|
2015-07-12 14:42:22 +00:00
|
|
|
|
2015-10-11 18:55:28 +00:00
|
|
|
for _, node := range tree.Nodes {
|
2016-09-01 19:20:03 +00:00
|
|
|
switch node.Type {
|
2015-07-11 14:00:49 +00:00
|
|
|
case "file":
|
2016-04-10 14:51:16 +00:00
|
|
|
if node.Content == nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q has nil blob list", node.Name)})
|
2016-04-10 14:51:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 11:22:25 +00:00
|
|
|
var size uint64
|
2015-10-11 18:55:28 +00:00
|
|
|
for b, blobID := range node.Content {
|
|
|
|
if blobID.IsNull() {
|
2016-08-21 15:48:36 +00:00
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %d has null ID", node.Name, b)})
|
2015-10-11 18:55:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
blobs = append(blobs, blobID)
|
2018-03-31 11:22:25 +00:00
|
|
|
blobSize, found := c.repo.LookupBlobSize(blobID, restic.DataBlob)
|
|
|
|
if !found {
|
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %d size could not be found", node.Name, b)})
|
|
|
|
}
|
|
|
|
size += uint64(blobSize)
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
case "dir":
|
|
|
|
if node.Subtree == nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q has no subtree", node.Name)})
|
2015-07-11 14:00:49 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-10-11 16:46:26 +00:00
|
|
|
|
|
|
|
if node.Subtree.IsNull() {
|
2016-08-21 15:48:36 +00:00
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q subtree id is null", node.Name)})
|
2015-10-11 16:46:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-04-10 14:51:16 +00:00
|
|
|
|
2016-07-20 18:46:57 +00:00
|
|
|
case "symlink", "socket", "chardev", "dev", "fifo":
|
2016-05-08 21:16:01 +00:00
|
|
|
// nothing to check
|
|
|
|
|
2016-04-10 14:51:16 +00:00
|
|
|
default:
|
2016-09-01 19:20:03 +00:00
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.Errorf("node %q with invalid type %q", node.Name, node.Type)})
|
2016-04-10 14:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node.Name == "" {
|
|
|
|
errs = append(errs, Error{TreeID: id, Err: errors.New("node with empty name")})
|
2015-07-12 14:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blobID := range blobs {
|
|
|
|
c.blobRefs.Lock()
|
2020-04-18 17:46:33 +00:00
|
|
|
h := restic.BlobHandle{ID: blobID, Type: restic.DataBlob}
|
|
|
|
if (c.blobRefs.M[h] & blobStatusExists) == 0 {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("tree %v references blob %v which isn't contained in index", id, blobID)
|
2016-02-26 22:06:25 +00:00
|
|
|
errs = append(errs, Error{TreeID: id, BlobID: blobID, Err: errors.New("not found in index")})
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
2020-04-18 17:46:33 +00:00
|
|
|
c.blobRefs.M[h] |= blobStatusReferenced
|
2019-07-13 16:34:55 +00:00
|
|
|
debug.Log("blob %v is referenced", blobID)
|
|
|
|
c.blobRefs.Unlock()
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 14:42:22 +00:00
|
|
|
return errs
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnusedBlobs returns all blobs that have never been referenced.
|
2020-04-18 17:46:33 +00:00
|
|
|
func (c *Checker) UnusedBlobs() (blobs restic.BlobHandles) {
|
2015-07-12 14:42:22 +00:00
|
|
|
c.blobRefs.Lock()
|
|
|
|
defer c.blobRefs.Unlock()
|
|
|
|
|
2019-07-13 16:34:55 +00:00
|
|
|
debug.Log("checking %d blobs", len(c.blobRefs.M))
|
|
|
|
for id, flags := range c.blobRefs.M {
|
2020-04-18 17:46:33 +00:00
|
|
|
if (flags & blobStatusReferenced) == 0 {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("blob %v not referenced", id)
|
2015-07-25 15:05:45 +00:00
|
|
|
blobs = append(blobs, id)
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobs
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
2015-07-12 15:09:48 +00:00
|
|
|
|
2015-12-06 16:29:31 +00:00
|
|
|
// CountPacks returns the number of packs in the repository.
|
|
|
|
func (c *Checker) CountPacks() uint64 {
|
|
|
|
return uint64(len(c.packs))
|
|
|
|
}
|
|
|
|
|
2018-01-02 05:38:14 +00:00
|
|
|
// GetPacks returns IDSet of packs in the repository
|
|
|
|
func (c *Checker) GetPacks() restic.IDSet {
|
|
|
|
return c.packs
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:40:36 +00:00
|
|
|
// checkPack reads a pack and checks the integrity of all blobs.
|
2017-06-04 09:16:55 +00:00
|
|
|
func checkPack(ctx context.Context, r restic.Repository, id restic.ID) error {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("checking pack %v", id)
|
2020-08-16 09:16:38 +00:00
|
|
|
h := restic.Handle{Type: restic.PackFile, Name: id.String()}
|
2017-02-06 20:17:52 +00:00
|
|
|
|
2018-10-28 20:12:15 +00:00
|
|
|
packfile, hash, size, err := repository.DownloadAndHash(ctx, r.Backend(), h)
|
2017-02-06 20:17:52 +00:00
|
|
|
if err != nil {
|
2018-02-12 03:41:59 +00:00
|
|
|
return errors.Wrap(err, "checkPack")
|
2017-02-06 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2017-10-25 16:03:55 +00:00
|
|
|
_ = packfile.Close()
|
|
|
|
_ = os.Remove(packfile.Name())
|
2017-02-06 20:17:52 +00:00
|
|
|
}()
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("hash for pack %v is %v", id, hash)
|
2017-02-06 20:17:52 +00:00
|
|
|
|
2016-02-04 22:54:26 +00:00
|
|
|
if !hash.Equal(id) {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("Pack ID does not match, want %v, got %v", id, hash)
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("Pack ID does not match, want %v, got %v", id.Str(), hash.Str())
|
2016-02-04 22:54:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 20:17:52 +00:00
|
|
|
blobs, err := pack.List(r.Key(), packfile, size)
|
2015-12-02 21:40:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
2017-02-06 20:17:52 +00:00
|
|
|
var buf []byte
|
2016-08-25 19:08:16 +00:00
|
|
|
for i, blob := range blobs {
|
2017-02-06 20:17:52 +00:00
|
|
|
debug.Log(" check blob %d: %v", i, blob)
|
|
|
|
|
|
|
|
buf = buf[:cap(buf)]
|
|
|
|
if uint(len(buf)) < blob.Length {
|
|
|
|
buf = make([]byte, blob.Length)
|
|
|
|
}
|
|
|
|
buf = buf[:blob.Length]
|
|
|
|
|
|
|
|
_, err := packfile.Seek(int64(blob.Offset), 0)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("Seek(%v): %v", blob.Offset, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.ReadFull(packfile, buf)
|
|
|
|
if err != nil {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log(" error loading blob %v: %v", blob.ID, err)
|
2017-02-06 20:17:52 +00:00
|
|
|
errs = append(errs, errors.Errorf("blob %v: %v", i, err))
|
|
|
|
continue
|
|
|
|
}
|
2015-12-02 21:40:36 +00:00
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
nonce, ciphertext := buf[:r.Key().NonceSize()], buf[r.Key().NonceSize():]
|
|
|
|
plaintext, err := r.Key().Open(ciphertext[:0], nonce, ciphertext, nil)
|
2015-12-02 21:40:36 +00:00
|
|
|
if err != nil {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log(" error decrypting blob %v: %v", blob.ID, err)
|
2016-08-21 15:48:36 +00:00
|
|
|
errs = append(errs, errors.Errorf("blob %v: %v", i, err))
|
2015-12-02 21:40:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
hash := restic.Hash(plaintext)
|
2015-12-02 21:40:36 +00:00
|
|
|
if !hash.Equal(blob.ID) {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log(" Blob ID does not match, want %v, got %v", blob.ID, hash)
|
2016-08-21 15:48:36 +00:00
|
|
|
errs = append(errs, errors.Errorf("Blob ID does not match, want %v, got %v", blob.ID.Str(), hash.Str()))
|
2015-12-02 21:40:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("pack %v contains %v errors: %v", id.Str(), len(errs), errs)
|
2015-12-02 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadData loads all data from the repository and checks the integrity.
|
2017-06-04 09:16:55 +00:00
|
|
|
func (c *Checker) ReadData(ctx context.Context, p *restic.Progress, errChan chan<- error) {
|
2018-01-02 05:38:14 +00:00
|
|
|
c.ReadPacks(ctx, c.packs, p, errChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPacks loads data from specified packs and checks the integrity.
|
|
|
|
func (c *Checker) ReadPacks(ctx context.Context, packs restic.IDSet, p *restic.Progress, errChan chan<- error) {
|
2015-12-02 21:40:36 +00:00
|
|
|
defer close(errChan)
|
|
|
|
|
2015-12-06 16:29:31 +00:00
|
|
|
p.Start()
|
|
|
|
defer p.Done()
|
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
ch := make(chan restic.ID)
|
2015-12-02 21:40:36 +00:00
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
// run workers
|
|
|
|
for i := 0; i < defaultParallelism; i++ {
|
|
|
|
g.Go(func() error {
|
|
|
|
for {
|
|
|
|
var id restic.ID
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case id, ok = <-ch:
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 16:09:06 +00:00
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
err := checkPack(ctx, c.repo, id)
|
|
|
|
p.Report(restic.Stat{Blobs: 1})
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case errChan <- err:
|
|
|
|
}
|
2015-12-06 16:09:06 +00:00
|
|
|
}
|
2018-01-21 16:25:36 +00:00
|
|
|
})
|
2015-12-02 21:40:36 +00:00
|
|
|
}
|
2015-12-06 16:09:06 +00:00
|
|
|
|
2018-01-02 05:38:14 +00:00
|
|
|
// push packs to ch
|
|
|
|
for pack := range packs {
|
|
|
|
select {
|
|
|
|
case ch <- pack:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
err := g.Wait()
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case errChan <- err:
|
|
|
|
}
|
2015-12-06 16:09:06 +00:00
|
|
|
}
|
2015-12-02 21:40:36 +00:00
|
|
|
}
|