2025-09-23 14:01:09 -04:00
|
|
|
package data
|
2016-08-01 12:31:44 -04:00
|
|
|
|
2020-11-06 19:12:07 -05:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2020-11-07 08:16:04 -05:00
|
|
|
"github.com/restic/restic/internal/ui/progress"
|
2020-11-06 19:12:07 -05:00
|
|
|
)
|
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
|
2020-02-01 15:09:52 -05:00
|
|
|
// blobs) to the set blobs. Already seen tree blobs will not be visited again.
|
2025-09-23 14:01:09 -04:00
|
|
|
func FindUsedBlobs(ctx context.Context, repo restic.Loader, treeIDs restic.IDs, blobs restic.FindBlobSet, p *progress.Counter) error {
|
2020-11-06 19:12:07 -05:00
|
|
|
var lock sync.Mutex
|
2016-08-01 12:40:08 -04:00
|
|
|
|
2025-11-22 13:52:46 -05:00
|
|
|
return StreamTrees(ctx, repo, treeIDs, p, func(treeID restic.ID) bool {
|
2020-11-06 19:12:07 -05:00
|
|
|
// locking is necessary the goroutine below concurrently adds data blobs
|
|
|
|
|
lock.Lock()
|
2025-09-23 14:01:09 -04:00
|
|
|
h := restic.BlobHandle{ID: treeID, Type: restic.TreeBlob}
|
2020-11-06 19:12:07 -05:00
|
|
|
blobReferenced := blobs.Has(h)
|
|
|
|
|
// noop if already referenced
|
|
|
|
|
blobs.Insert(h)
|
|
|
|
|
lock.Unlock()
|
|
|
|
|
return blobReferenced
|
2025-11-22 13:55:50 -05:00
|
|
|
}, func(_ restic.ID, err error, nodes TreeNodeIterator) error {
|
2025-11-22 13:52:46 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-11-06 19:12:07 -05:00
|
|
|
|
2025-11-22 13:55:50 -05:00
|
|
|
for item := range nodes {
|
|
|
|
|
if item.Error != nil {
|
|
|
|
|
return item.Error
|
|
|
|
|
}
|
2020-11-06 19:12:07 -05:00
|
|
|
lock.Lock()
|
2025-11-22 13:55:50 -05:00
|
|
|
switch item.Node.Type {
|
2025-11-22 13:52:46 -05:00
|
|
|
case NodeTypeFile:
|
2025-11-22 13:55:50 -05:00
|
|
|
for _, blob := range item.Node.Content {
|
2025-11-22 13:52:46 -05:00
|
|
|
blobs.Insert(restic.BlobHandle{ID: blob, Type: restic.DataBlob})
|
2020-11-06 19:12:07 -05:00
|
|
|
}
|
2016-08-01 12:40:08 -04:00
|
|
|
}
|
2020-11-06 19:12:07 -05:00
|
|
|
lock.Unlock()
|
2016-08-01 12:40:08 -04:00
|
|
|
}
|
2020-11-06 19:12:07 -05:00
|
|
|
return nil
|
|
|
|
|
})
|
2016-08-01 12:40:08 -04:00
|
|
|
}
|