From 3b0e16e38b0fe30d98539cb2ee450822fe7ea912 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 28 Mar 2018 19:53:55 -0700 Subject: [PATCH] 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 --- core/commands/ls.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/core/commands/ls.go b/core/commands/ls.go index 6bfa91a67..d200fb0ac 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -18,6 +18,7 @@ import ( uio "github.com/ipfs/go-ipfs/unixfs/io" unixfspb "github.com/ipfs/go-ipfs/unixfs/pb" + cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" "gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit" ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" ) @@ -134,23 +135,28 @@ The JSON output contains type information. for j, link := range links { t := unixfspb.Data_DataType(-1) - linkNode, err := link.GetNode(req.Context(), dserv) - if err == ipld.ErrNotFound && !resolve { - // not an error - linkNode = nil - } else if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return - } - - if pn, ok := linkNode.(*merkledag.ProtoNode); ok { - d, err := unixfs.FromBytes(pn.Data()) - if err != nil { + switch link.Cid.Type() { + case cid.Raw: + // No need to check with raw leaves + t = unixfspb.Data_File + case cid.DagProtobuf: + linkNode, err := link.GetNode(req.Context(), dserv) + if err == ipld.ErrNotFound && !resolve { + // not an error + linkNode = nil + } else if err != nil { res.SetError(err, cmdkit.ErrNormal) return } - t = d.GetType() + if pn, ok := linkNode.(*merkledag.ProtoNode); ok { + d, err := unixfs.FromBytes(pn.Data()) + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + t = d.GetType() + } } output[i].Links[j] = LsLink{ Name: link.Name,