@cryptotaxi247 / kubo / commits / f5f44ab24

coreapi: remove ctx from ParsePath, split ParseCid

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Mar 30, 2018 at 22:21 UTC f5f44ab246bb58d39372df88760722dd9e9f14e5
10 files changed +60 -38
core/coreapi/block.go
+9 -5
@@ -65,11 +65,11 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
65 return nil, err
66 }
67
68 - return api.ParseCid(b.Cid()), nil
68 + return api.core().IpldPath(b.Cid()), nil
69 }
70
71 func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
72 - rp, err := api.ResolvePath(ctx, p)
72 + rp, err := api.core().ResolvePath(ctx, p)
73 if err != nil {
74 return nil, err
75 }
@@ -83,7 +83,7 @@ func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, erro
83 }
84
85 func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.BlockRmOption) error {
86 - rp, err := api.ResolvePath(ctx, p)
86 + rp, err := api.core().ResolvePath(ctx, p)
87 if err != nil {
88 return err
89 }
@@ -121,7 +121,7 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
121 }
122
123 func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.BlockStat, error) {
124 - rp, err := api.ResolvePath(ctx, p)
124 + rp, err := api.core().ResolvePath(ctx, p)
125 if err != nil {
126 return nil, err
127 }
@@ -132,7 +132,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.Bloc
132 }
133
134 return &BlockStat{
135 - path: api.ParseCid(b.Cid()),
135 + path: api.core().IpldPath(b.Cid()),
136 size: len(b.RawData()),
137 }, nil
138 }
@@ -144,3 +144,7 @@ func (bs *BlockStat) Size() int {
144 func (bs *BlockStat) Path() coreiface.ResolvedPath {
145 return bs.path
146 }
147 +
148 +func (api *BlockAPI) core() coreiface.CoreAPI {
149 + return (*CoreAPI)(api)
150 +}
core/coreapi/dag.go
+2 -2
@@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
44 return nil, err
45 }
46
47 - return api.ParseCid(nds[0].Cid()), nil
47 + return api.core().IpldPath(nds[0].Cid()), nil
48 }
49
50 // Get resolves `path` using Unixfs resolver, returns the resolved Node.
@@ -66,7 +66,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
66 paths := n.Tree("", settings.Depth)
67 out := make([]coreiface.Path, len(paths))
68 for n, p2 := range paths {
69 - out[n], err = api.ParsePath(ctx, gopath.Join(p.String(), p2))
69 + out[n], err = api.core().ParsePath(gopath.Join(p.String(), p2))
70 if err != nil {
71 return nil, err
72 }
core/coreapi/interface/coreapi.go
+6 -3
@@ -40,8 +40,11 @@ type CoreAPI interface {
40 ResolveNode(context.Context, Path) (ipld.Node, error)
41
42 // ParsePath parses string path to a Path
43 - ParsePath(context.Context, string) (Path, error)
43 + ParsePath(string) (Path, error)
44
45 - // ParseCid creates new path from the provided CID
46 - ParseCid(*cid.Cid) ResolvedPath
45 + // IpfsPath creates new /ipfs path from the provided CID
46 + IpfsPath(*cid.Cid) ResolvedPath
47 +
48 + // IpldPath creates new /ipld path from the provided CID
49 + IpldPath(*cid.Cid) ResolvedPath
50 }
core/coreapi/object.go
+4 -4
@@ -121,7 +121,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
121 return nil, err
122 }
123
124 - return api.ParseCid(dagnode.Cid()), nil
124 + return api.core().IpfsPath(dagnode.Cid()), nil
125 }
126
127 func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) {
@@ -218,7 +218,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
218 return nil, err
219 }
220
221 - return api.ParseCid(nnode.Cid()), nil
221 + return api.core().IpfsPath(nnode.Cid()), nil
222 }
223
224 func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
@@ -244,7 +244,7 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
244 return nil, err
245 }
246
247 - return api.ParseCid(nnode.Cid()), nil
247 + return api.core().IpfsPath(nnode.Cid()), nil
248 }
249
250 func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
@@ -281,7 +281,7 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R
281 return nil, err
282 }
283
284 - return api.ParseCid(pbnd.Cid()), nil
284 + return api.core().IpfsPath(pbnd.Cid()), nil
285 }
286
287 func (api *ObjectAPI) core() coreiface.CoreAPI {
core/coreapi/path.go
+10 -5
@@ -26,12 +26,17 @@ type resolvedPath struct {
26 root *cid.Cid
27 }
28
29 -// ParseCid parses the path from `c`, retruns the parsed path.
30 -func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.ResolvedPath {
31 - return &resolvedPath{path: path{ipfspath.FromCid(c)}, cid: c, root: c}
29 +// IpfsPath parses the path from `c`, reruns the parsed path.
30 +func (api *CoreAPI) IpfsPath(c *cid.Cid) coreiface.ResolvedPath {
31 + return &resolvedPath{path: path{ipfspath.Path("/ipfs/" + c.String())}, cid: c, root: c}
32 }
33
34 -// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
34 +// IpldPath parses the path from `c`, reruns the parsed path.
35 +func (api *CoreAPI) IpldPath(c *cid.Cid) coreiface.ResolvedPath {
36 + return &resolvedPath{path: path{ipfspath.Path("/ipld/" + c.String())}, cid: c, root: c}
37 +}
38 +
39 +// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
40 // resolved Node.
41 func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
42 return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
@@ -83,7 +88,7 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
88 }
89
90 // ParsePath parses path `p` using ipfspath parser, returns the parsed path.
86 -func (api *CoreAPI) ParsePath(ctx context.Context, p string) (coreiface.Path, error) {
91 +func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
92 pp, err := ipfspath.ParsePath(p)
93 if err != nil {
94 return nil, err
core/coreapi/pin.go
+14 -5
@@ -24,7 +24,12 @@ func (api *PinAPI) Add(ctx context.Context, p coreiface.Path, opts ...caopts.Pin
24
25 defer api.node.Blockstore.PinLock().Unlock()
26
27 - _, err = corerepo.Pin(api.node, ctx, []string{p.String()}, settings.Recursive)
27 + rp, err := api.core().ResolvePath(ctx, p)
28 + if err != nil {
29 + return err
30 + }
31 +
32 + _, err = corerepo.Pin(api.node, ctx, []string{rp.Cid().String()}, settings.Recursive)
33 if err != nil {
34 return err
35 }
@@ -62,12 +67,12 @@ func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface
67 return err
68 }
69
65 - fp, err := api.ResolvePath(ctx, from)
70 + fp, err := api.core().ResolvePath(ctx, from)
71 if err != nil {
72 return err
73 }
74
70 - tp, err := api.ResolvePath(ctx, to)
75 + tp, err := api.core().ResolvePath(ctx, to)
76 if err != nil {
77 return err
78 }
@@ -120,7 +125,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
125 links, err := getLinks(ctx, root)
126 if err != nil {
127 status := &pinStatus{ok: false, cid: root}
123 - status.badNodes = []coreiface.BadPinNode{&badNode{path: api.ParseCid(root), err: err}}
128 + status.badNodes = []coreiface.BadPinNode{&badNode{path: api.core().IpldPath(root), err: err}}
129 visited[key] = status
130 return status
131 }
@@ -170,7 +175,7 @@ func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pi
175 for _, c := range keyList {
176 keys[c.String()] = &pinInfo{
177 pinType: typeStr,
173 - path: api.ParseCid(c),
178 + path: api.core().IpldPath(c),
179 }
180 }
181 }
@@ -199,3 +204,7 @@ func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pi
204
205 return out, nil
206 }
207 +
208 +func (api *PinAPI) core() coreiface.CoreAPI {
209 + return (*CoreAPI)(api)
210 +}
core/coreapi/pin_test.go
+3 -3
@@ -52,7 +52,7 @@ func TestPinSimple(t *testing.T) {
52 t.Errorf("unexpected pin list len: %d", len(list))
53 }
54
55 - if list[0].Path().String() != p.String() {
55 + if list[0].Path().Cid().String() != p.Cid().String() {
56 t.Error("paths don't match")
57 }
58
@@ -156,7 +156,7 @@ func TestPinRecursive(t *testing.T) {
156 t.Errorf("unexpected pin list len: %d", len(list))
157 }
158
159 - if list[0].Path().String() != p0.String() {
159 + if list[0].Path().Cid().String() != p0.Cid().String() {
160 t.Error("unexpected path")
161 }
162
@@ -195,7 +195,7 @@ func TestPinRecursive(t *testing.T) {
195 t.Fatalf("unexpected badNodes len")
196 }
197
198 - if r.BadNodes()[0].Path().String() != p0.String() {
198 + if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() {
199 t.Error("unexpected badNode path")
200 }
201
core/coreapi/unixfs.go
+1 -1
@@ -25,7 +25,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.ResolvedP
25 if err != nil {
26 return nil, err
27 }
28 - return api.core().ParseCid(c), nil
28 + return api.core().IpfsPath(c), nil
29 }
30
31 // Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.
core/coreapi/unixfs_test.go
+9 -8
@@ -12,6 +12,7 @@ import (
12 core "github.com/ipfs/go-ipfs/core"
13 coreapi "github.com/ipfs/go-ipfs/core/coreapi"
14 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15 + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16 coreunix "github.com/ipfs/go-ipfs/core/coreunix"
17 keystore "github.com/ipfs/go-ipfs/keystore"
18 mdag "github.com/ipfs/go-ipfs/merkledag"
@@ -213,18 +214,18 @@ func TestCatDir(t *testing.T) {
214 if err != nil {
215 t.Error(err)
216 }
216 - p := api.ParseCid(edir.Cid())
217 + p := api.IpfsPath(edir.Cid())
218
218 - emptyDir, err := api.Object().New(ctx, api.Object().WithType("unixfs-dir"))
219 + emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir"))
220 if err != nil {
221 t.Error(err)
222 }
223
223 - if p.String() != api.ParseCid(emptyDir.Cid()).String() {
224 + if p.String() != api.IpfsPath(emptyDir.Cid()).String() {
225 t.Fatalf("expected path %s, got: %s", emptyDir.Cid(), p.String())
226 }
227
227 - _, err = api.Unixfs().Cat(ctx, api.ParseCid(emptyDir.Cid()))
228 + _, err = api.Unixfs().Cat(ctx, api.IpfsPath(emptyDir.Cid()))
229 if err != coreiface.ErrIsDir {
230 t.Fatalf("expected ErrIsDir, got: %s", err)
231 }
@@ -243,7 +244,7 @@ func TestCatNonUnixfs(t *testing.T) {
244 t.Error(err)
245 }
246
246 - _, err = api.Unixfs().Cat(ctx, api.ParseCid(nd.Cid()))
247 + _, err = api.Unixfs().Cat(ctx, api.IpfsPath(nd.Cid()))
248 if !strings.Contains(err.Error(), "proto: required field") {
249 t.Fatalf("expected protobuf error, got: %s", err)
250 }
@@ -318,12 +319,12 @@ func TestLsEmptyDir(t *testing.T) {
319 t.Error(err)
320 }
321
321 - emptyDir, err := api.Object().New(ctx, api.Object().WithType("unixfs-dir"))
322 + emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir"))
323 if err != nil {
324 t.Error(err)
325 }
326
326 - links, err := api.Unixfs().Ls(ctx, api.ParseCid(emptyDir.Cid()))
327 + links, err := api.Unixfs().Ls(ctx, api.IpfsPath(emptyDir.Cid()))
328 if err != nil {
329 t.Error(err)
330 }
@@ -351,7 +352,7 @@ func TestLsNonUnixfs(t *testing.T) {
352 t.Error(err)
353 }
354
354 - links, err := api.Unixfs().Ls(ctx, api.ParseCid(nd.Cid()))
355 + links, err := api.Unixfs().Ls(ctx, api.IpfsPath(nd.Cid()))
356 if err != nil {
357 t.Error(err)
358 }
core/corehttp/gateway_handler.go
+2 -2
@@ -159,7 +159,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
159 ipnsHostname = true
160 }
161
162 - parsedPath, err := i.api.ParsePath(ctx, urlPath)
162 + parsedPath, err := i.api.ParsePath(urlPath)
163 if err != nil {
164 webError(w, "invalid ipfs path", err, http.StatusBadRequest)
165 return
@@ -287,7 +287,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
287 return
288 }
289
290 - dr, err := i.api.Unixfs().Cat(ctx, i.api.ParseCid(ixnd.Cid()))
290 + dr, err := i.api.Unixfs().Cat(ctx, i.api.IpfsPath(ixnd.Cid()))
291 if err != nil {
292 internalWebError(w, err)
293 return