restic/internal/find.go

40 lines
885 B
Go
Raw Normal View History

2016-08-01 16:31:44 +00:00
package restic
2017-06-04 09:16:55 +00:00
import "context"
2016-08-15 15:58:32 +00:00
// FindUsedBlobs traverses the tree ID and adds all seen blobs (trees and data
// blobs) to the set blobs. The tree blobs in the `seen` BlobSet will not be visited
// again.
2017-06-04 09:16:55 +00:00
func FindUsedBlobs(ctx context.Context, repo Repository, treeID ID, blobs BlobSet, seen BlobSet) error {
2016-08-31 18:58:57 +00:00
blobs.Insert(BlobHandle{ID: treeID, Type: TreeBlob})
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":
2016-08-01 16:45:03 +00:00
subtreeID := *node.Subtree
2016-08-31 18:58:57 +00:00
h := BlobHandle{ID: subtreeID, Type: TreeBlob}
if seen.Has(h) {
2016-08-01 16:45:03 +00:00
continue
}
seen.Insert(h)
2016-08-01 16:45:03 +00:00
2017-06-04 09:16:55 +00:00
err := FindUsedBlobs(ctx, repo, subtreeID, blobs, seen)
2016-08-01 16:40:08 +00:00
if err != nil {
return err
}
}
}
return nil
}