Files
restic/internal/data/find.go

50 lines
1.2 KiB
Go
Raw Normal View History

package data
2016-08-01 18:31:44 +02:00
import (
"context"
"sync"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/progress"
"golang.org/x/sync/errgroup"
)
2017-06-04 11:16:55 +02:00
2016-08-15 17:58:32 +02:00
// FindUsedBlobs traverses the tree ID and adds all seen blobs (trees and data
// blobs) to the set blobs. Already seen tree blobs will not be visited again.
func FindUsedBlobs(ctx context.Context, repo restic.Loader, treeIDs restic.IDs, blobs restic.FindBlobSet, p *progress.Counter) error {
var lock sync.Mutex
2016-08-01 18:40:08 +02:00
wg, ctx := errgroup.WithContext(ctx)
treeStream := StreamTrees(ctx, wg, repo, treeIDs, func(treeID restic.ID) bool {
// locking is necessary the goroutine below concurrently adds data blobs
lock.Lock()
h := restic.BlobHandle{ID: treeID, Type: restic.TreeBlob}
blobReferenced := blobs.Has(h)
// noop if already referenced
blobs.Insert(h)
lock.Unlock()
return blobReferenced
}, p)
2016-08-01 18:40:08 +02:00
wg.Go(func() error {
for tree := range treeStream {
if tree.Error != nil {
return tree.Error
2016-08-01 18:40:08 +02:00
}
lock.Lock()
for _, node := range tree.Nodes {
switch node.Type {
2024-07-09 19:51:44 +02:00
case NodeTypeFile:
for _, blob := range node.Content {
blobs.Insert(restic.BlobHandle{ID: blob, Type: restic.DataBlob})
}
}
2016-08-01 18:40:08 +02:00
}
lock.Unlock()
2016-08-01 18:40:08 +02:00
}
return nil
})
return wg.Wait()
2016-08-01 18:40:08 +02:00
}