-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
object.go
564 lines (482 loc) · 14 KB
/
object.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package objectcmd
import (
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"text/tabwriter"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
dag "gx/ipfs/QmPJNbVw8o3ohC43ppSXyNXwYKsWShG4zygnirHptfbHri/go-merkledag"
"gx/ipfs/QmQkW9fnCsg9SLHdViiAh6qfBppodsPZVpU92dZLqYtEfs/go-ipfs-cmds"
"gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
coreiface "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core"
"gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core/options"
ipld "gx/ipfs/QmZ6nzCLwGLVfRzYLpD7pW6UNuBDKEcA2imJtVpbEx2rxy/go-ipld-format"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
type Node struct {
Links []Link
Data string
}
type Link struct {
Name, Hash string
Size uint64
}
type Object struct {
Hash string `json:"Hash,omitempty"`
Links []Link `json:"Links,omitempty"`
}
var ErrDataEncoding = errors.New("unkown data field encoding")
const (
headersOptionName = "headers"
encodingOptionName = "data-encoding"
inputencOptionName = "inputenc"
datafieldencOptionName = "datafieldenc"
pinOptionName = "pin"
quietOptionName = "quiet"
)
var ObjectCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Interact with IPFS objects.",
ShortDescription: `
'ipfs object' is a plumbing command used to manipulate DAG objects
directly.`,
},
Subcommands: map[string]*cmds.Command{
"data": ObjectDataCmd,
"diff": ObjectDiffCmd,
"get": ObjectGetCmd,
"links": ObjectLinksCmd,
"new": ObjectNewCmd,
"patch": ObjectPatchCmd,
"put": ObjectPutCmd,
"stat": ObjectStatCmd,
},
}
// ObjectDataCmd object data command
var ObjectDataCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Output the raw bytes of an IPFS object.",
ShortDescription: `
'ipfs object data' is a plumbing command for retrieving the raw bytes stored
in a DAG node. It outputs to stdout, and <key> is a base58 encoded multihash.
`,
LongDescription: `
'ipfs object data' is a plumbing command for retrieving the raw bytes stored
in a DAG node. It outputs to stdout, and <key> is a base58 encoded multihash.
Note that the "--encoding" option does not affect the output, since the output
is the raw data of the object.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
path, err := coreiface.ParsePath(req.Arguments[0])
if err != nil {
return err
}
data, err := api.Object().Data(req.Context, path)
if err != nil {
return err
}
return res.Emit(data)
},
}
// ObjectLinksCmd object links command
var ObjectLinksCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Output the links pointed to by the specified object.",
ShortDescription: `
'ipfs object links' is a plumbing command for retrieving the links from
a DAG node. It outputs to stdout, and <key> is a base58 encoded
multihash.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.BoolOption(headersOptionName, "v", "Print table headers (Hash, Size, Name)."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
enc, err := cmdenv.GetLowLevelCidEncoder(req)
if err != nil {
return err
}
path, err := coreiface.ParsePath(req.Arguments[0])
if err != nil {
return err
}
rp, err := api.ResolvePath(req.Context, path)
if err != nil {
return err
}
links, err := api.Object().Links(req.Context, rp)
if err != nil {
return err
}
outLinks := make([]Link, len(links))
for i, link := range links {
outLinks[i] = Link{
Hash: enc.Encode(link.Cid),
Name: link.Name,
Size: link.Size,
}
}
out := &Object{
Hash: enc.Encode(rp.Cid()),
Links: outLinks,
}
return cmds.EmitOnce(res, out)
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error {
tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
headers, _ := req.Options[headersOptionName].(bool)
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
for _, link := range out.Links {
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
}
tw.Flush()
return nil
}),
},
Type: &Object{},
}
// ObjectGetCmd object get command
var ObjectGetCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Get and serialize the DAG node named by <key>.",
ShortDescription: `
'ipfs object get' is a plumbing command for retrieving DAG nodes.
It serializes the DAG node to the format specified by the "--encoding"
flag. It outputs to stdout, and <key> is a base58 encoded multihash.
`,
LongDescription: `
'ipfs object get' is a plumbing command for retrieving DAG nodes.
It serializes the DAG node to the format specified by the "--encoding"
flag. It outputs to stdout, and <key> is a base58 encoded multihash.
This command outputs data in the following encodings:
* "protobuf"
* "json"
* "xml"
(Specified by the "--encoding" or "--enc" flag)
The encoding of the object's data field can be specifed by using the
--data-encoding flag
Supported values are:
* "text" (default)
* "base64"
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption(encodingOptionName, "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
enc, err := cmdenv.GetLowLevelCidEncoder(req)
if err != nil {
return err
}
path, err := coreiface.ParsePath(req.Arguments[0])
if err != nil {
return err
}
datafieldenc, _ := req.Options[encodingOptionName].(string)
if err != nil {
return err
}
nd, err := api.Object().Get(req.Context, path)
if err != nil {
return err
}
r, err := api.Object().Data(req.Context, path)
if err != nil {
return err
}
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
out, err := encodeData(data, datafieldenc)
if err != nil {
return err
}
node := &Node{
Links: make([]Link, len(nd.Links())),
Data: out,
}
for i, link := range nd.Links() {
node.Links[i] = Link{
Hash: enc.Encode(link.Cid),
Name: link.Name,
Size: link.Size,
}
}
return cmds.EmitOnce(res, node)
},
Type: Node{},
Encoders: cmds.EncoderMap{
cmds.Protobuf: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Node) error {
// deserialize the Data field as text as this was the standard behaviour
object, err := deserializeNode(out, "text")
if err != nil {
return nil
}
marshaled, err := object.Marshal()
if err != nil {
return err
}
fmt.Fprint(w, marshaled)
return nil
}),
},
}
// ObjectStatCmd object stat command
var ObjectStatCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Get stats for the DAG node named by <key>.",
ShortDescription: `
'ipfs object stat' is a plumbing command to print DAG node statistics.
<key> is a base58 encoded multihash. It outputs to stdout:
NumLinks int number of links in link table
BlockSize int size of the raw, encoded data
LinksSize int size of the links segment
DataSize int size of the data segment
CumulativeSize int cumulative size of object and its references
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
enc, err := cmdenv.GetLowLevelCidEncoder(req)
if err != nil {
return err
}
path, err := coreiface.ParsePath(req.Arguments[0])
if err != nil {
return err
}
ns, err := api.Object().Stat(req.Context, path)
if err != nil {
return err
}
oldStat := &ipld.NodeStat{
Hash: enc.Encode(ns.Cid),
NumLinks: ns.NumLinks,
BlockSize: ns.BlockSize,
LinksSize: ns.LinksSize,
DataSize: ns.DataSize,
CumulativeSize: ns.CumulativeSize,
}
return cmds.EmitOnce(res, oldStat)
},
Type: ipld.NodeStat{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ipld.NodeStat) error {
fw := func(s string, n int) {
fmt.Fprintf(w, "%s: %d\n", s, n)
}
fw("NumLinks", out.NumLinks)
fw("BlockSize", out.BlockSize)
fw("LinksSize", out.LinksSize)
fw("DataSize", out.DataSize)
fw("CumulativeSize", out.CumulativeSize)
return nil
}),
},
}
// ObjectPutCmd object put command
var ObjectPutCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Store input as a DAG object, print its key.",
ShortDescription: `
'ipfs object put' is a plumbing command for storing DAG nodes.
It reads from stdin, and the output is a base58 encoded multihash.
`,
LongDescription: `
'ipfs object put' is a plumbing command for storing DAG nodes.
It reads from stdin, and the output is a base58 encoded multihash.
Data should be in the format specified by the --inputenc flag.
--inputenc may be one of the following:
* "protobuf"
* "json" (default)
Examples:
$ echo '{ "Data": "abc" }' | ipfs object put
This creates a node with the data 'abc' and no links. For an object with
links, create a file named 'node.json' with the contents:
{
"Data": "another",
"Links": [ {
"Name": "some link",
"Hash": "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V",
"Size": 8
} ]
}
And then run:
$ ipfs object put node.json
`,
},
Arguments: []cmdkit.Argument{
cmdkit.FileArg("data", true, false, "Data to be stored as a DAG object.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption(inputencOptionName, "Encoding type of input data. One of: {\"protobuf\", \"json\"}.").WithDefault("json"),
cmdkit.StringOption(datafieldencOptionName, "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
cmdkit.BoolOption(pinOptionName, "Pin this object when adding."),
cmdkit.BoolOption(quietOptionName, "q", "Write minimal output."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
enc, err := cmdenv.GetLowLevelCidEncoder(req)
if err != nil {
return err
}
file, err := cmdenv.GetFileArg(req.Files.Entries())
if err != nil {
return err
}
inputenc, _ := req.Options[inputencOptionName].(string)
if err != nil {
return err
}
datafieldenc, _ := req.Options[datafieldencOptionName].(string)
if err != nil {
return err
}
dopin, _ := req.Options[pinOptionName].(bool)
if err != nil {
return err
}
p, err := api.Object().Put(req.Context, file,
options.Object.DataType(datafieldenc),
options.Object.InputEnc(inputenc),
options.Object.Pin(dopin))
if err != nil {
return err
}
return cmds.EmitOnce(res, &Object{Hash: enc.Encode(p.Cid())})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error {
quiet, _ := req.Options[quietOptionName].(bool)
o := out.Hash
if !quiet {
o = "added " + o
}
fmt.Fprintln(w, o)
return nil
}),
},
Type: Object{},
}
// ObjectNewCmd object new command
var ObjectNewCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Create a new object from an ipfs template.",
ShortDescription: `
'ipfs object new' is a plumbing command for creating new DAG nodes.
`,
LongDescription: `
'ipfs object new' is a plumbing command for creating new DAG nodes.
By default it creates and returns a new empty merkledag node, but
you may pass an optional template argument to create a preformatted
node.
Available templates:
* unixfs-dir
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("template", false, false, "Template to use. Optional."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
enc, err := cmdenv.GetLowLevelCidEncoder(req)
if err != nil {
return err
}
template := "empty"
if len(req.Arguments) == 1 {
template = req.Arguments[0]
}
nd, err := api.Object().New(req.Context, options.Object.Type(template))
if err != nil && err != io.EOF {
return err
}
return cmds.EmitOnce(res, &Object{Hash: enc.Encode(nd.Cid())})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error {
fmt.Fprintln(w, out.Hash)
return nil
}),
},
Type: Object{},
}
// converts the Node object into a real dag.ProtoNode
func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
dagnode := new(dag.ProtoNode)
switch dataFieldEncoding {
case "text":
dagnode.SetData([]byte(nd.Data))
case "base64":
data, err := base64.StdEncoding.DecodeString(nd.Data)
if err != nil {
return nil, err
}
dagnode.SetData(data)
default:
return nil, ErrDataEncoding
}
links := make([]*ipld.Link, len(nd.Links))
for i, link := range nd.Links {
c, err := cid.Decode(link.Hash)
if err != nil {
return nil, err
}
links[i] = &ipld.Link{
Name: link.Name,
Size: link.Size,
Cid: c,
}
}
dagnode.SetLinks(links)
return dagnode, nil
}
func encodeData(data []byte, encoding string) (string, error) {
switch encoding {
case "text":
return string(data), nil
case "base64":
return base64.StdEncoding.EncodeToString(data), nil
}
return "", ErrDataEncoding
}