Skip to content

Commit

Permalink
Fix "ipfs ls" so it works correctly with raw leaves.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Jan 4, 2017
1 parent 3526c26 commit 6cc0903
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
49 changes: 19 additions & 30 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"io"
"text/tabwriter"

blockservice "github.com/ipfs/go-ipfs/blockservice"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
offline "github.com/ipfs/go-ipfs/exchange/offline"
merkledag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
unixfs "github.com/ipfs/go-ipfs/unixfs"
Expand Down Expand Up @@ -71,6 +73,13 @@ The JSON output contains type information.
return
}

dserv := nd.DAG
if !resolve {
offlineexch := offline.Exchange(nd.Blockstore)
bserv := blockservice.New(nd.Blockstore, offlineexch)
dserv = merkledag.NewDAGService(bserv)
}

paths := req.Arguments()

var dagnodes []node.Node
Expand Down Expand Up @@ -101,39 +110,19 @@ The JSON output contains type information.
Links: make([]LsLink, len(dagnode.Links())),
}
for j, link := range dagnode.Links() {
var linkNode *merkledag.ProtoNode
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)
if !ok {
res.SetError(merkledag.ErrNotProtobuf, cmds.ErrNormal)
return
}

linkNode = pbnd
linkNode, err := link.GetNode(req.Context(), dserv)
if err == merkledag.ErrNotFound && !resolve {
// not an error
linkNode = nil
} else if err != nil {
res.SetError(err, cmds.ErrNormal)
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 {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
13 changes: 13 additions & 0 deletions test/sharness/t0045-ls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
test_ls_cmd
test_ls_cmd_raw_leaves

# should work online
test_launch_ipfs_daemon
test_ls_cmd
test_ls_cmd_raw_leaves
test_kill_ipfs_daemon

test_done

0 comments on commit 6cc0903

Please sign in to comment.