From 2e77df04ca89d738c3090b093de5b2251161663b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 25 Mar 2019 17:04:59 +0100 Subject: [PATCH] coreapi: Update path error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/commands/block.go | 24 +++--------------------- core/commands/cat.go | 7 +------ core/commands/dag/dag.go | 14 ++------------ core/commands/files.go | 7 +------ core/commands/get.go | 5 +---- core/commands/ls.go | 7 +------ core/commands/name/publish.go | 5 +---- core/commands/object/diff.go | 14 ++------------ core/commands/object/object.go | 22 ++++------------------ core/commands/object/patch.go | 27 +++++---------------------- core/commands/pin.go | 32 +++++--------------------------- core/commands/resolve.go | 7 +------ core/commands/unixfs/ls.go | 7 +------ core/coreapi/key.go | 7 +------ core/coreapi/name.go | 4 +--- core/coreapi/path.go | 3 +++ core/corehttp/gateway_handler.go | 4 ++-- fuse/readonly/ipfs_test.go | 2 +- 18 files changed, 36 insertions(+), 162 deletions(-) diff --git a/core/commands/block.go b/core/commands/block.go index 6e6b24afc..a5ba7a3e3 100644 --- a/core/commands/block.go +++ b/core/commands/block.go @@ -65,12 +65,7 @@ on raw IPFS blocks. It outputs the following to stdout: return err } - p, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - - b, err := api.Block().Stat(req.Context, p) + b, err := api.Block().Stat(req.Context, coreiface.ParsePath(req.Arguments[0])) if err != nil { return err } @@ -107,12 +102,7 @@ It outputs to stdout, and is a base58 encoded multihash. return err } - p, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - - r, err := api.Block().Get(req.Context, p) + r, err := api.Block().Get(req.Context, coreiface.ParsePath(req.Arguments[0])) if err != nil { return err } @@ -234,15 +224,7 @@ It takes a list of base58 encoded multihashes to remove. // TODO: use batching coreapi when done for _, b := range req.Arguments { - p, err := coreiface.ParsePath(b) - if err != nil { - return err - } - - rp, err := api.ResolvePath(req.Context, p) - if err != nil { - return err - } + rp, err := api.ResolvePath(req.Context, coreiface.ParsePath(b)) err = api.Block().Rm(req.Context, rp, options.Block.Force(force)) if err != nil { diff --git a/core/commands/cat.go b/core/commands/cat.go index 3fe5ee7bf..757ecd9fb 100644 --- a/core/commands/cat.go +++ b/core/commands/cat.go @@ -118,12 +118,7 @@ func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, m return nil, 0, nil } for _, p := range paths { - fpath, err := iface.ParsePath(p) - if err != nil { - return nil, 0, err - } - - f, err := api.Unixfs().Get(ctx, fpath) + f, err := api.Unixfs().Get(ctx, iface.ParsePath(p)) if err != nil { return nil, 0, err } diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index e6958f8ce..7619c7fa0 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -160,12 +160,7 @@ format. return err } - p, err := iface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - - rp, err := api.ResolvePath(req.Context, p) + rp, err := api.ResolvePath(req.Context, iface.ParsePath(req.Arguments[0])) if err != nil { return err } @@ -205,12 +200,7 @@ var DagResolveCmd = &cmds.Command{ return err } - p, err := iface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - - rp, err := api.ResolvePath(req.Context, p) + rp, err := api.ResolvePath(req.Context, iface.ParsePath(req.Arguments[0])) if err != nil { return err } diff --git a/core/commands/files.go b/core/commands/files.go index 928434d72..f2fb8e65a 100644 --- a/core/commands/files.go +++ b/core/commands/files.go @@ -363,12 +363,7 @@ var filesCpCmd = &cmds.Command{ func getNodeFromPath(ctx context.Context, node *core.IpfsNode, api iface.CoreAPI, p string) (ipld.Node, error) { switch { case strings.HasPrefix(p, "/ipfs/"): - np, err := iface.ParsePath(p) - if err != nil { - return nil, err - } - - return api.ResolveNode(ctx, np) + return api.ResolveNode(ctx, iface.ParsePath(p)) default: fsn, err := mfs.Lookup(node.FilesRoot, p) if err != nil { diff --git a/core/commands/get.go b/core/commands/get.go index b73aa5cbd..7aed9d3bb 100644 --- a/core/commands/get.go +++ b/core/commands/get.go @@ -71,10 +71,7 @@ may also specify the level of compression by specifying '-l=<1-9>'. return err } - p, err := iface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + p := iface.ParsePath(req.Arguments[0]) file, err := api.Unixfs().Get(req.Context, p) if err != nil { diff --git a/core/commands/ls.go b/core/commands/ls.go index 0289d69fd..8890a91da 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -131,12 +131,7 @@ The JSON output contains type information. } for i, fpath := range paths { - p, err := iface.ParsePath(fpath) - if err != nil { - return err - } - - results, err := api.Unixfs().Ls(req.Context, p, + results, err := api.Unixfs().Ls(req.Context, iface.ParsePath(fpath), options.Unixfs.ResolveChildren(resolveSize || resolveType)) if err != nil { return err diff --git a/core/commands/name/publish.go b/core/commands/name/publish.go index 74bb3bd83..81322f7e4 100644 --- a/core/commands/name/publish.go +++ b/core/commands/name/publish.go @@ -112,10 +112,7 @@ Alternatively, publish an using a valid PeerID (as listed by opts = append(opts, options.Name.TTL(d)) } - p, err := iface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + p := iface.ParsePath(req.Arguments[0]) if verifyExists, _ := req.Options[resolveOptionName].(bool); verifyExists { _, err := api.ResolveNode(req.Context, p) diff --git a/core/commands/object/diff.go b/core/commands/object/diff.go index 4e02110d7..96a06acdb 100644 --- a/core/commands/object/diff.go +++ b/core/commands/object/diff.go @@ -60,18 +60,8 @@ Example: return err } - a := req.Arguments[0] - b := req.Arguments[1] - - pa, err := coreiface.ParsePath(a) - if err != nil { - return err - } - - pb, err := coreiface.ParsePath(b) - if err != nil { - return err - } + pa := coreiface.ParsePath(req.Arguments[0]) + pb := coreiface.ParsePath(req.Arguments[1]) changes, err := api.Object().Diff(req.Context, pa, pb) if err != nil { diff --git a/core/commands/object/object.go b/core/commands/object/object.go index 76dc91291..3d3038b53 100644 --- a/core/commands/object/object.go +++ b/core/commands/object/object.go @@ -91,10 +91,7 @@ is the raw data of the object. return err } - path, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + path := coreiface.ParsePath(req.Arguments[0]) data, err := api.Object().Data(req.Context, path) if err != nil { @@ -133,10 +130,7 @@ multihash. return err } - path, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + path := coreiface.ParsePath(req.Arguments[0]) rp, err := api.ResolvePath(req.Context, path) if err != nil { @@ -228,10 +222,7 @@ Supported values are: return err } - path, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + path := coreiface.ParsePath(req.Arguments[0]) datafieldenc, _ := req.Options[encodingOptionName].(string) if err != nil { @@ -323,12 +314,7 @@ var ObjectStatCmd = &cmds.Command{ return err } - path, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - - ns, err := api.Object().Stat(req.Context, path) + ns, err := api.Object().Stat(req.Context, coreiface.ParsePath(req.Arguments[0])) if err != nil { return err } diff --git a/core/commands/object/patch.go b/core/commands/object/patch.go index 9f87a3798..639a27730 100644 --- a/core/commands/object/patch.go +++ b/core/commands/object/patch.go @@ -55,10 +55,7 @@ the limit will not be respected by the network. return err } - root, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + root := coreiface.ParsePath(req.Arguments[0]) file, err := cmdenv.GetFileArg(req.Files.Entries()) if err != nil { @@ -102,10 +99,7 @@ Example: return err } - root, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + root := coreiface.ParsePath(req.Arguments[0]) file, err := cmdenv.GetFileArg(req.Files.Entries()) if err != nil { @@ -145,10 +139,7 @@ Remove a Merkle-link from the given object and return the hash of the result. return err } - root, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } + root := coreiface.ParsePath(req.Arguments[0]) name := req.Arguments[1] 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. return err } - root, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - + root := coreiface.ParsePath(req.Arguments[0]) name := req.Arguments[1] - - child, err := coreiface.ParsePath(req.Arguments[2]) - if err != nil { - return err - } + child := coreiface.ParsePath(req.Arguments[2]) create, _ := req.Options[createOptionName].(bool) if err != nil { diff --git a/core/commands/pin.go b/core/commands/pin.go index 0379f1635..1f1a52f03 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -183,12 +183,7 @@ var addPinCmd = &cmds.Command{ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) { added := make([]string, len(paths)) for i, b := range paths { - p, err := coreiface.ParsePath(b) - if err != nil { - return nil, err - } - - rp, err := api.ResolvePath(ctx, p) + rp, err := api.ResolvePath(ctx, coreiface.ParsePath(b)) if err != nil { return nil, err } @@ -238,12 +233,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.) pins := make([]string, 0, len(req.Arguments)) for _, b := range req.Arguments { - p, err := coreiface.ParsePath(b) - if err != nil { - return err - } - - rp, err := api.ResolvePath(req.Context, p) + rp, err := api.ResolvePath(req.Context, coreiface.ParsePath(b)) if err != nil { return err } @@ -417,15 +407,8 @@ new pin and removing the old one. unpin, _ := req.Options[pinUnpinOptionName].(bool) - from, err := coreiface.ParsePath(req.Arguments[0]) - if err != nil { - return err - } - - to, err := coreiface.ParsePath(req.Arguments[1]) - if err != nil { - return err - } + from := coreiface.ParsePath(req.Arguments[0]) + to := coreiface.ParsePath(req.Arguments[1]) err = api.Pin().Update(req.Context, from, to, options.Pin.Unpin(unpin)) if err != nil { @@ -514,12 +497,7 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN keys := make(map[cid.Cid]RefKeyObject) for _, p := range args { - pth, err := coreiface.ParsePath(p) - if err != nil { - return nil, err - } - - c, err := api.ResolvePath(ctx, pth) + c, err := api.ResolvePath(ctx, coreiface.ParsePath(p)) if err != nil { return nil, err } diff --git a/core/commands/resolve.go b/core/commands/resolve.go index bc6f8850a..7d3639b97 100644 --- a/core/commands/resolve.go +++ b/core/commands/resolve.go @@ -130,12 +130,7 @@ Resolve the value of an IPFS DAG path: } // else, ipfs path or ipns with recursive flag - p, err := coreiface.ParsePath(name) - if err != nil { - return err - } - - rp, err := api.ResolvePath(req.Context, p) + rp, err := api.ResolvePath(req.Context, coreiface.ParsePath(name)) if err != nil { return err } diff --git a/core/commands/unixfs/ls.go b/core/commands/unixfs/ls.go index 76223d1fa..62d7833bc 100644 --- a/core/commands/unixfs/ls.go +++ b/core/commands/unixfs/ls.go @@ -96,12 +96,7 @@ possible, please use 'ipfs ls' instead. for _, p := range paths { ctx := req.Context - fpath, err := iface.ParsePath(p) - if err != nil { - return err - } - - merkleNode, err := api.ResolveNode(ctx, fpath) + merkleNode, err := api.ResolveNode(ctx, iface.ParsePath(p)) if err != nil { return err } diff --git a/core/coreapi/key.go b/core/coreapi/key.go index f87639d48..8eb5cf07e 100644 --- a/core/coreapi/key.go +++ b/core/coreapi/key.go @@ -28,12 +28,7 @@ func (k *key) Name() string { // Path returns the path of the key. func (k *key) Path() coreiface.Path { - path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerID.Pretty()})) - if err != nil { - panic("error parsing path: " + err.Error()) - } - - return path + return coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerID.Pretty()})) } // ID returns key PeerID diff --git a/core/coreapi/name.go b/core/coreapi/name.go index 0b368ec54..9f9d55d09 100644 --- a/core/coreapi/name.go +++ b/core/coreapi/name.go @@ -106,10 +106,8 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name go func() { defer close(out) for res := range resolver.ResolveAsync(ctx, name, options.ResolveOpts...) { - p, _ := coreiface.ParsePath(res.Path.String()) - select { - case out <- coreiface.IpnsResult{Path: p, Err: res.Err}: + case out <- coreiface.IpnsResult{Path: coreiface.ParsePath(res.Path.String()), Err: res.Err}: case <-ctx.Done(): return } diff --git a/core/coreapi/path.go b/core/coreapi/path.go index 6302ec67f..6fc2845de 100644 --- a/core/coreapi/path.go +++ b/core/coreapi/path.go @@ -36,6 +36,9 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreifac if _, ok := p.(coreiface.ResolvedPath); ok { return p.(coreiface.ResolvedPath), nil } + if err := p.IsValid(); err != nil { + return nil, err + } ipath := ipfspath.Path(p.String()) ipath, err := core.ResolveIPNS(ctx, api.namesys, ipath) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index a62aee4cd..638131d83 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -147,8 +147,8 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ipnsHostname = true } - parsedPath, err := coreiface.ParsePath(urlPath) - if err != nil { + parsedPath := coreiface.ParsePath(urlPath) + if err := parsedPath.IsValid(); err != nil { webError(w, "invalid ipfs path", err, http.StatusBadRequest) return } diff --git a/fuse/readonly/ipfs_test.go b/fuse/readonly/ipfs_test.go index 1383c169e..e10113d31 100644 --- a/fuse/readonly/ipfs_test.go +++ b/fuse/readonly/ipfs_test.go @@ -184,7 +184,7 @@ func TestIpfsStressRead(t *testing.T) { defer wg.Done() for i := 0; i < 2000; i++ { - item, _ := iface.ParsePath(paths[rand.Intn(len(paths))]) + item := iface.ParsePath(paths[rand.Intn(len(paths))]) relpath := strings.Replace(item.String(), item.Namespace(), "", 1) fname := path.Join(mnt.Dir, relpath)