ls.go 6.12 KB
Newer Older
1 2 3 4 5 6
package unixfs

import (
	"bytes"
	"fmt"
	"io"
7
	"sort"
8 9
	"text/tabwriter"

10
	cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
11

12 13
	cmds "github.com/ipfs/go-ipfs/commands"
	core "github.com/ipfs/go-ipfs/core"
Jan Winkelmann's avatar
Jan Winkelmann committed
14
	e "github.com/ipfs/go-ipfs/core/commands/e"
Steven Allen's avatar
Steven Allen committed
15 16 17 18 19
	unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
	uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
	merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
	path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
	resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
20 21 22 23 24
)

type LsLink struct {
	Name, Hash string
	Size       uint64
25
	Type       string
26 27 28
}

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

type LsOutput struct {
36 37
	Arguments map[string]string
	Objects   map[string]*LsObject
38 39 40
}

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

The JSON output contains size information. For files, the child size
is the total size of the file contents. For directories, the child
size is the IPFS link size.
49 50 51

This functionality is deprecated, and will be removed in future versions. If
possible, please use 'ipfs ls' instead.
Richard Littauer's avatar
Richard Littauer committed
52 53 54
`,
		LongDescription: `
Displays the contents of an IPFS or IPNS object(s) at the given path.
55

56 57
The JSON output contains size information. For files, the child size
is the total size of the file contents. For directories, the child
58
size is the IPFS link size.
Richard Littauer's avatar
Richard Littauer committed
59 60 61 62 63 64 65 66 67 68

The path can be a prefixless ref; in this case, we assume it to be an
/ipfs ref and not /ipns.

Example:

    > ipfs file ls QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ
    cat.jpg
    > ipfs file ls /ipfs/QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ
    cat.jpg
69 70 71

This functionality is deprecated, and will be removed in future versions. If
possible, please use 'ipfs ls' instead.
72 73 74
`,
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
75 76
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from.").EnableStdin(),
77 78
	},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
79
		node, err := req.InvocContext().GetNode()
80
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
81
			res.SetError(err, cmdkit.ErrNormal)
82 83 84 85 86
			return
		}

		paths := req.Arguments()

87 88 89 90 91 92
		output := LsOutput{
			Arguments: map[string]string{},
			Objects:   map[string]*LsObject{},
		}

		for _, fpath := range paths {
Jeromy's avatar
Jeromy committed
93
			ctx := req.Context()
94

95
			resolver := &resolver.Resolver{
96 97 98 99 100
				DAG:         node.DAG,
				ResolveOnce: uio.ResolveUnixfsOnce,
			}

			merkleNode, err := core.Resolve(ctx, node.Namesys, resolver, path.Path(fpath))
101
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
102
				res.SetError(err, cmdkit.ErrNormal)
103 104 105
				return
			}

Jeromy's avatar
Jeromy committed
106
			c := merkleNode.Cid()
107

Jeromy's avatar
Jeromy committed
108
			hash := c.String()
109 110 111 112 113 114 115
			output.Arguments[fpath] = hash

			if _, ok := output.Objects[hash]; ok {
				// duplicate argument for an already-listed node
				continue
			}

116 117
			ndpb, ok := merkleNode.(*merkledag.ProtoNode)
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
118
				res.SetError(merkledag.ErrNotProtobuf, cmdkit.ErrNormal)
119 120 121
				return
			}

Overbool's avatar
Overbool committed
122
			unixFSNode, err := unixfs.FSNodeFromBytes(ndpb.Data())
123
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
124
				res.SetError(err, cmdkit.ErrNormal)
125 126
				return
			}
127

Overbool's avatar
Overbool committed
128
			t := unixFSNode.Type()
129 130

			output.Objects[hash] = &LsObject{
Jeromy's avatar
Jeromy committed
131
				Hash: c.String(),
132
				Type: t.String(),
Overbool's avatar
Overbool committed
133
				Size: unixFSNode.FileSize(),
134 135
			}

136
			switch t {
Overbool's avatar
Overbool committed
137
			case unixfs.TFile:
138
				break
Overbool's avatar
Overbool committed
139
			case unixfs.THAMTShard:
140 141 142
				// We need a streaming ls API for this.
				res.SetError(fmt.Errorf("cannot list large directories yet"), cmdkit.ErrNormal)
				return
Overbool's avatar
Overbool committed
143
			case unixfs.TDirectory:
144
				links := make([]LsLink, len(merkleNode.Links()))
145
				output.Objects[hash].Links = links
146 147
				for i, link := range merkleNode.Links() {
					linkNode, err := link.GetNode(ctx, node.DAG)
148
					if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
149
						res.SetError(err, cmdkit.ErrNormal)
150 151
						return
					}
152 153
					lnpb, ok := linkNode.(*merkledag.ProtoNode)
					if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
154
						res.SetError(merkledag.ErrNotProtobuf, cmdkit.ErrNormal)
155 156 157
						return
					}

Overbool's avatar
Overbool committed
158
					d, err := unixfs.FSNodeFromBytes(lnpb.Data())
159
					if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
160
						res.SetError(err, cmdkit.ErrNormal)
161 162
						return
					}
Overbool's avatar
Overbool committed
163
					t := d.Type()
164 165
					lsLink := LsLink{
						Name: link.Name,
166
						Hash: link.Cid.String(),
167
						Type: t.String(),
168
					}
Overbool's avatar
Overbool committed
169
					if t == unixfs.TFile {
Overbool's avatar
Overbool committed
170
						lsLink.Size = d.FileSize()
171 172 173
					} else {
						lsLink.Size = link.Size
					}
174
					links[i] = lsLink
175
				}
Overbool's avatar
Overbool committed
176
			case unixfs.TSymlink:
Jan Winkelmann's avatar
Jan Winkelmann committed
177
				res.SetError(fmt.Errorf("cannot list symlinks yet"), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
178 179
				return
			default:
Jan Winkelmann's avatar
Jan Winkelmann committed
180
				res.SetError(fmt.Errorf("unrecognized type: %s", t), cmdkit.ErrImplementation)
Jeromy's avatar
Jeromy committed
181
				return
182 183 184
			}
		}

185
		res.SetOutput(&output)
186 187 188
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
189 190 191 192
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}
193

Jan Winkelmann's avatar
Jan Winkelmann committed
194 195 196 197
			output, ok := v.(*LsOutput)
			if !ok {
				return nil, e.TypeErr(output, v)
			}
198 199
			buf := new(bytes.Buffer)
			w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
200 201 202 203 204 205 206 207 208

			nonDirectories := []string{}
			directories := []string{}
			for argument, hash := range output.Arguments {
				object, ok := output.Objects[hash]
				if !ok {
					return nil, fmt.Errorf("unresolved hash: %s", hash)
				}

209
				if object.Type == "Directory" {
210
					directories = append(directories, argument)
211 212
				} else {
					nonDirectories = append(nonDirectories, argument)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
				}
			}
			sort.Strings(nonDirectories)
			sort.Strings(directories)

			for _, argument := range nonDirectories {
				fmt.Fprintf(w, "%s\n", argument)
			}

			seen := map[string]bool{}
			for i, argument := range directories {
				hash := output.Arguments[argument]
				if _, ok := seen[hash]; ok {
					continue
				}
				seen[hash] = true

				object := output.Objects[hash]
				if i > 0 || len(nonDirectories) > 0 {
					fmt.Fprintln(w)
				}
234 235 236 237 238
				if len(output.Arguments) > 1 {
					for _, arg := range directories[i:] {
						if output.Arguments[arg] == hash {
							fmt.Fprintf(w, "%s:\n", arg)
						}
239
					}
240 241 242 243 244 245 246 247 248 249 250 251
				}
				for _, link := range object.Links {
					fmt.Fprintf(w, "%s\n", link.Name)
				}
			}
			w.Flush()

			return buf, nil
		},
	},
	Type: LsOutput{},
}