ls.go 5.32 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2 3
package commands

import (
4
	"bytes"
Matt Bell's avatar
Matt Bell committed
5
	"fmt"
6
	"io"
7
	"text/tabwriter"
Jeromy's avatar
Jeromy committed
8

9
	cmds "github.com/ipfs/go-ipfs/commands"
Jan Winkelmann's avatar
Jan Winkelmann committed
10
	e "github.com/ipfs/go-ipfs/core/commands/e"
11 12
	iface "github.com/ipfs/go-ipfs/core/coreapi/interface"

13
	blockservice "gx/ipfs/QmNozJswSuwiZspexEHcQo5GMqpzM5exUGjNW6s4AAipUX/go-blockservice"
14
	cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
15
	offline "gx/ipfs/QmPXcrGQQEEPswwg6YiE2WLk8qkmvncZ7zphMKKP8bXqY3/go-ipfs-exchange-offline"
16
	"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
17 18 19 20
	merkledag "gx/ipfs/QmTGpm48qm4fUZ9E5hMXy4ZngJUYCMKu15rTMVR3BSEnPm/go-merkledag"
	unixfs "gx/ipfs/QmavvHwEZTkNShKWK1jRejv2Y8oF6ZYxdGxytL3Mwvices/go-unixfs"
	uio "gx/ipfs/QmavvHwEZTkNShKWK1jRejv2Y8oF6ZYxdGxytL3Mwvices/go-unixfs/io"
	unixfspb "gx/ipfs/QmavvHwEZTkNShKWK1jRejv2Y8oF6ZYxdGxytL3Mwvices/go-unixfs/pb"
21
	ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
Matt Bell's avatar
Matt Bell committed
22 23
)

24
type LsLink struct {
Matt Bell's avatar
Matt Bell committed
25 26
	Name, Hash string
	Size       uint64
27
	Type       unixfspb.Data_DataType
Matt Bell's avatar
Matt Bell committed
28 29
}

30
type LsObject struct {
31
	Hash  string
32
	Links []LsLink
33 34 35
}

type LsOutput struct {
36
	Objects []LsObject
37 38
}

39
var LsCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
40
	Helptext: cmdkit.HelpText{
41
		Tagline: "List directory contents for Unix filesystem objects.",
42
		ShortDescription: `
43 44
Displays the contents of an IPFS or IPNS object(s) at the given path, with
the following format:
45 46

  <link base58 hash> <link size in bytes> <link name>
47 48

The JSON output contains type information.
49
`,
50
	},
51

Jan Winkelmann's avatar
Jan Winkelmann committed
52 53
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from.").EnableStdin(),
54
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
55
	Options: []cmdkit.Option{
56
		cmdkit.BoolOption("headers", "v", "Print table headers (Hash, Size, Name)."),
57
		cmdkit.BoolOption("resolve-type", "Resolve linked objects to find out their types.").WithDefault(true),
Henry's avatar
Henry committed
58
	},
59
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
60
		nd, err := req.InvocContext().GetNode()
61
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
62
			res.SetError(err, cmdkit.ErrNormal)
63
			return
64
		}
Matt Bell's avatar
Matt Bell committed
65

66 67 68 69 70 71
		api, err := req.InvocContext().GetApi()
		if err != nil {
			res.SetError(err, cmdkit.ErrNormal)
			return
		}

Henry's avatar
Henry committed
72 73
		// get options early -> exit early in case of error
		if _, _, err := req.Option("headers").Bool(); err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
74
			res.SetError(err, cmdkit.ErrNormal)
Henry's avatar
Henry committed
75 76 77
			return
		}

78 79
		resolve, _, err := req.Option("resolve-type").Bool()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
80
			res.SetError(err, cmdkit.ErrNormal)
81 82 83
			return
		}

84 85 86 87 88 89 90
		dserv := nd.DAG
		if !resolve {
			offlineexch := offline.Exchange(nd.Blockstore)
			bserv := blockservice.New(nd.Blockstore, offlineexch)
			dserv = merkledag.NewDAGService(bserv)
		}

91
		paths := req.Arguments()
92

93
		var dagnodes []ipld.Node
Jeromy's avatar
Jeromy committed
94
		for _, fpath := range paths {
95
			p, err := iface.ParsePath(fpath)
96
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
97
				res.SetError(err, cmdkit.ErrNormal)
98 99 100
				return
			}

101
			dagnode, err := api.ResolveNode(req.Context(), p)
Matt Bell's avatar
Matt Bell committed
102
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
103
				res.SetError(err, cmdkit.ErrNormal)
104
				return
Matt Bell's avatar
Matt Bell committed
105
			}
106 107
			dagnodes = append(dagnodes, dagnode)
		}
Matt Bell's avatar
Matt Bell committed
108

109
		output := make([]LsObject, len(req.Arguments()))
hannahhoward's avatar
hannahhoward committed
110 111
		ng := merkledag.NewSession(req.Context(), nd.DAG)
		ro := merkledag.NewReadOnlyDagService(ng)
Jan Winkelmann's avatar
Jan Winkelmann committed
112

113
		for i, dagnode := range dagnodes {
hannahhoward's avatar
hannahhoward committed
114
			dir, err := uio.NewDirectoryFromNode(ro, dagnode)
115
			if err != nil && err != uio.ErrNotADir {
116
				res.SetError(fmt.Errorf("the data in %s (at %q) is not a UnixFS directory: %s", dagnode.Cid(), paths[i], err), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
117 118 119
				return
			}

120
			var links []*ipld.Link
121 122 123 124 125
			if dir == nil {
				links = dagnode.Links()
			} else {
				links, err = dir.Links(req.Context())
				if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
126
					res.SetError(err, cmdkit.ErrNormal)
127 128
					return
				}
Jeromy's avatar
Jeromy committed
129 130
			}

131
			output[i] = LsObject{
132
				Hash:  paths[i],
Jeromy's avatar
Jeromy committed
133
				Links: make([]LsLink, len(links)),
134
			}
Jeromy's avatar
Jeromy committed
135 136

			for j, link := range links {
137
				t := unixfspb.Data_DataType(-1)
138

139 140 141
				switch link.Cid.Type() {
				case cid.Raw:
					// No need to check with raw leaves
Overbool's avatar
Overbool committed
142
					t = unixfs.TFile
143 144 145 146 147 148
				case cid.DagProtobuf:
					linkNode, err := link.GetNode(req.Context(), dserv)
					if err == ipld.ErrNotFound && !resolve {
						// not an error
						linkNode = nil
					} else if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
149
						res.SetError(err, cmdkit.ErrNormal)
150 151 152
						return
					}

153
					if pn, ok := linkNode.(*merkledag.ProtoNode); ok {
Overbool's avatar
Overbool committed
154
						d, err := unixfs.FSNodeFromBytes(pn.Data())
155 156 157 158
						if err != nil {
							res.SetError(err, cmdkit.ErrNormal)
							return
						}
Overbool's avatar
Overbool committed
159
						t = d.Type()
160
					}
161
				}
162 163
				output[i].Links[j] = LsLink{
					Name: link.Name,
164
					Hash: link.Cid.String(),
165
					Size: link.Size,
166
					Type: t,
Matt Bell's avatar
Matt Bell committed
167 168 169 170
				}
			}
		}

171
		res.SetOutput(&LsOutput{output})
Matt Bell's avatar
Matt Bell committed
172
	},
173
	Marshalers: cmds.MarshalerMap{
174
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Henry's avatar
Henry committed
175

Jan Winkelmann's avatar
Jan Winkelmann committed
176 177 178 179 180
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

Henry's avatar
Henry committed
181
			headers, _, _ := res.Request().Option("headers").Bool()
Jan Winkelmann's avatar
Jan Winkelmann committed
182 183 184 185 186
			output, ok := v.(*LsOutput)
			if !ok {
				return nil, e.TypeErr(output, v)
			}

187 188
			buf := new(bytes.Buffer)
			w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
Henry's avatar
Henry committed
189 190
			for _, object := range output.Objects {
				if len(output.Objects) > 1 {
191
					fmt.Fprintf(w, "%s:\n", object.Hash)
192
				}
Henry's avatar
Henry committed
193
				if headers {
194
					fmt.Fprintln(w, "Hash\tSize\tName")
Henry's avatar
Henry committed
195
				}
196
				for _, link := range object.Links {
Overbool's avatar
Overbool committed
197
					if link.Type == unixfs.TDirectory {
198 199
						link.Name += "/"
					}
200
					fmt.Fprintf(w, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
201
				}
Henry's avatar
Henry committed
202
				if len(output.Objects) > 1 {
203
					fmt.Fprintln(w)
204
				}
Matt Bell's avatar
Matt Bell committed
205
			}
206
			w.Flush()
Matt Bell's avatar
Matt Bell committed
207

208
			return buf, nil
209
		},
Matt Bell's avatar
Matt Bell committed
210
	},
211
	Type: LsOutput{},
Matt Bell's avatar
Matt Bell committed
212
}