diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index 8df46cfbe..c43342180 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -506,7 +506,10 @@ func (f *Finder) indexPacksToBlobs(ctx context.Context, packIDs map[string]struc // remember which packs were found in the index indexPackIDs := make(map[string]struct{}) - err := f.repo.ListBlobs(wctx, func(pb restic.PackBlob) { + for pb := range f.repo.ListBlobs() { + if err := wctx.Err(); err != nil { + return nil, err + } packID := pb.PackID() idStr := packID.String() // keep entry in packIDs as Each() returns individual index entries @@ -525,9 +528,6 @@ func (f *Finder) indexPacksToBlobs(ctx context.Context, packIDs map[string]struc f.addBlobHandle(pb.Handle()) indexPackIDs[idStr] = struct{}{} } - }) - if err != nil { - return nil, err } for id := range indexPackIDs { diff --git a/cmd/restic/cmd_find_integration_test.go b/cmd/restic/cmd_find_integration_test.go index 1133de350..ce5ae0099 100644 --- a/cmd/restic/cmd_find_integration_test.go +++ b/cmd/restic/cmd_find_integration_test.go @@ -180,15 +180,14 @@ func TestFindPackfile(t *testing.T) { packID := restic.ID{} done := false - err = repo.ListBlobs(ctx, func(pb restic.PackBlob) { + for pb := range repo.ListBlobs() { + rtest.OK(t, ctx.Err()) h := pb.Handle() if !done && h.Type == restic.TreeBlob { packID = pb.PackID() done = true } - }) - - rtest.OK(t, err) + } rtest.Assert(t, !packID.IsNull(), "expected a tree packfile ID") findOptions := FindOptions{PackID: true} results := testRunFind(t, true, findOptions, env.gopts, packID.String()) @@ -238,14 +237,15 @@ func TestFindPackID(t *testing.T) { // load Index rtest.OK(t, repo.LoadIndex(ctx, restic.NoopTerminalCounterFactory)) // go through all index entries and collect data and tree packfile(s) - rtest.OK(t, repo.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range repo.ListBlobs() { + rtest.OK(t, ctx.Err()) switch blob.Handle().Type { case restic.DataBlob: dataPackID = blob.PackID() case restic.TreeBlob: treePackID = blob.PackID() } - })) + } return nil }) rtest.OK(t, err) diff --git a/cmd/restic/cmd_list_integration_test.go b/cmd/restic/cmd_list_integration_test.go index 718b0ef18..e9f8f72fc 100644 --- a/cmd/restic/cmd_list_integration_test.go +++ b/cmd/restic/cmd_list_integration_test.go @@ -75,9 +75,10 @@ func testListBlobs(t testing.TB, gopts global.Options) (blobSetFromIndex restic. // get blobs from index blobSetFromIndex = restic.NewIDSet() - rtest.OK(t, repo.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range repo.ListBlobs() { + rtest.OK(t, ctx.Err()) blobSetFromIndex.Insert(blob.Handle().ID) - })) + } return nil }) rtest.OK(t, err) diff --git a/cmd/restic/cmd_recover.go b/cmd/restic/cmd_recover.go index cc62fa319..d3ef1d579 100644 --- a/cmd/restic/cmd_recover.go +++ b/cmd/restic/cmd_recover.go @@ -75,14 +75,14 @@ func runRecover(ctx context.Context, gopts global.Options, term ui.Terminal) err // tree. If it is not referenced, we have a root tree. trees := make(map[restic.ID]bool) - err = repo.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range repo.ListBlobs() { + if err = ctx.Err(); err != nil { + return err + } h := blob.Handle() if h.Type == restic.TreeBlob { trees[h.ID] = false } - }) - if err != nil { - return err } printer.P("load %d trees\n", len(trees)) diff --git a/cmd/restic/cmd_repair_packs_integration_test.go b/cmd/restic/cmd_repair_packs_integration_test.go index 05732ae00..7fe9add67 100644 --- a/cmd/restic/cmd_repair_packs_integration_test.go +++ b/cmd/restic/cmd_repair_packs_integration_test.go @@ -40,13 +40,12 @@ func TestRunRepairPackfiles(t *testing.T) { rtest.OK(t, repo.LoadIndex(ctx, printer)) // load packfiles from master index - err = repo.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range repo.ListBlobs() { + rtest.OK(t, ctx.Err()) if blob.Handle().Type == restic.DataBlob { packfileID = blob.PackID() - return } - }) - rtest.OK(t, err) + } return nil }) diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index 06c5e3592..583f50b5b 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -419,11 +419,14 @@ func statsDebugBlobs(ctx context.Context, repo restic.Repository) ([restic.NumBl hist[i] = newSizeHistogram(2 * chunker.MaxSize) } - err := repo.ListBlobs(ctx, func(pb restic.PackBlob) { + for pb := range repo.ListBlobs() { + if err := ctx.Err(); err != nil { + return hist, err + } hist[pb.Handle().Type].Add(uint64(pb.CiphertextLength())) - }) + } - return hist, err + return hist, nil } type sizeClass struct { diff --git a/cmd/restic/integration_helpers_test.go b/cmd/restic/integration_helpers_test.go index e05c88976..8f69fb4b3 100644 --- a/cmd/restic/integration_helpers_test.go +++ b/cmd/restic/integration_helpers_test.go @@ -273,11 +273,15 @@ func listTreePacks(gopts global.Options, t *testing.T) restic.IDSet { rtest.OK(t, r.LoadIndex(ctx, restic.NoopTerminalCounterFactory)) treePacks = restic.NewIDSet() - return r.ListBlobs(ctx, func(pb restic.PackBlob) { + for pb := range r.ListBlobs() { + if err := ctx.Err(); err != nil { + return err + } if pb.Handle().Type == restic.TreeBlob { treePacks.Insert(pb.PackID()) } - }) + } + return nil }) rtest.OK(t, err) return treePacks @@ -322,11 +326,12 @@ func removePacksExcept(gopts global.Options, t testing.TB, keep restic.IDSet, re rtest.OK(t, r.LoadIndex(ctx, restic.NoopTerminalCounterFactory)) treePacks := restic.NewIDSet() - rtest.OK(t, r.ListBlobs(ctx, func(pb restic.PackBlob) { + for pb := range r.ListBlobs() { + rtest.OK(t, ctx.Err()) if pb.Handle().Type == restic.TreeBlob { treePacks.Insert(pb.PackID()) } - })) + } // remove all packs containing data blobs return r.List(ctx, restic.PackFile, func(id restic.ID, size int64) error { diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 47e51e31d..7b1b46ff6 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -285,15 +285,18 @@ func (c *Checker) UnusedBlobs(ctx context.Context) (blobs restic.BlobHandles, er ctx, cancel := context.WithCancel(ctx) defer cancel() - err = c.repo.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range c.repo.ListBlobs() { + if err := ctx.Err(); err != nil { + return blobs, err + } h := blob.Handle() if !c.blobRefs.M.Has(h) { debug.Log("blob %v not referenced", h) blobs = append(blobs, h) } - }) + } - return blobs, err + return blobs, nil } // ReadPacks wraps repository.ReadPacks: diff --git a/internal/repository/checker.go b/internal/repository/checker.go index 5855dbffa..bf2e720aa 100644 --- a/internal/repository/checker.go +++ b/internal/repository/checker.go @@ -7,6 +7,7 @@ import ( "crypto/sha256" "fmt" "io" + "iter" "github.com/klauspost/compress/zstd" "github.com/restic/restic/internal/backend" @@ -86,9 +87,13 @@ func newChecker(repo *Repository) *Checker { repo: repo, } } -func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.ID]restic.BlobType, error) { + +func computePackTypes(ctx context.Context, blobs iter.Seq[restic.PackBlob]) (map[restic.ID]restic.BlobType, error) { packs := make(map[restic.ID]restic.BlobType) - err := idx.ListBlobs(ctx, func(pb restic.PackBlob) { + for pb := range blobs { + if err := ctx.Err(); err != nil { + return packs, err + } packID := pb.PackID() h := pb.Handle() tpe, exists := packs[packID] @@ -100,8 +105,8 @@ func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.I tpe = h.Type } packs[packID] = tpe - }) - return packs, err + } + return packs, nil } // LoadIndex loads all index files. @@ -153,7 +158,7 @@ func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory return hints, append(errs, err) } - packTypes, err := computePackTypes(ctx, c.repo) + packTypes, err := computePackTypes(ctx, c.repo.ListBlobs()) if err != nil { return hints, append(errs, err) } @@ -189,7 +194,7 @@ func (c *Checker) Packs(ctx context.Context, errChan chan<- error) { defer close(errChan) // compute pack size using index entries - packs, err := pack.Size(ctx, c.repo, false) + packs, err := pack.Size(ctx, c.repo.ListBlobs(), false) if err != nil { errChan <- err return @@ -249,7 +254,7 @@ func (c *Checker) ReadPacks(ctx context.Context, filter func(packs map[restic.ID defer close(errChan) // compute pack size using index entries - packs, err := pack.Size(ctx, c.repo, false) + packs, err := pack.Size(ctx, c.repo.ListBlobs(), false) if err != nil { errChan <- err return diff --git a/internal/repository/checker_test.go b/internal/repository/checker_test.go index 4992ad833..9855cead7 100644 --- a/internal/repository/checker_test.go +++ b/internal/repository/checker_test.go @@ -41,7 +41,7 @@ func TestGapInBlobs(t *testing.T) { err := repo.LoadIndex(context.TODO(), restic.NoopTerminalCounterFactory) rtest.OK(t, err) - repoPacks, err := pack.Size(context.TODO(), repo, false) + repoPacks, err := pack.Size(context.TODO(), repo.ListBlobs(), false) rtest.OK(t, err) packID := restic.TestParseID("19a731a515618ec8b75fc0ff3b887d8feb83aef1001c9899f6702761142ed068") diff --git a/internal/repository/index/master_index.go b/internal/repository/index/master_index.go index 7a03587cf..6b45f4f83 100644 --- a/internal/repository/index/master_index.go +++ b/internal/repository/index/master_index.go @@ -234,6 +234,17 @@ func (mi *MasterIndex) Values() iter.Seq[*pack.PackedBlob] { } } +// PackBlobSeq adapts an iterator of index entries to restic.PackBlob. +func PackBlobSeq(blobs iter.Seq[*pack.PackedBlob]) iter.Seq[restic.PackBlob] { + return func(yield func(restic.PackBlob) bool) { + for blob := range blobs { + if !yield(blob) { + return + } + } + } +} + // MergeFinalIndexes merges all final indexes together. // After calling, there will be only one big final index in MasterIndex // containing all final index contents. diff --git a/internal/repository/index_list_test.go b/internal/repository/index_list_test.go index ce9fad969..0ee16ae45 100644 --- a/internal/repository/index_list_test.go +++ b/internal/repository/index_list_test.go @@ -26,9 +26,9 @@ func TestAllIndexBlobs(t *testing.T) { rtest.OK(t, repo.LoadIndex(context.TODO(), restic.NoopTerminalCounterFactory)) fromMaster := restic.NewBlobSet() - rtest.OK(t, repo.ListBlobs(context.TODO(), func(pb restic.PackBlob) { + for pb := range repo.ListBlobs() { fromMaster.Insert(pb.Handle()) - })) + } rtest.Equals(t, want, fromMaster) fromStream := restic.NewBlobSet() diff --git a/internal/repository/pack/pack.go b/internal/repository/pack/pack.go index b0a4be24c..4d96835b6 100644 --- a/internal/repository/pack/pack.go +++ b/internal/repository/pack/pack.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "fmt" "io" + "iter" "sync" "github.com/restic/restic/internal/debug" @@ -439,10 +440,13 @@ func CalculateHeaderSize(blobs Blobs) int { // If onlyHdr is set to true, only the size of the header is returned // Note that this function only gives correct sizes, if there are no // duplicates in the index. -func Size(ctx context.Context, idx restic.ListBlobser, onlyHdr bool) (map[restic.ID]int64, error) { +func Size(ctx context.Context, blobs iter.Seq[restic.PackBlob], onlyHdr bool) (map[restic.ID]int64, error) { packSize := make(map[restic.ID]int64) - err := idx.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range blobs { + if err := ctx.Err(); err != nil { + return packSize, err + } packID := blob.PackID() size, ok := packSize[packID] if !ok { @@ -452,7 +456,7 @@ func Size(ctx context.Context, idx restic.ListBlobser, onlyHdr bool) (map[restic size += int64(blob.CiphertextLength()) } packSize[packID] = size + int64(CalculateEntrySize(blob.IsCompressed())) - }) + } - return packSize, err + return packSize, nil } diff --git a/internal/repository/prune.go b/internal/repository/prune.go index 687f0a8c5..4e7607de7 100644 --- a/internal/repository/prune.go +++ b/internal/repository/prune.go @@ -4,6 +4,7 @@ import ( "cmp" "context" "fmt" + "iter" "math" "slices" "sort" @@ -127,7 +128,7 @@ func PlanPrune(ctx context.Context, opts PruneOptions, repo *Repository, getUsed } printer.P("searching used packs...\n") - keepBlobs, indexPack, err := packInfoFromIndex(ctx, repo, usedBlobs, &stats, printer) + keepBlobs, indexPack, err := packInfoFromIndex(ctx, repo.ListBlobs, usedBlobs, &stats, printer) if err != nil { return nil, err } @@ -141,15 +142,15 @@ func PlanPrune(ctx context.Context, opts PruneOptions, repo *Repository, getUsed if len(plan.repackPacks) != 0 { // when repacking, we do not want to keep blobs which are // already contained in kept packs, so delete them from keepBlobs - err := repo.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range repo.ListBlobs() { + if err := ctx.Err(); err != nil { + return nil, err + } packID := blob.PackID() if plan.removePacks.Has(packID) || plan.repackPacks.Has(packID) { - return + continue } keepBlobs.Delete(blob.Handle()) - }) - if err != nil { - return nil, err } } else { // keepBlobs is only needed if packs are repacked @@ -175,11 +176,15 @@ func PlanPrune(ctx context.Context, opts PruneOptions, repo *Repository, getUsed return &plan, nil } -func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *index.AssociatedSet[uint8], stats *PruneStats, printer restic.Printer) (*index.AssociatedSet[uint8], map[restic.ID]packInfo, error) { +func packInfoFromIndex(ctx context.Context, idx func() iter.Seq[restic.PackBlob], usedBlobs *index.AssociatedSet[uint8], stats *PruneStats, printer restic.Printer) (*index.AssociatedSet[uint8], map[restic.ID]packInfo, error) { // iterate over all blobs in index to find out which blobs are duplicates // The counter in usedBlobs describes how many instances of the blob exist in the repository index // Thus 0 == blob is missing, 1 == blob exists once, >= 2 == duplicates exist - err := idx.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range idx() { + if ctx.Err() != nil { + return nil, nil, ctx.Err() + } + bh := blob.Handle() count, ok := usedBlobs.Get(bh) if ok { @@ -192,9 +197,6 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i usedBlobs.Set(bh, count) } - }) - if err != nil { - return nil, nil, err } // Check if all used blobs have been found in index @@ -218,7 +220,7 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i indexPack := make(map[restic.ID]packInfo) // save computed pack header size - sz, err := pack.Size(ctx, idx, true) + sz, err := pack.Size(ctx, idx(), true) if err != nil { return nil, nil, err } @@ -229,7 +231,10 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i hasDuplicates := false // iterate over all blobs in index to generate packInfo - err = idx.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range idx() { + if ctx.Err() != nil { + return nil, nil, ctx.Err() + } packID := blob.PackID() h := blob.Handle() @@ -276,9 +281,6 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i } // update indexPack indexPack[packID] = ip - }) - if err != nil { - return nil, nil, err } // if duplicate blobs exist, those will be set to either "used" or "unused": @@ -289,14 +291,17 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i // - if there are no used blobs in a pack, possibly mark duplicates as "unused" if hasDuplicates { // iterate again over all blobs in index (this is pretty cheap, all in-mem) - err = idx.ListBlobs(ctx, func(blob restic.PackBlob) { + for blob := range idx() { + if ctx.Err() != nil { + return nil, nil, ctx.Err() + } packID := blob.PackID() bh := blob.Handle() count, ok := usedBlobs.Get(bh) // skip non-duplicate, aka. normal blobs // count == 0 is used to mark that this was a duplicate blob with only a single occurrence remaining if !ok || count == 1 { - return + continue } ip := indexPack[packID] @@ -329,9 +334,6 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i } // update indexPack indexPack[packID] = ip - }) - if err != nil { - return nil, nil, err } } diff --git a/internal/repository/prune_test.go b/internal/repository/prune_test.go index 3621a8ad4..f316c1cb8 100644 --- a/internal/repository/prune_test.go +++ b/internal/repository/prune_test.go @@ -150,7 +150,7 @@ func TestPruneSmall(t *testing.T) { })) // gather number of packfiles - repoPacks, err := pack.Size(context.TODO(), repo, false) + repoPacks, err := pack.Size(context.TODO(), repo.ListBlobs(), false) rtest.OK(t, err) lenPackfilesBefore := len(repoPacks) rtest.OK(t, repo.Close()) @@ -188,7 +188,7 @@ func TestPruneSmall(t *testing.T) { rtest.OK(t, err) } - repoPacks, err = pack.Size(context.TODO(), repo, false) + repoPacks, err = pack.Size(context.TODO(), repo.ListBlobs(), false) rtest.OK(t, err) lenPackfilesAfter := len(repoPacks) diff --git a/internal/repository/repair_index.go b/internal/repository/repair_index.go index 1df9b31b9..544248e70 100644 --- a/internal/repository/repair_index.go +++ b/internal/repository/repair_index.go @@ -43,7 +43,7 @@ func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions, return err } - packSizeFromIndex, err = pack.Size(ctx, repo, false) + packSizeFromIndex, err = pack.Size(ctx, repo.ListBlobs(), false) if err != nil { return err } diff --git a/internal/repository/repair_pack_test.go b/internal/repository/repair_pack_test.go index 814a31439..86b9fbc0c 100644 --- a/internal/repository/repair_pack_test.go +++ b/internal/repository/repair_pack_test.go @@ -15,9 +15,9 @@ import ( func listBlobs(repo restic.Repository) restic.BlobSet { blobs := restic.NewBlobSet() - _ = repo.ListBlobs(context.TODO(), func(pb restic.PackBlob) { + for pb := range repo.ListBlobs() { blobs.Insert(pb.Handle()) - }) + } return blobs } @@ -87,11 +87,11 @@ func testRepairBrokenPack(t *testing.T, version uint) { // all blobs in the file are broken damagedBlobs := restic.NewBlobSet() - rtest.OK(t, repo.ListBlobs(context.TODO(), func(pb restic.PackBlob) { + for pb := range repo.ListBlobs() { if pb.PackID().Equal(damagedID) { damagedBlobs.Insert(pb.Handle()) } - })) + } return restic.NewIDSet(damagedID), damagedBlobs }, }, { diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 3e29749c6..cd40a7c91 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "iter" "math" "runtime" "sync" @@ -713,16 +714,10 @@ func (r *Repository) LookupBlobSize(bh restic.BlobHandle) (uint, bool) { return r.idx.LookupSize(bh) } -// ListBlobs runs fn on all blobs known to the index. When the context is cancelled, -// the index iteration returns immediately with ctx.Err(). This blocks any modification of the index. -func (r *Repository) ListBlobs(ctx context.Context, fn func(restic.PackBlob)) error { - for blob := range r.idx.Values() { - if ctx.Err() != nil { - return ctx.Err() - } - fn(blob) - } - return nil +// ListBlobs returns an iterator over all blobs known to the index. +// While using the iterator, the index cannot be modified. +func (r *Repository) ListBlobs() iter.Seq[restic.PackBlob] { + return index.PackBlobSeq(r.idx.Values()) } // listPacksFromIndex returns index entries for the given packs, grouped by pack file. diff --git a/internal/restic/repository.go b/internal/restic/repository.go index 527ef0ff4..0bea6d4f7 100644 --- a/internal/restic/repository.go +++ b/internal/restic/repository.go @@ -25,9 +25,9 @@ type Repository interface { LookupBlobSize(bh BlobHandle) (size uint, exists bool) NewAssociatedBlobSet() AssociatedBlobSet - // ListBlobs runs fn on all blobs known to the index. When the context is cancelled, - // the index iteration returns immediately with ctx.Err(). This blocks any modification of the index. - ListBlobs(ctx context.Context, fn func(PackBlob)) error + // ListBlobs returns an iterator over all blobs known to the index. + // While using the iterator, the index cannot be modified. + ListBlobs() iter.Seq[PackBlob] // ListPackHandles returns the blob handles stored in the pack file header. ListPackHandles(ctx context.Context, id ID, packSize int64) ([]BlobHandle, error) @@ -108,10 +108,6 @@ type Unpacked[FT FileTypes] interface { RemoverUnpacked[FT] } -type ListBlobser interface { - ListBlobs(ctx context.Context, fn func(PackBlob)) error -} - type BlobLoader interface { LoadBlob(context.Context, BlobHandle, []byte) ([]byte, error) }