-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
493 lines (417 loc) · 13 KB
/
add.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
package commands
import (
"fmt"
"io"
"path"
"path/filepath"
"strings"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"
ignore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/sabhiram/go-git-ignore"
//context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
cmds "github.com/ipfs/go-ipfs/commands"
files "github.com/ipfs/go-ipfs/commands/files"
core "github.com/ipfs/go-ipfs/core"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
importer "github.com/ipfs/go-ipfs/importer"
"github.com/ipfs/go-ipfs/importer/chunk"
dag "github.com/ipfs/go-ipfs/merkledag"
pin "github.com/ipfs/go-ipfs/pin"
ft "github.com/ipfs/go-ipfs/unixfs"
u "github.com/ipfs/go-ipfs/util"
)
// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
// how many bytes of progress to wait before sending a progress update message
const progressReaderIncrement = 1024 * 256
const (
progressOptionName = "progress"
trickleOptionName = "trickle"
wrapOptionName = "wrap-with-directory"
hiddenOptionName = "hidden"
)
type AddedObject struct {
Name string
Hash string `json:",omitempty"`
Bytes int64 `json:",omitempty"`
}
var AddCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Add an object to ipfs.",
ShortDescription: `
Adds contents of <path> to ipfs. Use -r to add directories.
Note that directories are added recursively, to form the ipfs
MerkleDAG. A smarter partial add with a staging area (like git)
remains to be implemented.
`,
},
Arguments: []cmds.Argument{
cmds.FileArg("path", true, true, "The path to a file to be added to IPFS").EnableRecursive().EnableStdin(),
},
Options: []cmds.Option{
cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
cmds.BoolOption("quiet", "q", "Write minimal output"),
cmds.BoolOption(progressOptionName, "p", "Stream progress data"),
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"),
cmds.BoolOption(hiddenOptionName, "Include files that are hidden"),
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation"),
cmds.BoolOption("only-hash", "n", "Only chunk and hash the specified content, don't write to disk"),
},
PreRun: func(req cmds.Request) error {
if quiet, _, _ := req.Option("quiet").Bool(); quiet {
return nil
}
req.SetOption(progressOptionName, true)
sizeFile, ok := req.Files().(files.SizeFile)
if !ok {
// we don't need to error, the progress bar just won't know how big the files are
return nil
}
size, err := sizeFile.Size()
if err != nil {
// see comment above
return nil
}
log.Debugf("Total size of file being added: %v\n", size)
req.Values()["size"] = size
return nil
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.Context().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
progress, _, _ := req.Option(progressOptionName).Bool()
trickle, _, _ := req.Option(trickleOptionName).Bool()
wrap, _, _ := req.Option(wrapOptionName).Bool()
hash, _, _ := req.Option("only-hash").Bool()
hidden, _, _ := req.Option(hiddenOptionName).Bool()
var ignoreFilePatterns []ignore.GitIgnore
// Check the IPFS_PATH
if ipfs_path := req.Context().ConfigRoot; len(ipfs_path) > 0 {
baseFilePattern, err := ignore.CompileIgnoreFile(path.Join(ipfs_path, ".ipfsignore"))
if err == nil && baseFilePattern != nil {
ignoreFilePatterns = append(ignoreFilePatterns, *baseFilePattern)
}
}
if hash {
nilnode, err := core.NewNodeBuilder().NilRepo().Build(n.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
n = nilnode
}
outChan := make(chan interface{}, 8)
res.SetOutput((<-chan interface{})(outChan))
go func() {
defer close(outChan)
for {
file, err := req.Files().NextFile()
if err != nil && err != io.EOF {
res.SetError(err, cmds.ErrNormal)
return
}
if file == nil { // done
return
}
// If the file is not a folder, then let's get the root of that
// folder and attempt to load the appropriate .ipfsignore.
localIgnorePatterns := checkForParentIgnorePatterns(file.FileName(), ignoreFilePatterns)
addParams := adder{n, outChan, progress, wrap, hidden, trickle}
rootnd, err := addParams.addFile(file, localIgnorePatterns)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
rnk, err := rootnd.Key()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
mp := n.Pinning.GetManual()
mp.RemovePinWithMode(rnk, pin.Indirect)
mp.PinWithMode(rnk, pin.Recursive)
err = n.Pinning.Flush()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}
}()
},
PostRun: func(req cmds.Request, res cmds.Response) {
if res.Error() != nil {
return
}
outChan, ok := res.Output().(<-chan interface{})
if !ok {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
res.SetOutput(nil)
quiet, _, err := req.Option("quiet").Bool()
if err != nil {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
size := int64(0)
s, found := req.Values()["size"]
if found {
size = s.(int64)
}
showProgressBar := !quiet && size >= progressBarMinSize
var bar *pb.ProgressBar
var terminalWidth int
if showProgressBar {
bar = pb.New64(size).SetUnits(pb.U_BYTES)
bar.ManualUpdate = true
bar.Start()
// the progress bar lib doesn't give us a way to get the width of the output,
// so as a hack we just use a callback to measure the output, then git rid of it
terminalWidth = 0
bar.Callback = func(line string) {
terminalWidth = len(line)
bar.Callback = nil
bar.Output = res.Stderr()
log.Infof("terminal width: %v\n", terminalWidth)
}
bar.Update()
}
lastFile := ""
var totalProgress, prevFiles, lastBytes int64
for out := range outChan {
output := out.(*AddedObject)
if len(output.Hash) > 0 {
if showProgressBar {
// clear progress bar line before we print "added x" output
fmt.Fprintf(res.Stderr(), "\r%s\r", strings.Repeat(" ", terminalWidth))
}
if quiet {
fmt.Fprintf(res.Stdout(), "%s\n", output.Hash)
} else {
fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
}
} else {
log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
if !showProgressBar {
continue
}
if len(lastFile) == 0 {
lastFile = output.Name
}
if output.Name != lastFile || output.Bytes < lastBytes {
prevFiles += lastBytes
lastFile = output.Name
}
lastBytes = output.Bytes
delta := prevFiles + lastBytes - totalProgress
totalProgress = bar.Add64(delta)
}
if showProgressBar {
bar.Update()
}
}
},
Type: AddedObject{},
}
// Internal structure for holding the switches passed to the `add` call
type adder struct {
node *core.IpfsNode
out chan interface{}
progress bool
wrap bool
hidden bool
trickle bool
}
// Perform the actual add & pin locally, outputting results to reader
func add(n *core.IpfsNode, reader io.Reader, useTrickle bool) (*dag.Node, error) {
var node *dag.Node
var err error
if useTrickle {
node, err = importer.BuildTrickleDagFromReader(
reader,
n.DAG,
chunk.DefaultSplitter,
importer.PinIndirectCB(n.Pinning.GetManual()),
)
} else {
node, err = importer.BuildDagFromReader(
reader,
n.DAG,
chunk.DefaultSplitter,
importer.PinIndirectCB(n.Pinning.GetManual()),
)
}
if err != nil {
return nil, err
}
return node, nil
}
// Add the given file while respecting the params and ignoreFilePatterns.
// Note that ignoreFilePatterns is not part of the struct as it may change while
// we dig through folders.
func (params *adder) addFile(file files.File, ignoreFilePatterns []ignore.GitIgnore) (*dag.Node, error) {
// Check if file is hidden
if fileIsHidden := files.IsHidden(file); fileIsHidden && !params.hidden {
log.Debugf("%s is hidden, skipping", file.FileName())
return nil, &hiddenFileError{file.FileName()}
}
// Check for ignore files matches
for i := range ignoreFilePatterns {
if ignoreFilePatterns[i].MatchesPath(file.FileName()) {
log.Debugf("%s is ignored file, skipping", file.FileName())
return nil, &ignoreFileError{file.FileName()}
}
}
// Check if "file" is actually a directory
if file.IsDirectory() {
return params.addDir(file, ignoreFilePatterns)
}
// if the progress flag was specified, wrap the file so that we can send
// progress updates to the client (over the output channel)
var reader io.Reader = file
if params.progress {
reader = &progressReader{file: file, out: params.out}
}
if params.wrap {
p, dagnode, err := coreunix.AddWrapped(params.node, reader, path.Base(file.FileName()))
if err != nil {
return nil, err
}
params.out <- &AddedObject{
Hash: p,
Name: file.FileName(),
}
return dagnode, nil
}
dagnode, err := add(params.node, reader, params.trickle)
if err != nil {
return nil, err
}
log.Infof("adding file: %s", file.FileName())
if err := outputDagnode(params.out, file.FileName(), dagnode); err != nil {
return nil, err
}
return dagnode, nil
}
func (params *adder) addDir(file files.File, ignoreFilePatterns []ignore.GitIgnore) (*dag.Node, error) {
tree := &dag.Node{Data: ft.FolderPBData()}
log.Infof("adding directory: %s", file.FileName())
// Check for an .ipfsignore file that is local to this Dir and append to the incoming
localIgnorePatterns := checkForLocalIgnorePatterns(file.FileName(), ignoreFilePatterns)
for {
file, err := file.NextFile()
if err != nil && err != io.EOF {
return nil, err
}
if file == nil {
break
}
node, err := params.addFile(file, localIgnorePatterns)
if _, ok := err.(*hiddenFileError); ok {
// hidden file error, set the node to nil for below
node = nil
} else if _, ok := err.(*ignoreFileError); ok {
// ignore file error, set the node to nil for below
node = nil
} else if err != nil {
return nil, err
}
if node != nil {
_, name := path.Split(file.FileName())
err = tree.AddNodeLink(name, node)
if err != nil {
return nil, err
}
}
}
err := outputDagnode(params.out, file.FileName(), tree)
if err != nil {
return nil, err
}
k, err := params.node.DAG.Add(tree)
if err != nil {
return nil, err
}
params.node.Pinning.GetManual().PinWithMode(k, pin.Indirect)
return tree, nil
}
// this helper checks the local path for any .ipfsignore file that need to be
// respected. returns the updated or the original GitIgnore.
func checkForLocalIgnorePatterns(dir string, ignoreFilePatterns []ignore.GitIgnore) []ignore.GitIgnore {
ignorePathname := path.Join(dir, ".ipfsignore")
localIgnore, ignoreErr := ignore.CompileIgnoreFile(ignorePathname)
if ignoreErr == nil && localIgnore != nil {
absoluteDir, _ := filepath.Abs(ignorePathname) // ignoring error it's just loggin
log.Debugf("found local ignore file: %s", absoluteDir)
return append(ignoreFilePatterns, *localIgnore)
} else {
return ignoreFilePatterns
}
}
// this helper just walks the parent directories of the given path looking for
// any .ipfsignore files in those directories.
func checkForParentIgnorePatterns(givenPath string, ignoreFilePatterns []ignore.GitIgnore) []ignore.GitIgnore {
absolutePath, err := filepath.Abs(givenPath)
if err != nil {
return ignoreFilePatterns
}
// break out the absolute path
dir := filepath.Dir(absolutePath)
pathComponents := strings.Split(dir, string(filepath.Separator))
// We loop through each parent component attempting to find an .ipfsignore file
for index, _ := range pathComponents {
pathParts := make([]string, index+1)
copy(pathParts, pathComponents[0:index+1])
ignorePathname := path.Clean(strings.Join(append(pathParts, ".ipfsignore"), string(filepath.Separator)))
localIgnore, ignoreErr := ignore.CompileIgnoreFile(ignorePathname)
if ignoreErr == nil && localIgnore != nil {
log.Debugf("found parent ignore file: %s", ignorePathname)
ignoreFilePatterns = append(ignoreFilePatterns, *localIgnore)
}
}
return ignoreFilePatterns
}
// outputDagnode sends dagnode info over the output channel
func outputDagnode(out chan interface{}, name string, dn *dag.Node) error {
o, err := getOutput(dn)
if err != nil {
return err
}
out <- &AddedObject{
Hash: o.Hash,
Name: name,
}
return nil
}
type hiddenFileError struct {
fileName string
}
func (e *hiddenFileError) Error() string {
return fmt.Sprintf("%s is a hidden file", e.fileName)
}
type ignoreFileError struct {
fileName string
}
func (e *ignoreFileError) Error() string {
return fmt.Sprintf("%s is an ignored file", e.fileName)
}
type progressReader struct {
file files.File
out chan interface{}
bytes int64
lastProgress int64
}
func (i *progressReader) Read(p []byte) (int, error) {
n, err := i.file.Read(p)
i.bytes += int64(n)
if i.bytes-i.lastProgress >= progressReaderIncrement || err == io.EOF {
i.lastProgress = i.bytes
i.out <- &AddedObject{
Name: i.file.FileName(),
Bytes: i.bytes,
}
}
return n, err
}