From 239931578ca1a88d0da68aa139ea1dc5b477d800 Mon Sep 17 00:00:00 2001
From: Alexander Weiss <alex@weissfam.de>
Date: Sun, 1 Nov 2020 16:30:20 +0100
Subject: [PATCH] check: check index for packs that are read

---
 changelog/unreleased/pull-3048 |  9 +++++++++
 internal/checker/checker.go    | 14 ++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 changelog/unreleased/pull-3048

diff --git a/changelog/unreleased/pull-3048 b/changelog/unreleased/pull-3048
new file mode 100644
index 000000000..61781e100
--- /dev/null
+++ b/changelog/unreleased/pull-3048
@@ -0,0 +1,9 @@
+Enhancement: Check now checks index when reading packs
+
+Restic used to only verfiy the pack file content when calling `check --read-data`
+`check --read-data-subset` but did not check if the blobs within the pack are
+correctly contained in the index.
+This check is now added and may give an "Blob ID is not contained in index" error.
+If the index is not correct, it can be rebuilt by using the `rebuild-index` command.
+
+https://github.com/restic/restic/pull/3048
diff --git a/internal/checker/checker.go b/internal/checker/checker.go
index 96ce75f10..98a3c14b4 100644
--- a/internal/checker/checker.go
+++ b/internal/checker/checker.go
@@ -727,6 +727,7 @@ func checkPack(ctx context.Context, r restic.Repository, id restic.ID) error {
 
 	var errs []error
 	var buf []byte
+	idx := r.Index()
 	for i, blob := range blobs {
 		debug.Log("  check blob %d: %v", i, blob)
 
@@ -762,6 +763,19 @@ func checkPack(ctx context.Context, r restic.Repository, id restic.ID) error {
 			errs = append(errs, errors.Errorf("Blob ID does not match, want %v, got %v", blob.ID.Str(), hash.Str()))
 			continue
 		}
+
+		// Check if blob is contained in index
+		idxHas := false
+		for _, pb := range idx.Lookup(blob.ID, blob.Type) {
+			if pb.PackID == id {
+				idxHas = true
+				break
+			}
+		}
+		if !idxHas {
+			errs = append(errs, errors.Errorf("Blob ID %v is not contained in index", blob.ID.Str()))
+			continue
+		}
 	}
 
 	if len(errs) > 0 {