-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ls.go
255 lines (217 loc) · 5.99 KB
/
ls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package unixfs
import (
"bytes"
"fmt"
"io"
"sort"
"text/tabwriter"
cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
merkledag "gx/ipfs/QmSei8kFMfqdJq7Q68d2LMnHbTWKKg2daA29ezUYFAUNgc/go-merkledag"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
unixfs "gx/ipfs/QmfB3oNXGGq9S4B2a9YeCajoATms3Zw2VvDm8fK7VeLSV8/go-unixfs"
)
type LsLink struct {
Name, Hash string
Size uint64
Type string
}
type LsObject struct {
Hash string
Size uint64
Type string
Links []LsLink
}
type LsOutput struct {
Arguments map[string]string
Objects map[string]*LsObject
}
var LsCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List directory contents for Unix filesystem objects.",
ShortDescription: `
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.
This functionality is deprecated, and will be removed in future versions. If
possible, please use 'ipfs ls' instead.
`,
LongDescription: `
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.
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
This functionality is deprecated, and will be removed in future versions. If
possible, please use 'ipfs ls' instead.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from.").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
node, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
paths := req.Arguments()
output := LsOutput{
Arguments: map[string]string{},
Objects: map[string]*LsObject{},
}
for _, p := range paths {
ctx := req.Context()
fpath, err := iface.ParsePath(p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
merkleNode, err := api.ResolveNode(ctx, fpath)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
c := merkleNode.Cid()
hash := c.String()
output.Arguments[p] = hash
if _, ok := output.Objects[hash]; ok {
// duplicate argument for an already-listed node
continue
}
ndpb, ok := merkleNode.(*merkledag.ProtoNode)
if !ok {
res.SetError(merkledag.ErrNotProtobuf, cmdkit.ErrNormal)
return
}
unixFSNode, err := unixfs.FSNodeFromBytes(ndpb.Data())
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
t := unixFSNode.Type()
output.Objects[hash] = &LsObject{
Hash: c.String(),
Type: t.String(),
Size: unixFSNode.FileSize(),
}
switch t {
case unixfs.TFile:
break
case unixfs.THAMTShard:
// We need a streaming ls API for this.
res.SetError(fmt.Errorf("cannot list large directories yet"), cmdkit.ErrNormal)
return
case unixfs.TDirectory:
links := make([]LsLink, len(merkleNode.Links()))
output.Objects[hash].Links = links
for i, link := range merkleNode.Links() {
linkNode, err := link.GetNode(ctx, node.DAG)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
lnpb, ok := linkNode.(*merkledag.ProtoNode)
if !ok {
res.SetError(merkledag.ErrNotProtobuf, cmdkit.ErrNormal)
return
}
d, err := unixfs.FSNodeFromBytes(lnpb.Data())
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
t := d.Type()
lsLink := LsLink{
Name: link.Name,
Hash: link.Cid.String(),
Type: t.String(),
}
if t == unixfs.TFile {
lsLink.Size = d.FileSize()
} else {
lsLink.Size = link.Size
}
links[i] = lsLink
}
case unixfs.TSymlink:
res.SetError(fmt.Errorf("cannot list symlinks yet"), cmdkit.ErrNormal)
return
default:
res.SetError(fmt.Errorf("unrecognized type: %s", t), cmdkit.ErrImplementation)
return
}
}
res.SetOutput(&output)
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}
output, ok := v.(*LsOutput)
if !ok {
return nil, e.TypeErr(output, v)
}
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
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)
}
if object.Type == "Directory" {
directories = append(directories, argument)
} else {
nonDirectories = append(nonDirectories, argument)
}
}
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)
}
if len(output.Arguments) > 1 {
for _, arg := range directories[i:] {
if output.Arguments[arg] == hash {
fmt.Fprintf(w, "%s:\n", arg)
}
}
}
for _, link := range object.Links {
fmt.Fprintf(w, "%s\n", link.Name)
}
}
w.Flush()
return buf, nil
},
},
Type: LsOutput{},
}