coreapi: Update path error handling
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Mar 25, 2019 at 17:04 UTC
2e77df04ca89d738c3090b093de5b2251161663b
18 files changed
+36
-162
core/commands/block.go
+3
-21
@@ -65,12 +65,7 @@ on raw IPFS blocks. It outputs the following to stdout:
65
return err
66
}
67
68
- p, err := coreiface.ParsePath(req.Arguments[0])
69
- if err != nil {
70
- return err
71
- }
72
-
73
- b, err := api.Block().Stat(req.Context, p)
68
+ b, err := api.Block().Stat(req.Context, coreiface.ParsePath(req.Arguments[0]))
69
if err != nil {
70
return err
71
}
@@ -107,12 +102,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
102
return err
103
}
104
110
- p, err := coreiface.ParsePath(req.Arguments[0])
111
- if err != nil {
112
- return err
113
- }
114
-
115
- r, err := api.Block().Get(req.Context, p)
105
+ r, err := api.Block().Get(req.Context, coreiface.ParsePath(req.Arguments[0]))
106
if err != nil {
107
return err
108
}
@@ -234,15 +224,7 @@ It takes a list of base58 encoded multihashes to remove.
224
225
// TODO: use batching coreapi when done
226
for _, b := range req.Arguments {
237
- p, err := coreiface.ParsePath(b)
238
- if err != nil {
239
- return err
240
- }
241
-
242
- rp, err := api.ResolvePath(req.Context, p)
243
- if err != nil {
244
- return err
245
- }
227
+ rp, err := api.ResolvePath(req.Context, coreiface.ParsePath(b))
228
229
err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
230
if err != nil {
core/commands/cat.go
+1
-6
@@ -118,12 +118,7 @@ func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, m
118
return nil, 0, nil
119
}
120
for _, p := range paths {
121
- fpath, err := iface.ParsePath(p)
122
- if err != nil {
123
- return nil, 0, err
124
- }
125
-
126
- f, err := api.Unixfs().Get(ctx, fpath)
121
+ f, err := api.Unixfs().Get(ctx, iface.ParsePath(p))
122
if err != nil {
123
return nil, 0, err
124
}
core/commands/dag/dag.go
+2
-12
@@ -160,12 +160,7 @@ format.
160
return err
161
}
162
163
- p, err := iface.ParsePath(req.Arguments[0])
164
- if err != nil {
165
- return err
166
- }
167
-
168
- rp, err := api.ResolvePath(req.Context, p)
163
+ rp, err := api.ResolvePath(req.Context, iface.ParsePath(req.Arguments[0]))
164
if err != nil {
165
return err
166
}
@@ -205,12 +200,7 @@ var DagResolveCmd = &cmds.Command{
200
return err
201
}
202
208
- p, err := iface.ParsePath(req.Arguments[0])
209
- if err != nil {
210
- return err
211
- }
212
-
213
- rp, err := api.ResolvePath(req.Context, p)
203
+ rp, err := api.ResolvePath(req.Context, iface.ParsePath(req.Arguments[0]))
204
if err != nil {
205
return err
206
}
core/commands/files.go
+1
-6
@@ -363,12 +363,7 @@ var filesCpCmd = &cmds.Command{
363
func getNodeFromPath(ctx context.Context, node *core.IpfsNode, api iface.CoreAPI, p string) (ipld.Node, error) {
364
switch {
365
case strings.HasPrefix(p, "/ipfs/"):
366
- np, err := iface.ParsePath(p)
367
- if err != nil {
368
- return nil, err
369
- }
370
-
371
- return api.ResolveNode(ctx, np)
366
+ return api.ResolveNode(ctx, iface.ParsePath(p))
367
default:
368
fsn, err := mfs.Lookup(node.FilesRoot, p)
369
if err != nil {
core/commands/get.go
+1
-4
@@ -71,10 +71,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
71
return err
72
}
73
74
- p, err := iface.ParsePath(req.Arguments[0])
75
- if err != nil {
76
- return err
77
- }
74
+ p := iface.ParsePath(req.Arguments[0])
75
76
file, err := api.Unixfs().Get(req.Context, p)
77
if err != nil {
core/commands/ls.go
+1
-6
@@ -131,12 +131,7 @@ The JSON output contains type information.
131
}
132
133
for i, fpath := range paths {
134
- p, err := iface.ParsePath(fpath)
135
- if err != nil {
136
- return err
137
- }
138
-
139
- results, err := api.Unixfs().Ls(req.Context, p,
134
+ results, err := api.Unixfs().Ls(req.Context, iface.ParsePath(fpath),
135
options.Unixfs.ResolveChildren(resolveSize || resolveType))
136
if err != nil {
137
return err
core/commands/name/publish.go
+1
-4
@@ -112,10 +112,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
112
opts = append(opts, options.Name.TTL(d))
113
}
114
115
- p, err := iface.ParsePath(req.Arguments[0])
116
- if err != nil {
117
- return err
118
- }
115
+ p := iface.ParsePath(req.Arguments[0])
116
117
if verifyExists, _ := req.Options[resolveOptionName].(bool); verifyExists {
118
_, err := api.ResolveNode(req.Context, p)
core/commands/object/diff.go
+2
-12
@@ -60,18 +60,8 @@ Example:
60
return err
61
}
62
63
- a := req.Arguments[0]
64
- b := req.Arguments[1]
65
-
66
- pa, err := coreiface.ParsePath(a)
67
- if err != nil {
68
- return err
69
- }
70
-
71
- pb, err := coreiface.ParsePath(b)
72
- if err != nil {
73
- return err
74
- }
63
+ pa := coreiface.ParsePath(req.Arguments[0])
64
+ pb := coreiface.ParsePath(req.Arguments[1])
65
66
changes, err := api.Object().Diff(req.Context, pa, pb)
67
if err != nil {
core/commands/object/object.go
+4
-18
@@ -91,10 +91,7 @@ is the raw data of the object.
91
return err
92
}
93
94
- path, err := coreiface.ParsePath(req.Arguments[0])
95
- if err != nil {
96
- return err
97
- }
94
+ path := coreiface.ParsePath(req.Arguments[0])
95
96
data, err := api.Object().Data(req.Context, path)
97
if err != nil {
@@ -133,10 +130,7 @@ multihash.
130
return err
131
}
132
136
- path, err := coreiface.ParsePath(req.Arguments[0])
137
- if err != nil {
138
- return err
139
- }
133
+ path := coreiface.ParsePath(req.Arguments[0])
134
135
rp, err := api.ResolvePath(req.Context, path)
136
if err != nil {
@@ -228,10 +222,7 @@ Supported values are:
222
return err
223
}
224
231
- path, err := coreiface.ParsePath(req.Arguments[0])
232
- if err != nil {
233
- return err
234
- }
225
+ path := coreiface.ParsePath(req.Arguments[0])
226
227
datafieldenc, _ := req.Options[encodingOptionName].(string)
228
if err != nil {
@@ -323,12 +314,7 @@ var ObjectStatCmd = &cmds.Command{
314
return err
315
}
316
326
- path, err := coreiface.ParsePath(req.Arguments[0])
327
- if err != nil {
328
- return err
329
- }
330
-
331
- ns, err := api.Object().Stat(req.Context, path)
317
+ ns, err := api.Object().Stat(req.Context, coreiface.ParsePath(req.Arguments[0]))
318
if err != nil {
319
return err
320
}
core/commands/object/patch.go
+5
-22
@@ -55,10 +55,7 @@ the limit will not be respected by the network.
55
return err
56
}
57
58
- root, err := coreiface.ParsePath(req.Arguments[0])
59
- if err != nil {
60
- return err
61
- }
58
+ root := coreiface.ParsePath(req.Arguments[0])
59
60
file, err := cmdenv.GetFileArg(req.Files.Entries())
61
if err != nil {
@@ -102,10 +99,7 @@ Example:
99
return err
100
}
101
105
- root, err := coreiface.ParsePath(req.Arguments[0])
106
- if err != nil {
107
- return err
108
- }
102
+ root := coreiface.ParsePath(req.Arguments[0])
103
104
file, err := cmdenv.GetFileArg(req.Files.Entries())
105
if err != nil {
@@ -145,10 +139,7 @@ Remove a Merkle-link from the given object and return the hash of the result.
139
return err
140
}
141
148
- root, err := coreiface.ParsePath(req.Arguments[0])
149
- if err != nil {
150
- return err
151
- }
142
+ root := coreiface.ParsePath(req.Arguments[0])
143
144
name := req.Arguments[1]
145
p, err := api.Object().RmLink(req.Context, root, name)
@@ -201,17 +192,9 @@ to a file containing 'bar', and returns the hash of the new object.
192
return err
193
}
194
204
- root, err := coreiface.ParsePath(req.Arguments[0])
205
- if err != nil {
206
- return err
207
- }
208
-
195
+ root := coreiface.ParsePath(req.Arguments[0])
196
name := req.Arguments[1]
210
-
211
- child, err := coreiface.ParsePath(req.Arguments[2])
212
- if err != nil {
213
- return err
214
- }
197
+ child := coreiface.ParsePath(req.Arguments[2])
198
199
create, _ := req.Options[createOptionName].(bool)
200
if err != nil {
core/commands/pin.go
+5
-27
@@ -183,12 +183,7 @@ var addPinCmd = &cmds.Command{
183
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
184
added := make([]string, len(paths))
185
for i, b := range paths {
186
- p, err := coreiface.ParsePath(b)
187
- if err != nil {
188
- return nil, err
189
- }
190
-
191
- rp, err := api.ResolvePath(ctx, p)
186
+ rp, err := api.ResolvePath(ctx, coreiface.ParsePath(b))
187
if err != nil {
188
return nil, err
189
}
@@ -238,12 +233,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
233
234
pins := make([]string, 0, len(req.Arguments))
235
for _, b := range req.Arguments {
241
- p, err := coreiface.ParsePath(b)
242
- if err != nil {
243
- return err
244
- }
245
-
246
- rp, err := api.ResolvePath(req.Context, p)
236
+ rp, err := api.ResolvePath(req.Context, coreiface.ParsePath(b))
237
if err != nil {
238
return err
239
}
@@ -417,15 +407,8 @@ new pin and removing the old one.
407
408
unpin, _ := req.Options[pinUnpinOptionName].(bool)
409
420
- from, err := coreiface.ParsePath(req.Arguments[0])
421
- if err != nil {
422
- return err
423
- }
424
-
425
- to, err := coreiface.ParsePath(req.Arguments[1])
426
- if err != nil {
427
- return err
428
- }
410
+ from := coreiface.ParsePath(req.Arguments[0])
411
+ to := coreiface.ParsePath(req.Arguments[1])
412
413
err = api.Pin().Update(req.Context, from, to, options.Pin.Unpin(unpin))
414
if err != nil {
@@ -514,12 +497,7 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN
497
keys := make(map[cid.Cid]RefKeyObject)
498
499
for _, p := range args {
517
- pth, err := coreiface.ParsePath(p)
518
- if err != nil {
519
- return nil, err
520
- }
521
-
522
- c, err := api.ResolvePath(ctx, pth)
500
+ c, err := api.ResolvePath(ctx, coreiface.ParsePath(p))
501
if err != nil {
502
return nil, err
503
}
core/commands/resolve.go
+1
-6
@@ -130,12 +130,7 @@ Resolve the value of an IPFS DAG path:
130
}
131
132
// else, ipfs path or ipns with recursive flag
133
- p, err := coreiface.ParsePath(name)
134
- if err != nil {
135
- return err
136
- }
137
-
138
- rp, err := api.ResolvePath(req.Context, p)
133
+ rp, err := api.ResolvePath(req.Context, coreiface.ParsePath(name))
134
if err != nil {
135
return err
136
}
core/commands/unixfs/ls.go
+1
-6
@@ -96,12 +96,7 @@ possible, please use 'ipfs ls' instead.
96
for _, p := range paths {
97
ctx := req.Context
98
99
- fpath, err := iface.ParsePath(p)
100
- if err != nil {
101
- return err
102
- }
103
-
104
- merkleNode, err := api.ResolveNode(ctx, fpath)
99
+ merkleNode, err := api.ResolveNode(ctx, iface.ParsePath(p))
100
if err != nil {
101
return err
102
}
core/coreapi/key.go
+1
-6
@@ -28,12 +28,7 @@ func (k *key) Name() string {
28
29
// Path returns the path of the key.
30
func (k *key) Path() coreiface.Path {
31
- path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerID.Pretty()}))
32
- if err != nil {
33
- panic("error parsing path: " + err.Error())
34
- }
35
-
36
- return path
31
+ return coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerID.Pretty()}))
32
}
33
34
// ID returns key PeerID
core/coreapi/name.go
+1
-3
@@ -106,10 +106,8 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
106
go func() {
107
defer close(out)
108
for res := range resolver.ResolveAsync(ctx, name, options.ResolveOpts...) {
109
- p, _ := coreiface.ParsePath(res.Path.String())
110
-
109
select {
112
- case out <- coreiface.IpnsResult{Path: p, Err: res.Err}:
110
+ case out <- coreiface.IpnsResult{Path: coreiface.ParsePath(res.Path.String()), Err: res.Err}:
111
case <-ctx.Done():
112
return
113
}
core/coreapi/path.go
+3
@@ -36,6 +36,9 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreifac
36
if _, ok := p.(coreiface.ResolvedPath); ok {
37
return p.(coreiface.ResolvedPath), nil
38
}
39
+ if err := p.IsValid(); err != nil {
40
+ return nil, err
41
+ }
42
43
ipath := ipfspath.Path(p.String())
44
ipath, err := core.ResolveIPNS(ctx, api.namesys, ipath)
core/corehttp/gateway_handler.go
+2
-2
@@ -147,8 +147,8 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
147
ipnsHostname = true
148
}
149
150
- parsedPath, err := coreiface.ParsePath(urlPath)
151
- if err != nil {
150
+ parsedPath := coreiface.ParsePath(urlPath)
151
+ if err := parsedPath.IsValid(); err != nil {
152
webError(w, "invalid ipfs path", err, http.StatusBadRequest)
153
return
154
}
fuse/readonly/ipfs_test.go
+1
-1
@@ -184,7 +184,7 @@ func TestIpfsStressRead(t *testing.T) {
184
defer wg.Done()
185
186
for i := 0; i < 2000; i++ {
187
- item, _ := iface.ParsePath(paths[rand.Intn(len(paths))])
187
+ item := iface.ParsePath(paths[rand.Intn(len(paths))])
188
189
relpath := strings.Replace(item.String(), item.Namespace(), "", 1)
190
fname := path.Join(mnt.Dir, relpath)