add: fix adding multiple files
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 25, 2019 at 15:03 UTC
0b9e89ddfeca5ab14fb710dac1dec1f1acdbc83a
1 file changed
+55
-43
core/commands/add.go
+55
-43
@@ -181,24 +181,26 @@ You can now check what blocks have been created by:
181
return err
182
}
183
184
- events := make(chan interface{}, adderOutChanSize)
185
-
186
- var toadd files.Node = req.Files
187
- name := ""
184
+ toadd := []files.Node{req.Files}
185
+ name := []string{""}
186
if !wrap {
187
+ toadd, name = nil, nil
188
+
189
+ // process all entries in case user adds multiple files
190
it := req.Files.Entries()
190
- if !it.Next() {
191
- err := it.Err()
192
- if err == nil {
193
- return fmt.Errorf("expected a file argument")
194
- }
191
+ for it.Next() {
192
+ toadd = append(toadd, it.Node())
193
+ name = append(name, it.Name())
194
+ }
195
+
196
+ if err := it.Err(); err != nil {
197
return err
198
}
199
198
- toadd = it.Node()
199
- name = it.Name()
200
+ if len(toadd) == 0 {
201
+ return fmt.Errorf("expected a file argument")
202
+ }
203
}
201
- _, dir := toadd.(files.Directory)
204
205
opts := []options.UnixfsAddOption{
206
options.Unixfs.Hash(hashFunCode),
@@ -215,7 +217,8 @@ You can now check what blocks have been created by:
217
218
options.Unixfs.Progress(progress),
219
options.Unixfs.Silent(silent),
218
- options.Unixfs.Events(events),
220
+
221
+ nil, // reserved for events, must be last
222
}
223
224
if cidVerSet {
@@ -230,42 +233,51 @@ You can now check what blocks have been created by:
233
opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
234
}
235
233
- errCh := make(chan error, 1)
234
- go func() {
235
- var err error
236
- defer func() { errCh <- err }()
237
- defer close(events)
238
- _, err = api.Unixfs().Add(req.Context, toadd, opts...)
239
- }()
240
-
241
- for event := range events {
242
- output, ok := event.(*coreiface.AddEvent)
243
- if !ok {
244
- return errors.New("unknown event type")
245
- }
236
+ for i := range toadd {
237
+ _, dir := toadd[i].(files.Directory)
238
+ errCh := make(chan error, 1)
239
+ events := make(chan interface{}, adderOutChanSize)
240
+ opts[len(opts)-1] = options.Unixfs.Events(events)
241
247
- h := ""
248
- if output.Path != nil {
249
- h = enc.Encode(output.Path.Cid())
250
- }
242
+ go func() {
243
+ var err error
244
+ defer func() { errCh <- err }()
245
+ defer close(events)
246
+ _, err = api.Unixfs().Add(req.Context, toadd[i], opts...)
247
+ }()
248
252
- if !dir && name != "" {
253
- output.Name = name
254
- } else {
255
- output.Name = path.Join(name, output.Name)
249
+ for event := range events {
250
+ output, ok := event.(*coreiface.AddEvent)
251
+ if !ok {
252
+ return errors.New("unknown event type")
253
+ }
254
+
255
+ h := ""
256
+ if output.Path != nil {
257
+ h = enc.Encode(output.Path.Cid())
258
+ }
259
+
260
+ if !dir && name[i] != "" {
261
+ output.Name = name[i]
262
+ } else {
263
+ output.Name = path.Join(name[i], output.Name)
264
+ }
265
+
266
+ if err := res.Emit(&AddEvent{
267
+ Name: output.Name,
268
+ Hash: h,
269
+ Bytes: output.Bytes,
270
+ Size: output.Size,
271
+ }); err != nil {
272
+ return err
273
+ }
274
}
275
258
- if err := res.Emit(&AddEvent{
259
- Name: output.Name,
260
- Hash: h,
261
- Bytes: output.Bytes,
262
- Size: output.Size,
263
- }); err != nil {
264
- return err
276
+ if err := <-errCh; err != nil {
277
+ return nil
278
}
279
}
267
-
268
- return <-errCh
280
+ return nil
281
},
282
PostRun: cmds.PostRunMap{
283
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {