repository: add Checker() method to repository to replace unchecked cast

This commit is contained in:
Michael Eischer
2025-09-28 14:52:41 +02:00
parent 189b295c30
commit f0955fa931
8 changed files with 20 additions and 13 deletions

View File

@@ -29,7 +29,7 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func prepareTempdirRepoSrc(t testing.TB, src TestDir) (string, restic.Repository) { func prepareTempdirRepoSrc(t testing.TB, src TestDir) (string, *repository.Repository) {
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
repo := repository.TestRepository(t) repo := repository.TestRepository(t)

View File

@@ -33,10 +33,15 @@ type Checker struct {
repo restic.Repository repo restic.Repository
} }
type checkerRepository interface {
restic.Repository
Checker() *repository.Checker
}
// New returns a new checker which runs on repo. // New returns a new checker which runs on repo.
func New(repo restic.Repository, trackUnused bool) *Checker { func New(repo checkerRepository, trackUnused bool) *Checker {
c := &Checker{ c := &Checker{
Checker: repository.NewChecker(repo), Checker: repo.Checker(),
repo: repo, repo: repo,
trackUnused: trackUnused, trackUnused: trackUnused,
} }

View File

@@ -435,7 +435,7 @@ func TestCheckerModifiedData(t *testing.T) {
// loadTreesOnceRepository allows each tree to be loaded only once // loadTreesOnceRepository allows each tree to be loaded only once
type loadTreesOnceRepository struct { type loadTreesOnceRepository struct {
restic.Repository *repository.Repository
loadedTrees restic.IDSet loadedTrees restic.IDSet
mutex sync.Mutex mutex sync.Mutex
DuplicateTree bool DuplicateTree bool
@@ -476,7 +476,7 @@ func TestCheckerNoDuplicateTreeDecodes(t *testing.T) {
// delayRepository delays read of a specific handle. // delayRepository delays read of a specific handle.
type delayRepository struct { type delayRepository struct {
restic.Repository *repository.Repository
DelayTree restic.ID DelayTree restic.ID
UnblockChannel chan struct{} UnblockChannel chan struct{}
Unblocker sync.Once Unblocker sync.Once

View File

@@ -3,12 +3,10 @@ package checker
import ( import (
"context" "context"
"testing" "testing"
"github.com/restic/restic/internal/restic"
) )
// TestCheckRepo runs the checker on repo. // TestCheckRepo runs the checker on repo.
func TestCheckRepo(t testing.TB, repo restic.Repository) { func TestCheckRepo(t testing.TB, repo checkerRepository) {
chkr := New(repo, true) chkr := New(repo, true)
hints, errs := chkr.LoadIndex(context.TODO(), nil) hints, errs := chkr.LoadIndex(context.TODO(), nil)

View File

@@ -51,11 +51,11 @@ func (e *PackError) Error() string {
// Checker handles index-related operations for repository checking. // Checker handles index-related operations for repository checking.
type Checker struct { type Checker struct {
packs map[restic.ID]int64 packs map[restic.ID]int64
repo restic.Repository repo *Repository
} }
// NewChecker creates a new Checker. // NewChecker creates a new Checker.
func NewChecker(repo restic.Repository) *Checker { func NewChecker(repo *Repository) *Checker {
return &Checker{ return &Checker{
packs: make(map[restic.ID]int64), packs: make(map[restic.ID]int64),
repo: repo, repo: repo,
@@ -256,7 +256,7 @@ func (c *Checker) ReadPacks(ctx context.Context, packs map[restic.ID]int64, p *p
} }
} }
err := CheckPack(ctx, c.repo.(*Repository), ps.id, ps.blobs, ps.size, bufRd, dec) err := CheckPack(ctx, c.repo, ps.id, ps.blobs, ps.size, bufRd, dec)
p.Add(1) p.Add(1)
if err == nil { if err == nil {
continue continue

View File

@@ -347,7 +347,7 @@ var (
depth = 3 depth = 3
) )
func createFilledRepo(t testing.TB, snapshots int, version uint) (restic.Repository, restic.Unpacked[restic.FileType]) { func createFilledRepo(t testing.TB, snapshots int, version uint) (*repository.Repository, restic.Unpacked[restic.FileType]) {
repo, unpacked, _ := repository.TestRepositoryWithVersion(t, version) repo, unpacked, _ := repository.TestRepositoryWithVersion(t, version)
for i := 0; i < snapshots; i++ { for i := 0; i < snapshots; i++ {

View File

@@ -178,6 +178,10 @@ func (r *Repository) SetDryRun() {
r.be = dryrun.New(r.be) r.be = dryrun.New(r.be)
} }
func (r *Repository) Checker() *Checker {
return NewChecker(r)
}
// LoadUnpacked loads and decrypts the file with the given type and ID. // LoadUnpacked loads and decrypts the file with the given type and ID.
func (r *Repository) LoadUnpacked(ctx context.Context, t restic.FileType, id restic.ID) ([]byte, error) { func (r *Repository) LoadUnpacked(ctx context.Context, t restic.FileType, id restic.ID) ([]byte, error) {
debug.Log("load %v with id %v", t, id) debug.Log("load %v with id %v", t, id)

View File

@@ -164,7 +164,7 @@ func TestNewLock(_ *testing.T, repo *Repository, exclusive bool) (*restic.Lock,
} }
// TestCheckRepo runs the checker on repo. // TestCheckRepo runs the checker on repo.
func TestCheckRepo(t testing.TB, repo restic.Repository) { func TestCheckRepo(t testing.TB, repo *Repository) {
chkr := NewChecker(repo) chkr := NewChecker(repo)
hints, errs := chkr.LoadIndex(context.TODO(), nil) hints, errs := chkr.LoadIndex(context.TODO(), nil)