2016-08-01 16:31:44 +00:00
|
|
|
package restic
|
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
import "context"
|
|
|
|
|
2020-02-22 20:21:09 +00:00
|
|
|
// TreeLoader loads a tree from a repository.
|
|
|
|
type TreeLoader interface {
|
|
|
|
LoadTree(context.Context, ID) (*Tree, error)
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:58:32 +00:00
|
|
|
// FindUsedBlobs traverses the tree ID and adds all seen blobs (trees and data
|
2020-02-01 20:09:52 +00:00
|
|
|
// blobs) to the set blobs. Already seen tree blobs will not be visited again.
|
2020-02-22 20:21:09 +00:00
|
|
|
func FindUsedBlobs(ctx context.Context, repo TreeLoader, treeID ID, blobs BlobSet) error {
|
2020-02-22 20:17:39 +00:00
|
|
|
h := BlobHandle{ID: treeID, Type: TreeBlob}
|
|
|
|
if blobs.Has(h) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
blobs.Insert(h)
|
2016-08-01 16:40:08 +00:00
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
tree, err := repo.LoadTree(ctx, treeID)
|
2016-08-01 16:40:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range tree.Nodes {
|
2016-09-01 19:20:03 +00:00
|
|
|
switch node.Type {
|
2016-08-01 16:40:08 +00:00
|
|
|
case "file":
|
|
|
|
for _, blob := range node.Content {
|
2016-08-31 18:58:57 +00:00
|
|
|
blobs.Insert(BlobHandle{ID: blob, Type: DataBlob})
|
2016-08-01 16:40:08 +00:00
|
|
|
}
|
|
|
|
case "dir":
|
2020-02-22 20:17:39 +00:00
|
|
|
err := FindUsedBlobs(ctx, repo, *node.Subtree, blobs)
|
2016-08-01 16:40:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|