提交 6cc0903d 作者: Kevin Atkinson

Fix "ipfs ls" so it works correctly with raw leaves.

License: MIT
Signed-off-by: 's avatarKevin Atkinson <k@kevina.org>
上级 3526c26f
...@@ -6,8 +6,10 @@ import ( ...@@ -6,8 +6,10 @@ import (
"io" "io"
"text/tabwriter" "text/tabwriter"
blockservice "github.com/ipfs/go-ipfs/blockservice"
cmds "github.com/ipfs/go-ipfs/commands" cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core" core "github.com/ipfs/go-ipfs/core"
offline "github.com/ipfs/go-ipfs/exchange/offline"
merkledag "github.com/ipfs/go-ipfs/merkledag" merkledag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path" path "github.com/ipfs/go-ipfs/path"
unixfs "github.com/ipfs/go-ipfs/unixfs" unixfs "github.com/ipfs/go-ipfs/unixfs"
...@@ -71,6 +73,13 @@ The JSON output contains type information. ...@@ -71,6 +73,13 @@ The JSON output contains type information.
return return
} }
dserv := nd.DAG
if !resolve {
offlineexch := offline.Exchange(nd.Blockstore)
bserv := blockservice.New(nd.Blockstore, offlineexch)
dserv = merkledag.NewDAGService(bserv)
}
paths := req.Arguments() paths := req.Arguments()
var dagnodes []node.Node var dagnodes []node.Node
...@@ -101,39 +110,19 @@ The JSON output contains type information. ...@@ -101,39 +110,19 @@ The JSON output contains type information.
Links: make([]LsLink, len(dagnode.Links())), Links: make([]LsLink, len(dagnode.Links())),
} }
for j, link := range dagnode.Links() { for j, link := range dagnode.Links() {
var linkNode *merkledag.ProtoNode
t := unixfspb.Data_DataType(-1) t := unixfspb.Data_DataType(-1)
linkKey := link.Cid
if ok, err := nd.Blockstore.Has(linkKey); ok && err == nil {
b, err := nd.Blockstore.Get(linkKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
linkNode, err = merkledag.DecodeProtobuf(b.RawData())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}
if linkNode == nil && resolve {
nd, err := link.GetNode(req.Context(), nd.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
pbnd, ok := nd.(*merkledag.ProtoNode) linkNode, err := link.GetNode(req.Context(), dserv)
if !ok { if err == merkledag.ErrNotFound && !resolve {
res.SetError(merkledag.ErrNotProtobuf, cmds.ErrNormal) // not an error
return linkNode = nil
} } else if err != nil {
res.SetError(err, cmds.ErrNormal)
linkNode = pbnd return
} }
if linkNode != nil {
d, err := unixfs.FromBytes(linkNode.Data()) if pn, ok := linkNode.(*merkledag.ProtoNode); ok {
d, err := unixfs.FromBytes(pn.Data())
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
......
...@@ -90,12 +90,25 @@ test_ls_cmd() { ...@@ -90,12 +90,25 @@ test_ls_cmd() {
' '
} }
test_ls_cmd_raw_leaves() {
test_expect_success "'ipfs add -r --raw-leaves' then 'ipfs ls' works as expected" '
mkdir -p somedir &&
echo bar > somedir/foo &&
ipfs add --raw-leaves -r somedir/ > /dev/null &&
ipfs ls QmThNTdtKaVoCVrYmM5EBS6U3S5vfKFue2TxbxxAxRcKKE > ls-actual
echo "zb2rhf6GzX4ckKZtjy8yy8iyq1KttCrRyqDedD6xubhY3sw2F 4 foo" > ls-expect
test_cmp ls-actual ls-expect
'
}
# should work offline # should work offline
test_ls_cmd test_ls_cmd
test_ls_cmd_raw_leaves
# should work online # should work online
test_launch_ipfs_daemon test_launch_ipfs_daemon
test_ls_cmd test_ls_cmd
test_ls_cmd_raw_leaves
test_kill_ipfs_daemon test_kill_ipfs_daemon
test_done test_done
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论