2016-08-01 16:31:44 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"restic/backend"
|
2016-08-04 16:59:26 +00:00
|
|
|
"restic/pack"
|
2016-08-01 16:31:44 +00:00
|
|
|
"restic/repository"
|
|
|
|
)
|
|
|
|
|
2016-08-01 18:44:02 +00:00
|
|
|
// findUsedBlobs traverse the tree ID and adds all seen blobs to blobs.
|
2016-08-04 16:59:26 +00:00
|
|
|
func findUsedBlobs(repo *repository.Repository, treeID backend.ID, blobs pack.BlobSet, seen pack.BlobSet) error {
|
|
|
|
blobs.Insert(pack.Handle{ID: treeID, Type: pack.Tree})
|
2016-08-01 16:40:08 +00:00
|
|
|
|
|
|
|
tree, err := LoadTree(repo, treeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range tree.Nodes {
|
|
|
|
switch node.Type {
|
|
|
|
case "file":
|
|
|
|
for _, blob := range node.Content {
|
2016-08-04 16:59:26 +00:00
|
|
|
blobs.Insert(pack.Handle{ID: blob, Type: pack.Data})
|
2016-08-01 16:40:08 +00:00
|
|
|
}
|
|
|
|
case "dir":
|
2016-08-01 16:45:03 +00:00
|
|
|
subtreeID := *node.Subtree
|
2016-08-04 16:59:26 +00:00
|
|
|
h := pack.Handle{ID: subtreeID, Type: pack.Tree}
|
|
|
|
if seen.Has(h) {
|
2016-08-01 16:45:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:59:26 +00:00
|
|
|
seen.Insert(h)
|
2016-08-01 16:45:03 +00:00
|
|
|
|
|
|
|
err := findUsedBlobs(repo, subtreeID, blobs, seen)
|
2016-08-01 16:40:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-01 18:44:02 +00:00
|
|
|
// FindUsedBlobs traverses the tree ID and adds all seen blobs (trees and data blobs) to the set blobs.
|
2016-08-04 16:59:26 +00:00
|
|
|
func FindUsedBlobs(repo *repository.Repository, treeID backend.ID, blobs pack.BlobSet) error {
|
|
|
|
return findUsedBlobs(repo, treeID, blobs, pack.NewBlobSet())
|
2016-08-01 16:31:44 +00:00
|
|
|
}
|