| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | package checker | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2017-06-04 11:16:55 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-02-06 21:17:52 +01:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2021-09-04 14:08:53 +02:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2015-07-12 00:25:42 +02:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	"github.com/minio/sha256-simd" | 
					
						
							| 
									
										
										
										
											2021-11-07 22:33:44 +01:00
										 |  |  | 	"github.com/restic/restic/internal/backend" | 
					
						
							| 
									
										
										
										
											2017-07-23 14:21:03 +02:00
										 |  |  | 	"github.com/restic/restic/internal/debug" | 
					
						
							| 
									
										
										
										
											2018-10-28 21:12:15 +01:00
										 |  |  | 	"github.com/restic/restic/internal/errors" | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	"github.com/restic/restic/internal/hashing" | 
					
						
							| 
									
										
										
										
											2017-07-23 14:21:03 +02:00
										 |  |  | 	"github.com/restic/restic/internal/pack" | 
					
						
							|  |  |  | 	"github.com/restic/restic/internal/repository" | 
					
						
							| 
									
										
										
										
											2018-10-28 21:12:15 +01:00
										 |  |  | 	"github.com/restic/restic/internal/restic" | 
					
						
							| 
									
										
										
										
											2020-11-04 14:11:29 +01:00
										 |  |  | 	"github.com/restic/restic/internal/ui/progress" | 
					
						
							| 
									
										
										
										
											2018-10-28 21:12:15 +01:00
										 |  |  | 	"golang.org/x/sync/errgroup" | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02: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 { | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	packs    map[restic.ID]int64 | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	blobRefs struct { | 
					
						
							|  |  |  | 		sync.Mutex | 
					
						
							| 
									
										
										
										
											2020-11-06 23:41:04 +01:00
										 |  |  | 		M restic.BlobSet | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-07 00:07:32 +01:00
										 |  |  | 	trackUnused bool | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-12 22:34:12 +02:00
										 |  |  | 	masterIndex *repository.MasterIndex | 
					
						
							| 
									
										
										
										
											2021-11-07 22:33:44 +01:00
										 |  |  | 	snapshots   restic.Lister | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 23:07:50 +02:00
										 |  |  | 	repo restic.Repository | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // New returns a new checker which runs on repo. | 
					
						
							| 
									
										
										
										
											2020-11-07 00:07:32 +01:00
										 |  |  | func New(repo restic.Repository, trackUnused bool) *Checker { | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	c := &Checker{ | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 		packs:       make(map[restic.ID]int64), | 
					
						
							| 
									
										
										
										
											2015-10-12 22:34:12 +02:00
										 |  |  | 		masterIndex: repository.NewMasterIndex(), | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 		repo:        repo, | 
					
						
							| 
									
										
										
										
											2020-11-07 00:07:32 +01:00
										 |  |  | 		trackUnused: trackUnused, | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 23:41:04 +01:00
										 |  |  | 	c.blobRefs.M = restic.NewBlobSet() | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return c | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 21:49:30 +01:00
										 |  |  | const defaultParallelism = 5 | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | // ErrDuplicatePacks is returned when a pack is found in more than one index. | 
					
						
							|  |  |  | type ErrDuplicatePacks struct { | 
					
						
							| 
									
										
										
										
											2016-08-31 22:39:36 +02:00
										 |  |  | 	PackID  restic.ID | 
					
						
							|  |  |  | 	Indexes restic.IDSet | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e ErrDuplicatePacks) Error() string { | 
					
						
							|  |  |  | 	return fmt.Sprintf("pack %v contained in several indexes: %v", e.PackID.Str(), e.Indexes) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 21:50:48 +01:00
										 |  |  | // ErrOldIndexFormat is returned when an index with the old format is | 
					
						
							|  |  |  | // found. | 
					
						
							|  |  |  | type ErrOldIndexFormat struct { | 
					
						
							| 
									
										
										
										
											2016-08-31 22:39:36 +02:00
										 |  |  | 	restic.ID | 
					
						
							| 
									
										
										
										
											2015-11-08 21:50:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (err ErrOldIndexFormat) Error() string { | 
					
						
							|  |  |  | 	return fmt.Sprintf("index %v has old format", err.ID.Str()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 22:33:44 +01:00
										 |  |  | func (c *Checker) LoadSnapshots(ctx context.Context) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	c.snapshots, err = backend.MemorizeList(ctx, c.repo.Backend(), restic.SnapshotFile) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | // LoadIndex loads all index files. | 
					
						
							| 
									
										
										
										
											2017-06-04 11:16:55 +02:00
										 |  |  | func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) { | 
					
						
							| 
									
										
										
										
											2016-09-27 22:35:08 +02:00
										 |  |  | 	debug.Log("Start") | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 	packToIndex := make(map[restic.ID]restic.IDSet) | 
					
						
							|  |  |  | 	err := repository.ForAllIndexes(ctx, c.repo, func(id restic.ID, index *repository.Index, oldFormat bool, err error) error { | 
					
						
							|  |  |  | 		debug.Log("process index %v, err %v", id, err) | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 		if oldFormat { | 
					
						
							|  |  |  | 			debug.Log("index %v has old format", id.Str()) | 
					
						
							|  |  |  | 			hints = append(hints, ErrOldIndexFormat{id}) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 		err = errors.Wrapf(err, "error loading index %v", id.Str()) | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			errs = append(errs, err) | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 		c.masterIndex.Insert(index) | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 		debug.Log("process blobs") | 
					
						
							|  |  |  | 		cnt := 0 | 
					
						
							|  |  |  | 		for blob := range index.Each(ctx) { | 
					
						
							|  |  |  | 			cnt++ | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 			if _, ok := packToIndex[blob.PackID]; !ok { | 
					
						
							|  |  |  | 				packToIndex[blob.PackID] = restic.NewIDSet() | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 			packToIndex[blob.PackID].Insert(id) | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		debug.Log("%d blobs processed", cnt) | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errs = append(errs, err) | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	// Merge index before computing pack sizes, as this needs removed duplicates | 
					
						
							| 
									
										
										
										
											2021-01-30 16:35:05 +01:00
										 |  |  | 	err = c.masterIndex.MergeFinalIndexes() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// abort if an error occurs merging the indexes | 
					
						
							|  |  |  | 		return hints, append(errs, err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// compute pack size using index entries | 
					
						
							| 
									
										
										
										
											2022-02-13 14:25:38 +01:00
										 |  |  | 	c.packs = pack.Size(ctx, c.masterIndex, false) | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 22:35:08 +02:00
										 |  |  | 	debug.Log("checking for duplicate packs") | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | 	for packID := range c.packs { | 
					
						
							| 
									
										
										
										
											2018-01-25 20:49:41 +01:00
										 |  |  | 		debug.Log("  check pack %v: contained in %d indexes", packID, len(packToIndex[packID])) | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | 		if len(packToIndex[packID]) > 1 { | 
					
						
							| 
									
										
										
										
											2015-10-25 16:26:50 +01:00
										 |  |  | 			hints = append(hints, ErrDuplicatePacks{ | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | 				PackID:  packID, | 
					
						
							|  |  |  | 				Indexes: packToIndex[packID], | 
					
						
							| 
									
										
										
										
											2015-10-25 16:26:50 +01:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2015-10-25 16:00:06 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 21:27:28 +01:00
										 |  |  | 	err = c.repo.SetIndex(c.masterIndex) | 
					
						
							| 
									
										
										
										
											2018-03-31 10:02:09 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		debug.Log("SetIndex returned error: %v", err) | 
					
						
							|  |  |  | 		errs = append(errs, err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-25 16:26:50 +01:00
										 |  |  | 	return hints, errs | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | // PackError describes an error with a specific pack. | 
					
						
							|  |  |  | type PackError struct { | 
					
						
							| 
									
										
										
										
											2016-08-31 22:39:36 +02:00
										 |  |  | 	ID       restic.ID | 
					
						
							| 
									
										
										
										
											2015-07-12 17:09:48 +02:00
										 |  |  | 	Orphaned bool | 
					
						
							|  |  |  | 	Err      error | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e PackError) Error() string { | 
					
						
							| 
									
										
										
										
											2018-04-07 10:07:54 +02: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 16:00:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-12 00:25:42 +02:00
										 |  |  | // Packs checks that all packs referenced in the index are still available and | 
					
						
							| 
									
										
										
										
											2015-07-12 01:44:19 +02:00
										 |  |  | // there are no packs that aren't in an index. errChan is closed after all | 
					
						
							|  |  |  | // packs have been checked. | 
					
						
							| 
									
										
										
										
											2017-06-04 11:16:55 +02:00
										 |  |  | func (c *Checker) Packs(ctx context.Context, errChan chan<- error) { | 
					
						
							| 
									
										
										
										
											2015-07-12 01:44:19 +02:00
										 |  |  | 	defer close(errChan) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 22:35:08 +02:00
										 |  |  | 	debug.Log("checking for %d packs", len(c.packs)) | 
					
						
							| 
									
										
										
										
											2015-07-12 00:25:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:00:48 -05:00
										 |  |  | 	debug.Log("listing repository packs") | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	repoPacks := make(map[restic.ID]int64) | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 11:16:38 +02:00
										 |  |  | 	err := c.repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error { | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 		repoPacks[id] = size | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errChan <- err | 
					
						
							| 
									
										
										
										
											2015-07-12 00:25:42 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	for id, size := range c.packs { | 
					
						
							|  |  |  | 		reposize, ok := repoPacks[id] | 
					
						
							|  |  |  | 		// remove from repoPacks so we can find orphaned packs | 
					
						
							|  |  |  | 		delete(repoPacks, id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// missing: present in c.packs but not in the repo | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case <-ctx.Done(): | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			case errChan <- PackError{ID: id, Err: errors.New("does not exist")}: | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// size not matching: present in c.packs and in the repo, but sizes do not match | 
					
						
							|  |  |  | 		if size != reposize { | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case <-ctx.Done(): | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			case errChan <- PackError{ID: id, Err: errors.Errorf("unexpected file size: got %d, expected %d", reposize, size)}: | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-01-11 21:00:48 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-12 00:25:42 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	// orphaned: present in the repo but not in c.packs | 
					
						
							|  |  |  | 	for orphanID := range repoPacks { | 
					
						
							| 
									
										
										
										
											2018-01-11 21:00:48 -05:00
										 |  |  | 		select { | 
					
						
							|  |  |  | 		case <-ctx.Done(): | 
					
						
							|  |  |  | 			return | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 		case errChan <- PackError{ID: orphanID, Orphaned: true, Err: errors.New("not referenced in any index")}: | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Error is an error that occurred while checking a repository. | 
					
						
							|  |  |  | type Error struct { | 
					
						
							| 
									
										
										
										
											2016-08-31 22:39:36 +02:00
										 |  |  | 	TreeID restic.ID | 
					
						
							|  |  |  | 	BlobID restic.ID | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 	Err    error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e Error) Error() string { | 
					
						
							| 
									
										
										
										
											2016-02-26 23:06:25 +01:00
										 |  |  | 	if !e.BlobID.IsNull() && !e.TreeID.IsNull() { | 
					
						
							| 
									
										
										
										
											2015-10-11 19:25:02 +02:00
										 |  |  | 		msg := "tree " + e.TreeID.Str() | 
					
						
							|  |  |  | 		msg += ", blob " + e.BlobID.Str() | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 		msg += ": " + e.Err.Error() | 
					
						
							|  |  |  | 		return msg | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 23:06:25 +01:00
										 |  |  | 	if !e.TreeID.IsNull() { | 
					
						
							| 
									
										
										
										
											2015-10-11 19:25:02 +02:00
										 |  |  | 		return "tree " + e.TreeID.Str() + ": " + e.Err.Error() | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return e.Err.Error() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 19:13:35 +02:00
										 |  |  | // TreeError collects several errors that occurred while processing a tree. | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | type TreeError struct { | 
					
						
							| 
									
										
										
										
											2016-08-31 22:39:36 +02:00
										 |  |  | 	ID     restic.ID | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	Errors []error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e TreeError) Error() string { | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 	return fmt.Sprintf("tree %v: %v", e.ID.Str(), e.Errors) | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // checkTreeWorker checks the trees received and sends out errors to errChan. | 
					
						
							| 
									
										
										
										
											2020-11-07 00:39:40 +01:00
										 |  |  | func (c *Checker) checkTreeWorker(ctx context.Context, trees <-chan restic.TreeItem, out chan<- error) { | 
					
						
							|  |  |  | 	for job := range trees { | 
					
						
							|  |  |  | 		debug.Log("check tree %v (tree %v, err %v)", job.ID, job.Tree, job.Error) | 
					
						
							| 
									
										
										
										
											2020-11-07 00:23:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var errs []error | 
					
						
							| 
									
										
										
										
											2020-11-07 00:39:40 +01:00
										 |  |  | 		if job.Error != nil { | 
					
						
							|  |  |  | 			errs = append(errs, job.Error) | 
					
						
							| 
									
										
										
										
											2020-11-07 00:23:45 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			errs = c.checkTree(job.ID, job.Tree) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 00:23:45 +01:00
										 |  |  | 		if len(errs) == 0 { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		treeError := TreeError{ID: job.ID, Errors: errs} | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2017-06-04 11:16:55 +02:00
										 |  |  | 		case <-ctx.Done(): | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2020-11-07 00:23:45 +01:00
										 |  |  | 		case out <- treeError: | 
					
						
							| 
									
										
										
										
											2016-09-27 22:35:08 +02:00
										 |  |  | 			debug.Log("tree %v: sent %d errors", treeError.ID, len(treeError.Errors)) | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 22:33:44 +01:00
										 |  |  | func loadSnapshotTreeIDs(ctx context.Context, lister restic.Lister, repo restic.Repository) (ids restic.IDs, errs []error) { | 
					
						
							|  |  |  | 	err := restic.ForAllSnapshots(ctx, lister, repo, nil, func(id restic.ID, sn *restic.Snapshot, err error) error { | 
					
						
							| 
									
										
										
										
											2020-11-28 09:32:06 +01:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			errs = append(errs, err) | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		treeID := *sn.Tree | 
					
						
							|  |  |  | 		debug.Log("snapshot %v has tree %v", id, treeID) | 
					
						
							|  |  |  | 		ids = append(ids, treeID) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errs = append(errs, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ids, errs | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02: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. | 
					
						
							| 
									
										
										
										
											2020-12-06 00:07:45 +01:00
										 |  |  | func (c *Checker) Structure(ctx context.Context, p *progress.Counter, errChan chan<- error) { | 
					
						
							| 
									
										
										
										
											2021-11-07 22:33:44 +01:00
										 |  |  | 	trees, errs := loadSnapshotTreeIDs(ctx, c.snapshots, c.repo) | 
					
						
							| 
									
										
										
										
											2020-12-06 00:07:45 +01:00
										 |  |  | 	p.SetMax(uint64(len(trees))) | 
					
						
							| 
									
										
										
										
											2016-09-27 22:35:08 +02:00
										 |  |  | 	debug.Log("need to check %d trees from snapshots, %d errs returned", len(trees), len(errs)) | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	for _, err := range errs { | 
					
						
							|  |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2017-06-04 11:16:55 +02:00
										 |  |  | 		case <-ctx.Done(): | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		case errChan <- err: | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 00:23:45 +01:00
										 |  |  | 	wg, ctx := errgroup.WithContext(ctx) | 
					
						
							| 
									
										
										
										
											2020-11-07 00:39:40 +01:00
										 |  |  | 	treeStream := restic.StreamTrees(ctx, wg, c.repo, trees, func(treeID restic.ID) bool { | 
					
						
							|  |  |  | 		// blobRefs may be accessed in parallel by checkTree | 
					
						
							|  |  |  | 		c.blobRefs.Lock() | 
					
						
							|  |  |  | 		h := restic.BlobHandle{ID: treeID, Type: restic.TreeBlob} | 
					
						
							|  |  |  | 		blobReferenced := c.blobRefs.M.Has(h) | 
					
						
							|  |  |  | 		// noop if already referenced | 
					
						
							|  |  |  | 		c.blobRefs.M.Insert(h) | 
					
						
							|  |  |  | 		c.blobRefs.Unlock() | 
					
						
							|  |  |  | 		return blobReferenced | 
					
						
							| 
									
										
										
										
											2020-12-06 00:07:45 +01:00
										 |  |  | 	}, p) | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 00:23:45 +01:00
										 |  |  | 	defer close(errChan) | 
					
						
							|  |  |  | 	for i := 0; i < defaultParallelism; i++ { | 
					
						
							|  |  |  | 		wg.Go(func() error { | 
					
						
							|  |  |  | 			c.checkTreeWorker(ctx, treeStream, errChan) | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-30 16:46:34 +01:00
										 |  |  | 	// the wait group should not return an error because no worker returns an | 
					
						
							|  |  |  | 	// error, so panic if that has changed somehow. | 
					
						
							|  |  |  | 	err := wg.Wait() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 22:39:36 +02:00
										 |  |  | func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) { | 
					
						
							| 
									
										
										
										
											2018-01-25 20:49:41 +01:00
										 |  |  | 	debug.Log("checking tree %v", id) | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 20:55:28 +02:00
										 |  |  | 	for _, node := range tree.Nodes { | 
					
						
							| 
									
										
										
										
											2016-09-01 21:20:03 +02:00
										 |  |  | 		switch node.Type { | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 		case "file": | 
					
						
							| 
									
										
										
										
											2016-04-10 16:51:16 +02:00
										 |  |  | 			if node.Content == nil { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:48:36 +02:00
										 |  |  | 				errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q has nil blob list", node.Name)}) | 
					
						
							| 
									
										
										
										
											2016-04-10 16:51:16 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-11 20:55:28 +02:00
										 |  |  | 			for b, blobID := range node.Content { | 
					
						
							|  |  |  | 				if blobID.IsNull() { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:48:36 +02:00
										 |  |  | 					errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %d has null ID", node.Name, b)}) | 
					
						
							| 
									
										
										
										
											2015-10-11 20:55:28 +02:00
										 |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-12-20 08:48:31 +01:00
										 |  |  | 				// Note that we do not use the blob size. The "obvious" check | 
					
						
							|  |  |  | 				// whether the sum of the blob sizes matches the file size | 
					
						
							|  |  |  | 				// unfortunately fails in some cases that are not resolveable | 
					
						
							|  |  |  | 				// by users, so we omit this check, see #1887 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				_, found := c.repo.LookupBlobSize(blobID, restic.DataBlob) | 
					
						
							| 
									
										
										
										
											2018-03-31 13:22:25 +02:00
										 |  |  | 				if !found { | 
					
						
							| 
									
										
										
										
											2020-11-06 23:32:31 +01:00
										 |  |  | 					debug.Log("tree %v references blob %v which isn't contained in index", id, blobID) | 
					
						
							|  |  |  | 					errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %v not found in index", node.Name, blobID)}) | 
					
						
							| 
									
										
										
										
											2018-03-31 13:22:25 +02:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-11-07 00:07:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if c.trackUnused { | 
					
						
							|  |  |  | 				// loop a second time to keep the locked section as short as possible | 
					
						
							|  |  |  | 				c.blobRefs.Lock() | 
					
						
							|  |  |  | 				for _, blobID := range node.Content { | 
					
						
							|  |  |  | 					if blobID.IsNull() { | 
					
						
							|  |  |  | 						continue | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					h := restic.BlobHandle{ID: blobID, Type: restic.DataBlob} | 
					
						
							|  |  |  | 					c.blobRefs.M.Insert(h) | 
					
						
							|  |  |  | 					debug.Log("blob %v is referenced", blobID) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				c.blobRefs.Unlock() | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 		case "dir": | 
					
						
							|  |  |  | 			if node.Subtree == nil { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:48:36 +02:00
										 |  |  | 				errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q has no subtree", node.Name)}) | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-11 18:46:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if node.Subtree.IsNull() { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:48:36 +02:00
										 |  |  | 				errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q subtree id is null", node.Name)}) | 
					
						
							| 
									
										
										
										
											2015-10-11 18:46:26 +02:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-04-10 16:51:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 20:46:57 +02:00
										 |  |  | 		case "symlink", "socket", "chardev", "dev", "fifo": | 
					
						
							| 
									
										
										
										
											2016-05-08 23:16:01 +02:00
										 |  |  | 			// nothing to check | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-10 16:51:16 +02:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2016-09-01 21:20:03 +02:00
										 |  |  | 			errs = append(errs, Error{TreeID: id, Err: errors.Errorf("node %q with invalid type %q", node.Name, node.Type)}) | 
					
						
							| 
									
										
										
										
											2016-04-10 16:51:16 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if node.Name == "" { | 
					
						
							|  |  |  | 			errs = append(errs, Error{TreeID: id, Err: errors.New("node with empty name")}) | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return errs | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // UnusedBlobs returns all blobs that have never been referenced. | 
					
						
							| 
									
										
										
										
											2020-11-06 23:41:04 +01:00
										 |  |  | func (c *Checker) UnusedBlobs(ctx context.Context) (blobs restic.BlobHandles) { | 
					
						
							| 
									
										
										
										
											2020-11-07 00:07:32 +01:00
										 |  |  | 	if !c.trackUnused { | 
					
						
							|  |  |  | 		panic("only works when tracking blob references") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-12 16:42:22 +02:00
										 |  |  | 	c.blobRefs.Lock() | 
					
						
							|  |  |  | 	defer c.blobRefs.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 18:34:55 +02:00
										 |  |  | 	debug.Log("checking %d blobs", len(c.blobRefs.M)) | 
					
						
							| 
									
										
										
										
											2020-11-06 23:41:04 +01:00
										 |  |  | 	ctx, cancel := context.WithCancel(ctx) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for blob := range c.repo.Index().Each(ctx) { | 
					
						
							|  |  |  | 		h := restic.BlobHandle{ID: blob.ID, Type: blob.Type} | 
					
						
							|  |  |  | 		if !c.blobRefs.M.Has(h) { | 
					
						
							|  |  |  | 			debug.Log("blob %v not referenced", h) | 
					
						
							|  |  |  | 			blobs = append(blobs, h) | 
					
						
							| 
									
										
										
										
											2015-07-11 16:00:49 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return blobs | 
					
						
							| 
									
										
										
										
											2015-06-29 00:22:25 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-12 17:09:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 17:29:31 +01:00
										 |  |  | // CountPacks returns the number of packs in the repository. | 
					
						
							|  |  |  | func (c *Checker) CountPacks() uint64 { | 
					
						
							|  |  |  | 	return uint64(len(c.packs)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-02 00:38:14 -05:00
										 |  |  | // GetPacks returns IDSet of packs in the repository | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | func (c *Checker) GetPacks() map[restic.ID]int64 { | 
					
						
							| 
									
										
										
										
											2018-01-02 00:38:14 -05:00
										 |  |  | 	return c.packs | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | // checkPack reads a pack and checks the integrity of all blobs. | 
					
						
							| 
									
										
										
										
											2021-08-22 14:38:27 +02:00
										 |  |  | func checkPack(ctx context.Context, r restic.Repository, id restic.ID, blobs []restic.Blob, size int64, bufRd *bufio.Reader) error { | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	debug.Log("checking pack %v", id.String()) | 
					
						
							| 
									
										
										
										
											2017-02-06 21:17:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	if len(blobs) == 0 { | 
					
						
							|  |  |  | 		return errors.Errorf("pack %v is empty or not indexed", id) | 
					
						
							| 
									
										
										
										
											2017-02-06 21:17:52 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	// sanity check blobs in index | 
					
						
							|  |  |  | 	sort.Slice(blobs, func(i, j int) bool { | 
					
						
							|  |  |  | 		return blobs[i].Offset < blobs[j].Offset | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2022-02-13 16:51:47 +01:00
										 |  |  | 	idxHdrSize := pack.CalculateHeaderSize(blobs) | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	lastBlobEnd := 0 | 
					
						
							|  |  |  | 	nonContinuousPack := false | 
					
						
							|  |  |  | 	for _, blob := range blobs { | 
					
						
							|  |  |  | 		if lastBlobEnd != int(blob.Offset) { | 
					
						
							|  |  |  | 			nonContinuousPack = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		lastBlobEnd = int(blob.Offset + blob.Length) | 
					
						
							| 
									
										
										
										
											2016-02-04 22:54:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	// size was calculated by masterindex.PackSize, thus there's no need to recalculate it here | 
					
						
							| 
									
										
										
										
											2016-02-04 22:54:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	var errs []error | 
					
						
							|  |  |  | 	if nonContinuousPack { | 
					
						
							|  |  |  | 		debug.Log("Index for pack contains gaps / overlaps, blobs: %v", blobs) | 
					
						
							|  |  |  | 		errs = append(errs, errors.New("Index for pack contains gaps / overlapping blobs")) | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	// calculate hash on-the-fly while reading the pack and capture pack header | 
					
						
							|  |  |  | 	var hash restic.ID | 
					
						
							| 
									
										
										
										
											2021-09-04 14:08:53 +02:00
										 |  |  | 	var hdrBuf []byte | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	hashingLoader := func(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error { | 
					
						
							|  |  |  | 		return r.Backend().Load(ctx, h, int(size), 0, func(rd io.Reader) error { | 
					
						
							|  |  |  | 			hrd := hashing.NewReader(rd, sha256.New()) | 
					
						
							| 
									
										
										
										
											2021-08-22 14:38:27 +02:00
										 |  |  | 			bufRd.Reset(hrd) | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			// skip to start of first blob, offset == 0 for correct pack files | 
					
						
							|  |  |  | 			_, err := bufRd.Discard(int(offset)) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-06 21:17:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			err = fn(bufRd) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-06 21:17:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			// skip enough bytes until we reach the possible header start | 
					
						
							|  |  |  | 			curPos := length + int(offset) | 
					
						
							|  |  |  | 			minHdrStart := int(size) - pack.MaxHeaderSize | 
					
						
							|  |  |  | 			if minHdrStart > curPos { | 
					
						
							|  |  |  | 				_, err := bufRd.Discard(minHdrStart - curPos) | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					return err | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-06 21:17:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			// read remainder, which should be the pack header | 
					
						
							| 
									
										
										
										
											2021-09-04 14:08:53 +02:00
										 |  |  | 			hdrBuf, err = ioutil.ReadAll(bufRd) | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			hash = restic.IDFromHash(hrd.Sum(nil)) | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := repository.StreamPack(ctx, hashingLoader, r.Key(), id, blobs, func(blob restic.BlobHandle, buf []byte, err error) error { | 
					
						
							|  |  |  | 		debug.Log("  check blob %v: %v", blob.ID, blob) | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 			debug.Log("  error verifying blob %v: %v", blob.ID, err) | 
					
						
							|  |  |  | 			errs = append(errs, errors.Errorf("blob %v: %v", blob.ID, err)) | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to load the pack file, return as further checks cannot succeed anyways | 
					
						
							|  |  |  | 		debug.Log("  error streaming pack: %v", err) | 
					
						
							|  |  |  | 		return errors.Errorf("pack %v failed to download: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !hash.Equal(id) { | 
					
						
							|  |  |  | 		debug.Log("Pack ID does not match, want %v, got %v", id, hash) | 
					
						
							|  |  |  | 		return errors.Errorf("Pack ID does not match, want %v, got %v", id.Str(), hash.Str()) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 14:08:53 +02:00
										 |  |  | 	blobs, hdrSize, err := pack.List(r.Key(), bytes.NewReader(hdrBuf), int64(len(hdrBuf))) | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if uint32(idxHdrSize) != hdrSize { | 
					
						
							|  |  |  | 		debug.Log("Pack header size does not match, want %v, got %v", idxHdrSize, hdrSize) | 
					
						
							|  |  |  | 		errs = append(errs, errors.Errorf("Pack header size does not match, want %v, got %v", idxHdrSize, hdrSize)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-01 16:30:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	idx := r.Index() | 
					
						
							|  |  |  | 	for _, blob := range blobs { | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 		// Check if blob is contained in index and position is correct | 
					
						
							| 
									
										
										
										
											2020-11-01 16:30:20 +01:00
										 |  |  | 		idxHas := false | 
					
						
							| 
									
										
										
										
											2020-11-05 22:18:00 +01:00
										 |  |  | 		for _, pb := range idx.Lookup(blob.BlobHandle) { | 
					
						
							| 
									
										
										
										
											2022-02-13 16:50:50 +01:00
										 |  |  | 			if pb.PackID == id && pb.Blob == blob { | 
					
						
							| 
									
										
										
										
											2020-11-01 16:30:20 +01:00
										 |  |  | 				idxHas = true | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !idxHas { | 
					
						
							| 
									
										
										
										
											2020-11-10 14:35:59 +01:00
										 |  |  | 			errs = append(errs, errors.Errorf("Blob %v is not contained in index or position is incorrect", blob.ID.Str())) | 
					
						
							| 
									
										
										
										
											2020-11-01 16:30:20 +01:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(errs) > 0 { | 
					
						
							| 
									
										
										
										
											2016-08-21 17:48:36 +02:00
										 |  |  | 		return errors.Errorf("pack %v contains %v errors: %v", id.Str(), len(errs), errs) | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadData loads all data from the repository and checks the integrity. | 
					
						
							| 
									
										
										
										
											2020-11-08 21:03:59 +01:00
										 |  |  | func (c *Checker) ReadData(ctx context.Context, errChan chan<- error) { | 
					
						
							|  |  |  | 	c.ReadPacks(ctx, c.packs, nil, errChan) | 
					
						
							| 
									
										
										
										
											2018-01-02 00:38:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadPacks loads data from specified packs and checks the integrity. | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | func (c *Checker) ReadPacks(ctx context.Context, packs map[restic.ID]int64, p *progress.Counter, errChan chan<- error) { | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 	defer close(errChan) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 	g, ctx := errgroup.WithContext(ctx) | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	type checkTask struct { | 
					
						
							|  |  |  | 		id    restic.ID | 
					
						
							|  |  |  | 		size  int64 | 
					
						
							|  |  |  | 		blobs []restic.Blob | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	ch := make(chan checkTask) | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 	// run workers | 
					
						
							|  |  |  | 	for i := 0; i < defaultParallelism; i++ { | 
					
						
							|  |  |  | 		g.Go(func() error { | 
					
						
							| 
									
										
										
										
											2021-08-22 14:38:27 +02:00
										 |  |  | 			// create a buffer that is large enough to be reused by repository.StreamPack | 
					
						
							|  |  |  | 			// this ensures that we can read the pack header later on | 
					
						
							|  |  |  | 			bufRd := bufio.NewReaderSize(nil, repository.MaxStreamBufferSize) | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 			for { | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 				var ps checkTask | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 				var ok bool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				select { | 
					
						
							|  |  |  | 				case <-ctx.Done(): | 
					
						
							|  |  |  | 					return nil | 
					
						
							| 
									
										
										
										
											2020-11-10 08:16:47 +01:00
										 |  |  | 				case ps, ok = <-ch: | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 					if !ok { | 
					
						
							|  |  |  | 						return nil | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 14:38:27 +02:00
										 |  |  | 				err := checkPack(ctx, c.repo, ps.id, ps.blobs, ps.size, bufRd) | 
					
						
							| 
									
										
										
										
											2020-11-04 14:11:29 +01:00
										 |  |  | 				p.Add(1) | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 				if err == nil { | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				select { | 
					
						
							|  |  |  | 				case <-ctx.Done(): | 
					
						
							|  |  |  | 					return nil | 
					
						
							|  |  |  | 				case errChan <- err: | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-06 17:09:06 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-06 17:09:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	packSet := restic.NewIDSet() | 
					
						
							|  |  |  | 	for pack := range packs { | 
					
						
							|  |  |  | 		packSet.Insert(pack) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-02 00:38:14 -05:00
										 |  |  | 	// push packs to ch | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 	for pbs := range c.repo.Index().ListPacks(ctx, packSet) { | 
					
						
							|  |  |  | 		size := packs[pbs.PackID] | 
					
						
							|  |  |  | 		debug.Log("listed %v", pbs.PackID) | 
					
						
							| 
									
										
										
										
											2018-01-02 00:38:14 -05:00
										 |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2021-08-20 16:15:40 +02:00
										 |  |  | 		case ch <- checkTask{id: pbs.PackID, size: size, blobs: pbs.Blobs}: | 
					
						
							| 
									
										
										
										
											2018-01-02 00:38:14 -05:00
										 |  |  | 		case <-ctx.Done(): | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	close(ch) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-21 17:25:36 +01:00
										 |  |  | 	err := g.Wait() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		select { | 
					
						
							|  |  |  | 		case <-ctx.Done(): | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		case errChan <- err: | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-12-06 17:09:06 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-02 22:40:36 +01:00
										 |  |  | } |