@cryptotaxi247 / kubo / commits / aa0704e61

add: fix wrap with multiple files

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Apr 25, 2019 at 15:23 UTC aa0704e61a3413df62a92215e853cc752db90d7b
1 file changed +23 -29
core/commands/add.go
+23 -29
@@ -181,25 +181,11 @@ You can now check what blocks have been created by:
181 return err
182 }
183
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()
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 -
200 - if len(toadd) == 0 {
201 - return fmt.Errorf("expected a file argument")
202 - }
184 + toadd := req.Files
185 + if wrap {
186 + toadd = files.NewSliceDirectory([]files.DirEntry{
187 + files.FileEntry("", req.Files),
188 + })
189 }
190
191 opts := []options.UnixfsAddOption{
@@ -217,8 +203,6 @@ You can now check what blocks have been created by:
203
204 options.Unixfs.Progress(progress),
205 options.Unixfs.Silent(silent),
220 -
221 - nil, // reserved for events, must be last
206 }
207
208 if cidVerSet {
@@ -233,17 +217,21 @@ You can now check what blocks have been created by:
217 opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
218 }
219
236 - for i := range toadd {
237 - _, dir := toadd[i].(files.Directory)
220 + opts = append(opts, nil) // events option placeholder
221 +
222 + var added int
223 + addit := toadd.Entries()
224 + for addit.Next() {
225 + _, dir := addit.Node().(files.Directory)
226 errCh := make(chan error, 1)
227 events := make(chan interface{}, adderOutChanSize)
228 opts[len(opts)-1] = options.Unixfs.Events(events)
229
230 go func() {
231 var err error
244 - defer func() { errCh <- err }()
232 defer close(events)
246 - _, err = api.Unixfs().Add(req.Context, toadd[i], opts...)
233 + _, err = api.Unixfs().Add(req.Context, addit.Node(), opts...)
234 + errCh <- err
235 }()
236
237 for event := range events {
@@ -257,10 +245,10 @@ You can now check what blocks have been created by:
245 h = enc.Encode(output.Path.Cid())
246 }
247
260 - if !dir && name[i] != "" {
261 - output.Name = name[i]
248 + if !dir && addit.Name() != "" {
249 + output.Name = addit.Name()
250 } else {
263 - output.Name = path.Join(name[i], output.Name)
251 + output.Name = path.Join(addit.Name(), output.Name)
252 }
253
254 if err := res.Emit(&AddEvent{
@@ -274,9 +262,15 @@ You can now check what blocks have been created by:
262 }
263
264 if err := <-errCh; err != nil {
277 - return nil
265 + return err
266 }
267 + added++
268 }
269 +
270 + if added == 0 {
271 + return fmt.Errorf("expected a file argument")
272 + }
273 +
274 return nil
275 },
276 PostRun: cmds.PostRunMap{