-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
57 lines (48 loc) · 1.29 KB
/
resolve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package io
import (
"context"
dag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"
hamt "github.com/ipfs/go-ipfs/unixfs/hamt"
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
)
// ResolveUnixfsOnce resolves a single hop of a path through a graph in a
// unixfs context. This includes handling traversing sharded directories.
func ResolveUnixfsOnce(ctx context.Context, ds dag.DAGService, nd node.Node, names []string) (*node.Link, []string, error) {
switch nd := nd.(type) {
case *dag.ProtoNode:
upb, err := ft.FromBytes(nd.Data())
if err != nil {
// Not a unixfs node, use standard object traversal code
lnk, err := nd.GetNodeLink(names[0])
if err != nil {
return nil, nil, err
}
return lnk, names[1:], nil
}
switch upb.GetType() {
case ft.THAMTShard:
s, err := hamt.NewHamtFromDag(ds, nd)
if err != nil {
return nil, nil, err
}
out, err := s.Find(ctx, names[0])
if err != nil {
return nil, nil, err
}
return out, names[1:], nil
default:
lnk, err := nd.GetNodeLink(names[0])
if err != nil {
return nil, nil, err
}
return lnk, names[1:], nil
}
default:
lnk, rest, err := nd.ResolveLink(names)
if err != nil {
return nil, nil, err
}
return lnk, rest, nil
}
}