Fix "ipfs ls" so it works correctly with raw leaves.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Jan 2, 2017 at 22:58 UTC
6cc0903d251b44a672b73c0eb5170f4cf60c1f1d
2 files changed
+32
-30
core/commands/ls.go
+19
-30
@@ -6,8 +6,10 @@ import (
6
"io"
7
"text/tabwriter"
8
9
+ blockservice "github.com/ipfs/go-ipfs/blockservice"
10
cmds "github.com/ipfs/go-ipfs/commands"
11
core "github.com/ipfs/go-ipfs/core"
12
+ offline "github.com/ipfs/go-ipfs/exchange/offline"
13
merkledag "github.com/ipfs/go-ipfs/merkledag"
14
path "github.com/ipfs/go-ipfs/path"
15
unixfs "github.com/ipfs/go-ipfs/unixfs"
@@ -71,6 +73,13 @@ The JSON output contains type information.
73
return
74
}
75
76
+ dserv := nd.DAG
77
+ if !resolve {
78
+ offlineexch := offline.Exchange(nd.Blockstore)
79
+ bserv := blockservice.New(nd.Blockstore, offlineexch)
80
+ dserv = merkledag.NewDAGService(bserv)
81
+ }
82
+
83
paths := req.Arguments()
84
85
var dagnodes []node.Node
@@ -101,39 +110,19 @@ The JSON output contains type information.
110
Links: make([]LsLink, len(dagnode.Links())),
111
}
112
for j, link := range dagnode.Links() {
104
- var linkNode *merkledag.ProtoNode
113
t := unixfspb.Data_DataType(-1)
106
- linkKey := link.Cid
107
- if ok, err := nd.Blockstore.Has(linkKey); ok && err == nil {
108
- b, err := nd.Blockstore.Get(linkKey)
109
- if err != nil {
110
- res.SetError(err, cmds.ErrNormal)
111
- return
112
- }
113
- linkNode, err = merkledag.DecodeProtobuf(b.RawData())
114
- if err != nil {
115
- res.SetError(err, cmds.ErrNormal)
116
- return
117
- }
118
- }
119
-
120
- if linkNode == nil && resolve {
121
- nd, err := link.GetNode(req.Context(), nd.DAG)
122
- if err != nil {
123
- res.SetError(err, cmds.ErrNormal)
124
- return
125
- }
114
127
- pbnd, ok := nd.(*merkledag.ProtoNode)
128
- if !ok {
129
- res.SetError(merkledag.ErrNotProtobuf, cmds.ErrNormal)
130
- return
131
- }
132
-
133
- linkNode = pbnd
115
+ linkNode, err := link.GetNode(req.Context(), dserv)
116
+ if err == merkledag.ErrNotFound && !resolve {
117
+ // not an error
118
+ linkNode = nil
119
+ } else if err != nil {
120
+ res.SetError(err, cmds.ErrNormal)
121
+ return
122
}
135
- if linkNode != nil {
136
- d, err := unixfs.FromBytes(linkNode.Data())
123
+
124
+ if pn, ok := linkNode.(*merkledag.ProtoNode); ok {
125
+ d, err := unixfs.FromBytes(pn.Data())
126
if err != nil {
127
res.SetError(err, cmds.ErrNormal)
128
return
test/sharness/t0045-ls.sh
+13
@@ -90,12 +90,25 @@ test_ls_cmd() {
90
'
91
}
92
93
+test_ls_cmd_raw_leaves() {
94
+ test_expect_success "'ipfs add -r --raw-leaves' then 'ipfs ls' works as expected" '
95
+ mkdir -p somedir &&
96
+ echo bar > somedir/foo &&
97
+ ipfs add --raw-leaves -r somedir/ > /dev/null &&
98
+ ipfs ls QmThNTdtKaVoCVrYmM5EBS6U3S5vfKFue2TxbxxAxRcKKE > ls-actual
99
+ echo "zb2rhf6GzX4ckKZtjy8yy8iyq1KttCrRyqDedD6xubhY3sw2F 4 foo" > ls-expect
100
+ test_cmp ls-actual ls-expect
101
+ '
102
+}
103
+
104
# should work offline
105
test_ls_cmd
106
+test_ls_cmd_raw_leaves
107
108
# should work online
109
test_launch_ipfs_daemon
110
test_ls_cmd
111
+test_ls_cmd_raw_leaves
112
test_kill_ipfs_daemon
113
114
test_done