From 2450f6964c627b6a5cda2df8194858a0c020d982 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Thu, 6 Jul 2023 16:00:18 -0700 Subject: [PATCH] Fail to construct preload hamt shards when traversal fails Fix #54 --- hamt/shardeddir.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/hamt/shardeddir.go b/hamt/shardeddir.go index a273b29..fa4067f 100644 --- a/hamt/shardeddir.go +++ b/hamt/shardeddir.go @@ -65,10 +65,13 @@ func NewUnixFSHAMTShardWithPreload(ctx context.Context, substrate dagpb.PBNode, return n, err } - traverse := n.Length() + traverse, err := n.(*_UnixFSHAMTShard).length() if traverse == -1 { return n, fmt.Errorf("could not fully explore hamt during preload") } + if err != nil { + return n, err + } return n, nil } @@ -261,9 +264,9 @@ func (n UnixFSHAMTShard) ListIterator() ipld.ListIterator { // Length returns the length of a list, or the number of entries in a map, // or -1 if the node is not of list nor map kind. -func (n UnixFSHAMTShard) Length() int64 { +func (n UnixFSHAMTShard) length() (int64, error) { if n.cachedLength != -1 { - return n.cachedLength + return n.cachedLength, nil } maxPadLen := maxPadLength(n.data) total := int64(0) @@ -272,20 +275,34 @@ func (n UnixFSHAMTShard) Length() int64 { _, pbLink := itr.Next() isValue, err := isValueLink(pbLink, maxPadLen) if err != nil { - continue + return 0, err } if isValue { total++ } else { child, err := n.loadChild(pbLink) if err != nil { - continue + return 0, err } - total += child.Length() + cl, err := child.length() + if err != nil { + return 0, err + } + total += cl } } n.cachedLength = total - return total + return total, nil +} + +// Length returns the length of a list, or the number of entries in a map, +// or -1 if the node is not of list nor map kind. +func (n UnixFSHAMTShard) Length() int64 { + len, err := n.length() + if err != nil { + return 0 + } + return len } func (n UnixFSHAMTShard) IsAbsent() bool {