-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
323 lines (274 loc) · 7.39 KB
/
patch.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
package objectcmd
import (
"io"
"io/ioutil"
"strings"
key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
dag "github.com/ipfs/go-ipfs/merkledag"
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
path "github.com/ipfs/go-ipfs/path"
ft "github.com/ipfs/go-ipfs/unixfs"
u "github.com/ipfs/go-ipfs/util"
)
var ObjectPatchCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Create a new merkledag object based on an existing one.",
ShortDescription: `
'ipfs object patch <root> <cmd> <args>' is a plumbing command used to
build custom DAG objects. It mutates objects, creating new objects as a
result. This is the merkle-dag version of modifying an object.
`,
},
Arguments: []cmds.Argument{},
Subcommands: map[string]*cmds.Command{
"append-data": patchAppendDataCmd,
"add-link": patchAddLinkCmd,
"rm-link": patchRmLinkCmd,
"set-data": patchSetDataCmd,
},
}
func objectMarshaler(res cmds.Response) (io.Reader, error) {
o, ok := res.Output().(*Object)
if !ok {
return nil, u.ErrCast()
}
return strings.NewReader(o.Hash + "\n"), nil
}
var patchAppendDataCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Append data to the data segment of a dag node.",
ShortDescription: `
Append data to what already exists in the data segment in the given object.
EXAMPLE:
$ echo "hello" | ipfs object patch $HASH append-data
note: this does not append data to a 'file', it modifies the actual raw
data within an object. Objects have a max size of 1MB and objects larger than
the limit will not be respected by the network.
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("root", true, false, "The hash of the node to modify."),
cmds.FileArg("data", true, false, "Data to append.").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
root, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rootnd, err := core.Resolve(req.Context(), nd, root)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
fi, err := req.Files().NextFile()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
data, err := ioutil.ReadAll(fi)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rootnd.Data = append(rootnd.Data, data...)
newkey, err := nd.DAG.Add(rootnd)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&Object{Hash: newkey.B58String()})
},
Type: Object{},
Marshalers: cmds.MarshalerMap{
cmds.Text: objectMarshaler,
},
}
var patchSetDataCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Set data field of an ipfs object.",
ShortDescription: `
Set the data of an ipfs object from stdin or with the contents of a file
EXAMPLE:
$ echo "my data" | ipfs object patch $MYHASH set-data
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("root", true, false, "The hash of the node to modify."),
cmds.FileArg("data", true, false, "Data fill with.").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rp, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd, rp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
fi, err := req.Files().NextFile()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
data, err := ioutil.ReadAll(fi)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
root.Data = data
newkey, err := nd.DAG.Add(root)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&Object{Hash: newkey.B58String()})
},
Type: Object{},
Marshalers: cmds.MarshalerMap{
cmds.Text: objectMarshaler,
},
}
var patchRmLinkCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove a link from an object.",
ShortDescription: `
removes a link by the given name from root.
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("root", true, false, "The hash of the node to modify."),
cmds.StringArg("link", true, false, "Name of the link to remove."),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rootp, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd, rootp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
path := req.Arguments()[1]
e := dagutils.NewDagEditor(root, nd.DAG)
err = e.RmLink(req.Context(), path)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
nnode, err := e.Finalize(nd.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
nk, err := nnode.Key()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&Object{Hash: nk.B58String()})
},
Type: Object{},
Marshalers: cmds.MarshalerMap{
cmds.Text: objectMarshaler,
},
}
var patchAddLinkCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Add a link to a given object.",
ShortDescription: `
Add a merkle-link to the given object and return the hash of the result.
Examples:
EMPTY_DIR=$(ipfs object new unixfs-dir)
BAR=$(echo "bar" | ipfs add -q)
ipfs object patch $EMPTY_DIR add-link foo $BAR
This takes an empty directory, and adds a link named foo under it, pointing to
a file containing 'bar', and returns the hash of the new object.
`,
},
Options: []cmds.Option{
cmds.BoolOption("p", "create", "Create intermediary nodes."),
},
Arguments: []cmds.Argument{
cmds.StringArg("root", true, false, "The hash of the node to modify."),
cmds.StringArg("name", true, false, "Name of link to create."),
cmds.StringArg("ref", true, false, "IPFS object to add link to."),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rootp, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
root, err := core.Resolve(req.Context(), nd, rootp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
path := req.Arguments()[1]
childk := key.B58KeyDecode(req.Arguments()[2])
create, _, err := req.Option("create").Bool()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
var createfunc func() *dag.Node
if create {
createfunc = func() *dag.Node {
return &dag.Node{Data: ft.FolderPBData()}
}
}
e := dagutils.NewDagEditor(root, nd.DAG)
childnd, err := nd.DAG.Get(req.Context(), childk)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
err = e.InsertNodeAtPath(req.Context(), path, childnd, createfunc)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
nnode, err := e.Finalize(nd.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
nk, err := nnode.Key()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&Object{Hash: nk.B58String()})
},
Type: Object{},
Marshalers: cmds.MarshalerMap{
cmds.Text: objectMarshaler,
},
}