-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
297 lines (255 loc) · 6.9 KB
/
dag.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
package dagcmd
import (
"bytes"
"fmt"
"io"
"math"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
coredag "github.com/ipfs/go-ipfs/core/coredag"
path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin"
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
cmdkit "gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
files "gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit/files"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
var log = logging.Logger("cmds/files")
var DagCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Interact with ipld dag objects.",
ShortDescription: `
'ipfs dag' is used for creating and manipulating dag objects.
This subcommand is currently an experimental feature, but it is intended
to deprecate and replace the existing 'ipfs object' command moving forward.
`,
},
Subcommands: map[string]*cmds.Command{
"put": DagPutCmd,
"get": DagGetCmd,
"resolve": DagResolveCmd,
},
}
// OutputObject is the output type of 'dag put' command
type OutputObject struct {
Cid *cid.Cid
}
// ResolveOutput is the output type of 'dag resolve' command
type ResolveOutput struct {
Cid *cid.Cid
RemPath string
}
var DagPutCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Add a dag node to ipfs.",
ShortDescription: `
'ipfs dag put' accepts input from a file or stdin and parses it
into an object of the specified format.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.FileArg("object data", true, true, "The object to put").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption("format", "f", "Format that the object will be added as.").WithDefault("cbor"),
cmdkit.StringOption("input-enc", "Format that the input object will be.").WithDefault("json"),
cmdkit.BoolOption("pin", "Pin this object when adding."),
cmdkit.StringOption("hash", "Hash function to use").WithDefault(""),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
ienc, _, _ := req.Option("input-enc").String()
format, _, _ := req.Option("format").String()
hash, _, err := req.Option("hash").String()
dopin, _, err := req.Option("pin").Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
// mhType tells inputParser which hash should be used. MaxUint64 means 'use
// default hash' (sha256 for cbor, sha1 for git..)
mhType := uint64(math.MaxUint64)
if hash != "" {
var ok bool
mhType, ok = mh.Names[hash]
if !ok {
res.SetError(fmt.Errorf("%s in not a valid multihash name", hash), cmdkit.ErrNormal)
return
}
}
outChan := make(chan interface{}, 8)
res.SetOutput((<-chan interface{})(outChan))
addAllAndPin := func(f files.File) error {
cids := cid.NewSet()
b := ipld.NewBatch(req.Context(), n.DAG)
for {
file, err := f.NextFile()
if err == io.EOF {
// Finished the list of files.
break
} else if err != nil {
return err
}
nds, err := coredag.ParseInputs(ienc, format, file, mhType, -1)
if err != nil {
return err
}
if len(nds) == 0 {
return fmt.Errorf("no node returned from ParseInputs")
}
for _, nd := range nds {
err := b.Add(nd)
if err != nil {
return err
}
}
cid := nds[0].Cid()
cids.Add(cid)
outChan <- &OutputObject{Cid: cid}
}
if err := b.Commit(); err != nil {
return err
}
if dopin {
defer n.Blockstore.PinLock().Unlock()
cids.ForEach(func(c *cid.Cid) error {
n.Pinning.PinWithMode(c, pin.Recursive)
return nil
})
err := n.Pinning.Flush()
if err != nil {
return err
}
}
return nil
}
go func() {
defer close(outChan)
if err := addAllAndPin(req.Files()); err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
}()
},
Type: OutputObject{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}
oobj, ok := v.(*OutputObject)
if !ok {
return nil, e.TypeErr(oobj, v)
}
return strings.NewReader(oobj.Cid.String() + "\n"), nil
},
},
}
var DagGetCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Get a dag node from ipfs.",
ShortDescription: `
'ipfs dag get' fetches a dag node from ipfs and prints it out in the specifed
format.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("ref", true, false, "The object to get").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
p, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
obj, rem, err := n.Resolver.ResolveToLastNode(req.Context(), p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
var out interface{} = obj
if len(rem) > 0 {
final, _, err := obj.Resolve(rem)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
out = final
}
res.SetOutput(out)
},
}
// DagResolveCmd returns address of highest block within a path and a path remainder
var DagResolveCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Resolve ipld block",
ShortDescription: `
'ipfs dag resolve' fetches a dag node from ipfs, prints it's address and remaining path.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("ref", true, false, "The path to resolve").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
p, err := path.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
obj, rem, err := n.Resolver.ResolveToLastNode(req.Context(), p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
res.SetOutput(&ResolveOutput{
Cid: obj.Cid(),
RemPath: path.Join(rem),
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}
output := v.(*ResolveOutput)
buf := new(bytes.Buffer)
p := output.Cid.String()
if output.RemPath != "" {
p = path.Join([]string{p, output.RemPath})
}
buf.WriteString(p)
return buf, nil
},
},
Type: ResolveOutput{},
}
// copy+pasted from ../commands.go
func unwrapOutput(i interface{}) (interface{}, error) {
var (
ch <-chan interface{}
ok bool
)
if ch, ok = i.(<-chan interface{}); !ok {
return nil, e.TypeErr(ch, i)
}
return <-ch, nil
}