restic/internal/restic/find.go

40 lines
885 B
Go
Raw Normal View History

2016-08-01 18:31:44 +02:00
package restic
2017-06-04 11:16:55 +02:00
import "context"
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. The tree blobs in the `seen` BlobSet will not be visited
// again.
2017-06-04 11:16:55 +02:00
func FindUsedBlobs(ctx context.Context, repo Repository, treeID ID, blobs BlobSet, seen BlobSet) error {
2016-08-31 20:58:57 +02:00
blobs.Insert(BlobHandle{ID: treeID, Type: TreeBlob})
2016-08-01 18:40:08 +02:00
2017-06-04 11:16:55 +02:00
tree, err := repo.LoadTree(ctx, treeID)
2016-08-01 18:40:08 +02:00
if err != nil {
return err
}
for _, node := range tree.Nodes {
2016-09-01 21:20:03 +02:00
switch node.Type {
2016-08-01 18:40:08 +02:00
case "file":
for _, blob := range node.Content {
2016-08-31 20:58:57 +02:00
blobs.Insert(BlobHandle{ID: blob, Type: DataBlob})
2016-08-01 18:40:08 +02:00
}
case "dir":
2016-08-01 18:45:03 +02:00
subtreeID := *node.Subtree
2016-08-31 20:58:57 +02:00
h := BlobHandle{ID: subtreeID, Type: TreeBlob}
if seen.Has(h) {
2016-08-01 18:45:03 +02:00
continue
}
seen.Insert(h)
2016-08-01 18:45:03 +02:00
2017-06-04 11:16:55 +02:00
err := FindUsedBlobs(ctx, repo, subtreeID, blobs, seen)
2016-08-01 18:40:08 +02:00
if err != nil {
return err
}
}
}
return nil
}