diff --git a/core/commands/get.go b/core/commands/get.go index d8108a3f3..39fced25c 100644 --- a/core/commands/get.go +++ b/core/commands/get.go @@ -65,14 +65,8 @@ may also specify the level of compression by specifying '-l=<1-9>'. return } - // Validate path string - p, err := path.ParsePath(req.Arguments()[0]) - if err != nil { - res.SetError(fmt.Errorf("failed to validate path: %v", err), cmds.ErrNormal) - return - } + p := path.Path(req.Arguments()[0]) var reader io.Reader - if archive, _, _ := req.Option("archive").Bool(); !archive && cmplvl != gzip.NoCompression { // only use this when the flag is '-C' without '-a' reader, err = getZip(req.Context().Context, node, p, cmplvl) @@ -169,18 +163,18 @@ func getCompressOptions(req cmds.Request) (int, error) { return gzip.NoCompression, nil } -func get(ctx context.Context, node *core.IpfsNode, pathToResolve path.Path, compression int) (io.Reader, error) { - dagnode, err := core.Resolve(ctx, node, pathToResolve) +func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) { + dagnode, err := core.Resolve(ctx, node, p) if err != nil { return nil, err } - return utar.NewReader(pathToResolve, node.DAG, dagnode, compression) + return utar.NewReader(p, node.DAG, dagnode, compression) } // getZip is equivalent to `ipfs getdag $hash | gzip` -func getZip(ctx context.Context, node *core.IpfsNode, pathToResolve path.Path, compression int) (io.Reader, error) { - dagnode, err := core.Resolve(ctx, node, pathToResolve) +func getZip(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) { + dagnode, err := core.Resolve(ctx, node, p) if err != nil { return nil, err } diff --git a/core/commands/publish.go b/core/commands/publish.go index 5f832eb6b..b89214e17 100644 --- a/core/commands/publish.go +++ b/core/commands/publish.go @@ -88,15 +88,9 @@ Publish an to another public key (not implemented): pstr = args[0] } - p, err := path.ParsePath(pstr) - if err != nil { - res.SetError(fmt.Errorf("failed to validate path: %v", err), cmds.ErrNormal) - return - } - // TODO n.Keychain.Get(name).PrivKey // TODO(cryptix): is req.Context().Context a child of n.Context()? - output, err := publish(req.Context().Context, n, n.PrivateKey, p) + output, err := publish(req.Context().Context, n, n.PrivateKey, path.Path(pstr)) if err != nil { res.SetError(err, cmds.ErrNormal) return diff --git a/path/path.go b/path/path.go index f98f2cd13..6fe75adbe 100644 --- a/path/path.go +++ b/path/path.go @@ -61,12 +61,15 @@ func ParsePath(txt string) (Path, error) { } if parts[0] != "" { - return "", ErrBadPath + if _, err := ParseKeyToPath(parts[0]); err != nil { + return "", ErrBadPath + } + // The case when the path starts with hash without a protocol prefix + return Path("/ipfs/" + txt), nil } if parts[1] == "ipfs" { - _, err := ParseKeyToPath(parts[2]) - if err != nil { + if _, err := ParseKeyToPath(parts[2]); err != nil { return "", err } } else if parts[1] != "ipns" { @@ -77,13 +80,16 @@ func ParsePath(txt string) (Path, error) { } func ParseKeyToPath(txt string) (Path, error) { + if txt == "" { + return "", ErrNoComponents + } + chk := b58.Decode(txt) if len(chk) == 0 { return "", errors.New("not a key") } - _, err := mh.Cast(chk) - if err != nil { + if _, err := mh.Cast(chk); err != nil { return "", err } return FromKey(key.Key(chk)), nil diff --git a/path/resolver.go b/path/resolver.go index ce1f64a7e..288075000 100644 --- a/path/resolver.go +++ b/path/resolver.go @@ -65,6 +65,11 @@ func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) { // ResolvePath fetches the node for given path. It returns the last item // returned by ResolvePathComponents. func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (*merkledag.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