-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
184 lines (151 loc) · 4.76 KB
/
resolver.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Package path implements utilities for resolving paths within ipfs.
package path
import (
"context"
"errors"
"fmt"
"time"
dag "github.com/ipfs/go-ipfs/merkledag"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
)
var log = logging.Logger("path")
// Paths after a protocol must contain at least one component
var ErrNoComponents = errors.New(
"path must contain at least one component")
// ErrNoLink is returned when a link is not found in a path
type ErrNoLink struct {
Name string
Node *cid.Cid
}
func (e ErrNoLink) Error() string {
return fmt.Sprintf("no link named %q under %s", e.Name, e.Node.String())
}
// Resolver provides path resolution to IPFS
// It has a pointer to a DAGService, which is uses to resolve nodes.
// TODO: now that this is more modular, try to unify this code with the
// the resolvers in namesys
type Resolver struct {
DAG dag.DAGService
ResolveOnce func(ctx context.Context, ds dag.DAGService, nd node.Node, names []string) (*node.Link, []string, error)
}
func NewBasicResolver(ds dag.DAGService) *Resolver {
return &Resolver{
DAG: ds,
ResolveOnce: ResolveSingle,
}
}
// SplitAbsPath clean up and split fpath. It extracts the first component (which
// must be a Multihash) and return it separately.
func SplitAbsPath(fpath Path) (*cid.Cid, []string, error) {
log.Debugf("Resolve: '%s'", fpath)
parts := fpath.Segments()
if parts[0] == "ipfs" {
parts = parts[1:]
}
// if nothing, bail.
if len(parts) == 0 {
return nil, nil, ErrNoComponents
}
c, err := cid.Decode(parts[0])
// first element in the path is a cid
if err != nil {
log.Debug("given path element is not a cid.\n")
return nil, nil, err
}
return c, parts[1:], nil
}
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath Path) (node.Node, []string, error) {
c, p, err := SplitAbsPath(fpath)
if err != nil {
return nil, nil, err
}
nd, err := r.DAG.Get(ctx, c)
if err != nil {
return nil, nil, err
}
for len(p) > 0 {
val, rest, err := nd.Resolve(p)
if err != nil {
return nil, nil, err
}
switch val := val.(type) {
case *node.Link:
next, err := val.GetNode(ctx, r.DAG)
if err != nil {
return nil, nil, err
}
nd = next
p = rest
default:
return nd, p, nil
}
}
return nd, nil, nil
}
// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (node.Node, error) {
// validate path
if err := fpath.IsValid(); err != nil {
return nil, err
}
nodes, err := s.ResolvePathComponents(ctx, fpath)
if err != nil || nodes == nil {
return nil, err
}
return nodes[len(nodes)-1], err
}
// ResolveSingle simply resolves one hop of a path through a graph with no
// extra context (does not opaquely resolve through sharded nodes)
func ResolveSingle(ctx context.Context, ds dag.DAGService, nd node.Node, names []string) (*node.Link, []string, error) {
return nd.ResolveLink(names)
}
// ResolvePathComponents fetches the nodes for each segment of the given path.
// It uses the first path component as a hash (key) of the first node, then
// resolves all other components walking the links, with ResolveLinks.
func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]node.Node, error) {
h, parts, err := SplitAbsPath(fpath)
if err != nil {
return nil, err
}
log.Debug("resolve dag get")
nd, err := s.DAG.Get(ctx, h)
if err != nil {
return nil, err
}
return s.ResolveLinks(ctx, nd, parts)
}
// ResolveLinks iteratively resolves names by walking the link hierarchy.
// Every node is fetched from the DAGService, resolving the next name.
// Returns the list of nodes forming the path, starting with ndd. This list is
// guaranteed never to be empty.
//
// ResolveLinks(nd, []string{"foo", "bar", "baz"})
// would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links
func (s *Resolver) ResolveLinks(ctx context.Context, ndd node.Node, names []string) ([]node.Node, error) {
result := make([]node.Node, 0, len(names)+1)
result = append(result, ndd)
nd := ndd // dup arg workaround
// for each of the path components
for len(names) > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Minute)
defer cancel()
lnk, rest, err := s.ResolveOnce(ctx, s.DAG, nd, names)
if err == dag.ErrLinkNotFound {
return result, ErrNoLink{Name: names[0], Node: nd.Cid()}
} else if err != nil {
return result, err
}
nextnode, err := lnk.GetNode(ctx, s.DAG)
if err != nil {
return result, err
}
nd = nextnode
result = append(result, nextnode)
names = rest
}
return result, nil
}