Prevent loops when finding used blobs

This commit is contained in:
Alexander Neumann 2016-08-01 18:45:03 +02:00
parent ffc3503e6f
commit bdd085e9f1
1 changed files with 10 additions and 3 deletions

View File

@ -6,7 +6,7 @@ import (
) )
// FindUsedBlobs traverse the tree ID and adds all seen blobs to blobs. // FindUsedBlobs traverse the tree ID and adds all seen blobs to blobs.
func findUsedBlobs(repo *repository.Repository, treeID backend.ID, blobs backend.IDSet) error { func findUsedBlobs(repo *repository.Repository, treeID backend.ID, blobs backend.IDSet, seen backend.IDSet) error {
blobs.Insert(treeID) blobs.Insert(treeID)
tree, err := LoadTree(repo, treeID) tree, err := LoadTree(repo, treeID)
@ -21,7 +21,14 @@ func findUsedBlobs(repo *repository.Repository, treeID backend.ID, blobs backend
blobs.Insert(blob) blobs.Insert(blob)
} }
case "dir": case "dir":
err := findUsedBlobs(repo, *node.Subtree, blobs) subtreeID := *node.Subtree
if seen.Has(subtreeID) {
continue
}
seen.Insert(subtreeID)
err := findUsedBlobs(repo, subtreeID, blobs, seen)
if err != nil { if err != nil {
return err return err
} }
@ -35,5 +42,5 @@ func findUsedBlobs(repo *repository.Repository, treeID backend.ID, blobs backend
// encountered. // encountered.
func FindUsedBlobs(repo *repository.Repository, treeID backend.ID) (blobs backend.IDSet, err error) { func FindUsedBlobs(repo *repository.Repository, treeID backend.ID) (blobs backend.IDSet, err error) {
blobs = backend.NewIDSet() blobs = backend.NewIDSet()
return blobs, findUsedBlobs(repo, treeID, blobs) return blobs, findUsedBlobs(repo, treeID, blobs, backend.NewIDSet())
} }