@cryptotaxi247 / kubo / commits / 3b0e16e38

infer type from CID when possible in ls command

We don't need to fetch the linked node when it's a raw node, we already know it's a file. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Mar 28, 2018 at 19:53 UTC 3b0e16e38b0fe30d98539cb2ee450822fe7ea912
1 file changed +19 -13
core/commands/ls.go
+19 -13
@@ -18,6 +18,7 @@ import (
18 uio "github.com/ipfs/go-ipfs/unixfs/io"
19 unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"
20
21 + cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
22 "gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
23 ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
24 )
@@ -134,23 +135,28 @@ The JSON output contains type information.
135 for j, link := range links {
136 t := unixfspb.Data_DataType(-1)
137
137 - linkNode, err := link.GetNode(req.Context(), dserv)
138 - if err == ipld.ErrNotFound && !resolve {
139 - // not an error
140 - linkNode = nil
141 - } else if err != nil {
142 - res.SetError(err, cmdkit.ErrNormal)
143 - return
144 - }
145 -
146 - if pn, ok := linkNode.(*merkledag.ProtoNode); ok {
147 - d, err := unixfs.FromBytes(pn.Data())
148 - if err != nil {
138 + switch link.Cid.Type() {
139 + case cid.Raw:
140 + // No need to check with raw leaves
141 + t = unixfspb.Data_File
142 + case cid.DagProtobuf:
143 + linkNode, err := link.GetNode(req.Context(), dserv)
144 + if err == ipld.ErrNotFound && !resolve {
145 + // not an error
146 + linkNode = nil
147 + } else if err != nil {
148 res.SetError(err, cmdkit.ErrNormal)
149 return
150 }
151
153 - t = d.GetType()
152 + if pn, ok := linkNode.(*merkledag.ProtoNode); ok {
153 + d, err := unixfs.FromBytes(pn.Data())
154 + if err != nil {
155 + res.SetError(err, cmdkit.ErrNormal)
156 + return
157 + }
158 + t = d.GetType()
159 + }
160 }
161 output[i].Links[j] = LsLink{
162 Name: link.Name,