2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-30 07:30:50 +00:00
restic/internal/restic/find.go

40 lines
881 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"
// 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
// blobs) to the set blobs. Already seen tree blobs will not be visited again.
func FindUsedBlobs(ctx context.Context, repo TreeLoader, treeID ID, blobs BlobSet) error {
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":
err := FindUsedBlobs(ctx, repo, *node.Subtree, blobs)
2016-08-01 16:40:08 +00:00
if err != nil {
return err
}
}
}
return nil
}