diff --git a/core/commands/ls.go b/core/commands/ls.go index 979a93081cc..9bcfd79563c 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -6,6 +6,7 @@ import ( "io" "text/tabwriter" + key "github.com/ipfs/go-ipfs/blocks/key" cmds "github.com/ipfs/go-ipfs/commands" core "github.com/ipfs/go-ipfs/core" merkledag "github.com/ipfs/go-ipfs/merkledag" @@ -45,6 +46,7 @@ format: }, Options: []cmds.Option{ cmds.BoolOption("headers", "v", "Print table headers (Hash, Size, Name).").Default(false), + cmds.BoolOption("resolve-type", "Resolve linked objects to find out their types.").Default(true), }, Run: func(req cmds.Request, res cmds.Response) { node, err := req.InvocContext().GetNode() @@ -59,6 +61,12 @@ format: return } + resolve, _, err := req.Option("resolve-type").Bool() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + paths := req.Arguments() var dagnodes []*merkledag.Node @@ -79,21 +87,42 @@ format: } for j, link := range dagnode.Links { var linkNode *merkledag.Node - linkNode, err = link.GetNode(req.Context(), node.DAG) - if err != nil { - res.SetError(err, cmds.ErrNormal) - return + t := unixfspb.Data_DataType(-1) + linkKey := key.Key(link.Hash) + if ok, err := node.Blockstore.Has(linkKey); ok && err == nil { + b, err := node.Blockstore.Get(linkKey) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + linkNode, err = merkledag.DecodeProtobuf(b.Data()) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + } + + if linkNode == nil && resolve { + linkNode, err = link.GetNode(req.Context(), node.DAG) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } } - d, err := unixfs.FromBytes(linkNode.Data) - if err != nil { - res.SetError(err, cmds.ErrNormal) - return + if linkNode != nil { + d, err := unixfs.FromBytes(linkNode.Data) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + t = d.GetType() } output[i].Links[j] = LsLink{ Name: link.Name, Hash: link.Hash.B58String(), Size: link.Size, - Type: d.GetType(), + Type: t, } } }