restic/internal/data/find.go

46 lines
1.2 KiB
Go
Raw Permalink Normal View History

package data
2016-08-01 12:31:44 -04:00
import (
"context"
"sync"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/progress"
)
2017-06-04 05:16:55 -04:00
2016-08-15 11:58:32 -04: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 12:40:08 -04:00
return StreamTrees(ctx, repo, treeIDs, p, 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
}, func(_ restic.ID, err error, nodes TreeNodeIterator) error {
if err != nil {
return err
}
for item := range nodes {
if item.Error != nil {
return item.Error
}
lock.Lock()
switch item.Node.Type {
case NodeTypeFile:
for _, blob := range item.Node.Content {
blobs.Insert(restic.BlobHandle{ID: blob, Type: restic.DataBlob})
}
2016-08-01 12:40:08 -04:00
}
lock.Unlock()
2016-08-01 12:40:08 -04:00
}
return nil
})
2016-08-01 12:40:08 -04:00
}