Skip to content

Commit

Permalink
fix coreapi unixfs resolving
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed May 1, 2017
1 parent 0597a04 commit ac405cc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
8 changes: 7 additions & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
core "github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
ipfspath "github.com/ipfs/go-ipfs/path"
uio "github.com/ipfs/go-ipfs/unixfs/io"

cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
)
Expand Down Expand Up @@ -42,8 +43,13 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreifac
return p, nil
}

r := &ipfspath.Resolver{
DAG: api.node.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

p2 := ipfspath.FromString(p.String())
node, err := core.Resolve(ctx, api.node.Namesys, api.node.Resolver, p2)
node, err := core.Resolve(ctx, api.node.Namesys, r, p2)
if err == core.ErrNoNamesys {
return nil, coreiface.ErrOffline
} else if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
uio "github.com/ipfs/go-ipfs/unixfs/io"

cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
)

type UnixfsAPI CoreAPI
Expand Down Expand Up @@ -46,9 +47,23 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*coreiface.Li
return nil, err
}

l := dagnode.Links()
links := make([]*coreiface.Link, len(l))
for i, l := range l {
var ndlinks []*node.Link
dir, err := uio.NewDirectoryFromNode(api.node.DAG, dagnode)
switch err {
case nil:
l, err := dir.Links(ctx)
if err != nil {
return nil, err
}
ndlinks = l
case uio.ErrNotADir:
ndlinks = dagnode.Links()
default:
return nil, err
}

links := make([]*coreiface.Link, len(ndlinks))
for i, l := range ndlinks {
links[i] = &coreiface.Link{l.Name, l.Size, l.Cid}
}
return links, nil
Expand Down
8 changes: 7 additions & 1 deletion core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
config "github.com/ipfs/go-ipfs/repo/config"
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
unixfs "github.com/ipfs/go-ipfs/unixfs"
cbor "gx/ipfs/QmNrbCt8j9DT5W9Pmjy2SdudT9k8GpaDr4sRuFix3BXhgR/go-ipld-cbor"
)

// `echo -n 'hello, world!' | ipfs add`
Expand Down Expand Up @@ -276,7 +277,12 @@ func TestLsNonUnixfs(t *testing.T) {
t.Error(err)
}

c, err := node.DAG.Add(new(mdag.ProtoNode))
nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"})
if err != nil {
t.Fatal(err)
}

c, err := node.DAG.Add(nd)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit ac405cc

Please sign in to comment.