1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-03-09 00:00:02 +01:00

check with snapshot filter: check early for filter errors - rebase part 4

Hand down filtered tree IDs to CheckWithSnapshots which builds 'usedBlobs'.
repo.LookupBlob is used to derive packfiles from 'usedBlobs' and constructs
'snapPacks' and overwrites c.packs.
This commit is contained in:
Winfried Plappert 2025-02-15 07:20:32 +00:00
parent 3b05529334
commit c0e5a74774
2 changed files with 34 additions and 42 deletions

View file

@ -244,6 +244,28 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
}
defer unlock()
// check snapshot filter
selectedTrees := []restic.ID{}
if len(args) > 0 || !opts.SnapshotFilter.Empty() {
snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
if err != nil {
return err
}
err = (&opts.SnapshotFilter).FindAll(ctx, snapshotLister, repo, args, func(_ string, sn *restic.Snapshot, err error) error {
if err != nil {
return err
}
selectedTrees = append(selectedTrees, *sn.Tree)
return nil
})
if err != nil {
return err
}
}
chkr := checker.New(repo, opts.CheckUnused)
err = chkr.LoadSnapshots(ctx)
if err != nil {
@ -389,11 +411,12 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
}
filterBySnapshot := false
if len(args) > 0 || !opts.SnapshotFilter.Empty() {
filterBySnapshot, err = chkr.CheckWithSnapshots(ctx, repo, args, &opts.SnapshotFilter)
if len(selectedTrees) > 0 {
err = chkr.CheckWithSnapshots(ctx, selectedTrees)
if err != nil {
return err
}
filterBySnapshot = true
}
doReadData := func(packs map[restic.ID]int64) {

View file

@ -523,55 +523,24 @@ func (c *Checker) ReadPacks(ctx context.Context, packs map[restic.ID]int64, p *p
}
// CheckWithSnapshots will process snapshot IDs from command line and
// madify c.packs so it contains only the selected packfiles via snapshotFilter
func (c *Checker) CheckWithSnapshots(ctx context.Context, repo *repository.Repository, args []string, snapshotFilter *restic.SnapshotFilter) (bool, error) {
selectedTrees := []restic.ID{}
err := snapshotFilter.FindAll(ctx, c.snapshots, repo, args, func(_ string, sn *restic.Snapshot, err error) error {
if err != nil {
return err
} else if ctx.Err() != nil {
return ctx.Err()
}
selectedTrees = append(selectedTrees, *sn.Tree)
return nil
})
if err != nil {
return false, err
}
// add to snapPacks so it contains only the selected packfiles via snapshotFilter
func (c *Checker) CheckWithSnapshots(ctx context.Context, selectedTrees []restic.ID) error {
// gather used blobs from all trees
usedBlobs := restic.NewBlobSet()
err = restic.FindUsedBlobs(ctx, repo, selectedTrees, usedBlobs, nil)
err := restic.FindUsedBlobs(ctx, c.repo, selectedTrees, usedBlobs, nil)
if err != nil {
return false, err
}
if len(selectedTrees) == 0 {
return false, nil
return err
}
// convert blobs to packfile IDs
c.packs = map[restic.ID]int64{}
snapPacks := map[restic.ID]int64{}
for blob := range usedBlobs {
for _, res := range repo.LookupBlob(blob.Type, blob.ID) {
c.packs[res.PackID] = 0
for _, res := range c.repo.LookupBlob(blob.Type, blob.ID) {
snapPacks[res.PackID] = c.packs[res.PackID]
}
}
// gather size for selected packfiles
err = c.repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
if _, ok := c.packs[id]; ok {
c.packs[id] = size
}
return nil
})
if err != nil {
return false, err
}
return len(selectedTrees) > 0, nil
c.packs = snapPacks
return nil
}