Skip to content

Commit a731afe

Browse files
authoredJul 21, 2023
Merge pull request #55 from ipfs/feat/error-on-failed-preload
Fail to construct preload hamt shards when traversal fails
2 parents 62f4a1c + 2450f69 commit a731afe

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed
 

‎hamt/shardeddir.go

+24-7
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ func NewUnixFSHAMTShardWithPreload(ctx context.Context, substrate dagpb.PBNode,
6565
return n, err
6666
}
6767

68-
traverse := n.Length()
68+
traverse, err := n.(*_UnixFSHAMTShard).length()
6969
if traverse == -1 {
7070
return n, fmt.Errorf("could not fully explore hamt during preload")
7171
}
72+
if err != nil {
73+
return n, err
74+
}
7275

7376
return n, nil
7477
}
@@ -261,9 +264,9 @@ func (n UnixFSHAMTShard) ListIterator() ipld.ListIterator {
261264

262265
// Length returns the length of a list, or the number of entries in a map,
263266
// or -1 if the node is not of list nor map kind.
264-
func (n UnixFSHAMTShard) Length() int64 {
267+
func (n UnixFSHAMTShard) length() (int64, error) {
265268
if n.cachedLength != -1 {
266-
return n.cachedLength
269+
return n.cachedLength, nil
267270
}
268271
maxPadLen := maxPadLength(n.data)
269272
total := int64(0)
@@ -272,20 +275,34 @@ func (n UnixFSHAMTShard) Length() int64 {
272275
_, pbLink := itr.Next()
273276
isValue, err := isValueLink(pbLink, maxPadLen)
274277
if err != nil {
275-
continue
278+
return 0, err
276279
}
277280
if isValue {
278281
total++
279282
} else {
280283
child, err := n.loadChild(pbLink)
281284
if err != nil {
282-
continue
285+
return 0, err
283286
}
284-
total += child.Length()
287+
cl, err := child.length()
288+
if err != nil {
289+
return 0, err
290+
}
291+
total += cl
285292
}
286293
}
287294
n.cachedLength = total
288-
return total
295+
return total, nil
296+
}
297+
298+
// Length returns the length of a list, or the number of entries in a map,
299+
// or -1 if the node is not of list nor map kind.
300+
func (n UnixFSHAMTShard) Length() int64 {
301+
len, err := n.length()
302+
if err != nil {
303+
return 0
304+
}
305+
return len
289306
}
290307

291308
func (n UnixFSHAMTShard) IsAbsent() bool {

0 commit comments

Comments
 (0)
Please sign in to comment.