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

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

	cmds "github.com/ipfs/go-ipfs/commands"
	core "github.com/ipfs/go-ipfs/core"
Richard Littauer's avatar
Richard Littauer committed
12
	merkledag "github.com/ipfs/go-ipfs/merkledag"
13 14
	path "github.com/ipfs/go-ipfs/path"
	unixfs "github.com/ipfs/go-ipfs/unixfs"
15
	uio "github.com/ipfs/go-ipfs/unixfs/io"
16 17 18 19 20 21
	unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"
)

type LsLink struct {
	Name, Hash string
	Size       uint64
22
	Type       string
23 24 25
}

type LsObject struct {
26 27 28
	Hash  string
	Size  uint64
	Type  string
29
	Links []LsLink
30 31 32
}

type LsOutput struct {
33 34
	Arguments map[string]string
	Objects   map[string]*LsObject
35 36 37 38
}

var LsCmd = &cmds.Command{
	Helptext: cmds.HelpText{
39
		Tagline: "List directory contents for Unix filesystem objects.",
40
		ShortDescription: `
Richard Littauer's avatar
Richard Littauer committed
41 42 43 44 45
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.
46 47 48

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
49 50 51
`,
		LongDescription: `
Displays the contents of an IPFS or IPNS object(s) at the given path.
52

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

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
66 67 68

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

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

		paths := req.Arguments()

84 85 86 87 88 89
		output := LsOutput{
			Arguments: map[string]string{},
			Objects:   map[string]*LsObject{},
		}

		for _, fpath := range paths {
Jeromy's avatar
Jeromy committed
90
			ctx := req.Context()
91 92 93 94 95 96 97

			resolver := &path.Resolver{
				DAG:         node.DAG,
				ResolveOnce: uio.ResolveUnixfsOnce,
			}

			merkleNode, err := core.Resolve(ctx, node.Namesys, resolver, path.Path(fpath))
98 99 100 101 102
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

Jeromy's avatar
Jeromy committed
103
			c := merkleNode.Cid()
104

Jeromy's avatar
Jeromy committed
105
			hash := c.String()
106 107 108 109 110 111 112
			output.Arguments[fpath] = hash

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

113 114 115 116 117 118 119
			ndpb, ok := merkleNode.(*merkledag.ProtoNode)
			if !ok {
				res.SetError(merkledag.ErrNotProtobuf, cmds.ErrNormal)
				return
			}

			unixFSNode, err := unixfs.FromBytes(ndpb.Data())
120 121 122 123
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
124 125

			t := unixFSNode.GetType()
126 127

			output.Objects[hash] = &LsObject{
Jeromy's avatar
Jeromy committed
128
				Hash: c.String(),
129 130 131 132
				Type: t.String(),
				Size: unixFSNode.GetFilesize(),
			}

133 134
			switch t {
			case unixfspb.Data_File:
135
				break
136
			case unixfspb.Data_Directory:
137
				links := make([]LsLink, len(merkleNode.Links()))
138
				output.Objects[hash].Links = links
139 140
				for i, link := range merkleNode.Links() {
					linkNode, err := link.GetNode(ctx, node.DAG)
141 142 143 144
					if err != nil {
						res.SetError(err, cmds.ErrNormal)
						return
					}
145 146 147 148 149 150 151
					lnpb, ok := linkNode.(*merkledag.ProtoNode)
					if !ok {
						res.SetError(merkledag.ErrNotProtobuf, cmds.ErrNormal)
						return
					}

					d, err := unixfs.FromBytes(lnpb.Data())
152 153 154 155
					if err != nil {
						res.SetError(err, cmds.ErrNormal)
						return
					}
156
					t := d.GetType()
157 158
					lsLink := LsLink{
						Name: link.Name,
159
						Hash: link.Cid.String(),
160
						Type: t.String(),
161
					}
162
					if t == unixfspb.Data_File {
163 164 165 166
						lsLink.Size = d.GetFilesize()
					} else {
						lsLink.Size = link.Size
					}
167
					links[i] = lsLink
168
				}
Jeromy's avatar
Jeromy committed
169 170 171 172 173 174
			case unixfspb.Data_Symlink:
				res.SetError(fmt.Errorf("cannot list symlinks yet"), cmds.ErrNormal)
				return
			default:
				res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation)
				return
175 176 177
			}
		}

178
		res.SetOutput(&output)
179 180 181 182 183 184 185
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {

			output := res.Output().(*LsOutput)
			buf := new(bytes.Buffer)
			w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
186 187 188 189 190 191 192 193 194

			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)
				}

195
				if object.Type == "Directory" {
196
					directories = append(directories, argument)
197 198
				} else {
					nonDirectories = append(nonDirectories, argument)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
				}
			}
			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)
				}
220 221 222 223 224
				if len(output.Arguments) > 1 {
					for _, arg := range directories[i:] {
						if output.Arguments[arg] == hash {
							fmt.Fprintf(w, "%s:\n", arg)
						}
225
					}
226 227 228 229 230 231 232 233 234 235 236 237
				}
				for _, link := range object.Links {
					fmt.Fprintf(w, "%s\n", link.Name)
				}
			}
			w.Flush()

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