coreapi/unixfs: Use path instead of raw hash in AddEvent
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 18, 2018 at 14:23 UTC
24e317603af74d59d1ff334e440a6c5b440ffd8f
7 files changed
+66
-46
core/commands/add.go
+27
-5
@@ -1,6 +1,7 @@
1
package commands
2
3
import (
4
+ "errors"
5
"fmt"
6
"io"
7
"os"
@@ -19,6 +20,13 @@ import (
20
// ErrDepthLimitExceeded indicates that the max depth has been exceeded.
21
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
22
23
+type AddEvent struct {
24
+ Name string
25
+ Hash string `json:",omitempty"`
26
+ Bytes int64 `json:",omitempty"`
27
+ Size string `json:",omitempty"`
28
+}
29
+
30
const (
31
quietOptionName = "quiet"
32
quieterOptionName = "quieter"
@@ -210,9 +218,23 @@ You can now check what blocks have been created by:
218
_, err = api.Unixfs().Add(req.Context, req.Files, opts...)
219
}()
220
213
- err = res.Emit(events)
214
- if err != nil {
215
- return err
221
+ for event := range events {
222
+ output, ok := event.(*coreiface.AddEvent)
223
+ if !ok {
224
+ return errors.New("unknown event type")
225
+ }
226
+
227
+ h := ""
228
+ if output.Path != nil {
229
+ h = output.Path.Cid().String()
230
+ }
231
+
232
+ res.Emit(&AddEvent{
233
+ Name: output.Name,
234
+ Hash: h,
235
+ Bytes: output.Bytes,
236
+ Size: output.Size,
237
+ })
238
}
239
240
return <-errCh
@@ -269,7 +291,7 @@ You can now check what blocks have been created by:
291
292
break LOOP
293
}
272
- output := out.(*coreiface.AddEvent)
294
+ output := out.(*AddEvent)
295
if len(output.Hash) > 0 {
296
lastHash = output.Hash
297
if quieter {
@@ -357,5 +379,5 @@ You can now check what blocks have been created by:
379
}
380
},
381
},
360
- Type: coreiface.AddEvent{},
382
+ Type: AddEvent{},
383
}
core/commands/tar.go
+3
-4
@@ -6,7 +6,6 @@ import (
6
7
"github.com/ipfs/go-ipfs/core"
8
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
- coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
9
tar "github.com/ipfs/go-ipfs/tar"
10
11
"gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
@@ -57,14 +56,14 @@ represent it.
56
57
c := node.Cid()
58
60
- return cmds.EmitOnce(res, &coreiface.AddEvent{
59
+ return cmds.EmitOnce(res, &AddEvent{
60
Name: it.Name(),
61
Hash: c.String(),
62
})
63
},
65
- Type: coreiface.AddEvent{},
64
+ Type: AddEvent{},
65
Encoders: cmds.EncoderMap{
67
- cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *coreiface.AddEvent) error {
66
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *AddEvent) error {
67
fmt.Fprintln(w, out.Hash)
68
return nil
69
}),
core/coreapi/interface/path.go
+1
@@ -46,6 +46,7 @@ type ResolvedPath interface {
46
// cidRoot := {"A": {"/": cidA }}
47
//
48
// And resolve paths:
49
+ //
50
// * "/ipfs/${cidRoot}"
51
// * Calling Cid() will return `cidRoot`
52
// * Calling Root() will return `cidRoot`
core/coreapi/interface/unixfs.go
+3
-4
@@ -9,12 +9,11 @@ import (
9
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
10
)
11
12
-// TODO: ideas on making this more coreapi-ish without breaking the http API?
12
type AddEvent struct {
13
Name string
15
- Hash string `json:",omitempty"`
16
- Bytes int64 `json:",omitempty"`
17
- Size string `json:",omitempty"`
14
+ Path ResolvedPath `json:",omitempty"`
15
+ Bytes int64 `json:",omitempty"`
16
+ Size string `json:",omitempty"`
17
}
18
19
// UnixfsAPI is the basic interface to immutable files in IPFS
core/coreapi/unixfs_test.go
+24
-11
@@ -5,6 +5,7 @@ import (
5
"context"
6
"encoding/base64"
7
"fmt"
8
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
9
"io"
10
"io/ioutil"
11
"math"
@@ -178,6 +179,14 @@ func TestAdd(t *testing.T) {
179
t.Error(err)
180
}
181
182
+ p := func(h string) coreiface.ResolvedPath {
183
+ c, err := cid.Parse(h)
184
+ if err != nil {
185
+ t.Fatal(err)
186
+ }
187
+ return coreiface.IpfsPath(c)
188
+ }
189
+
190
cases := []struct {
191
name string
192
data func() files.Node
@@ -406,7 +415,7 @@ func TestAdd(t *testing.T) {
415
data: strFile(helloStr),
416
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
417
events: []coreiface.AddEvent{
409
- {Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Hash: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Size: strconv.Itoa(len(helloStr))},
418
+ {Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Path: p("zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd"), Size: strconv.Itoa(len(helloStr))},
419
},
420
opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)},
421
},
@@ -415,8 +424,8 @@ func TestAdd(t *testing.T) {
424
data: twoLevelDir(),
425
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
426
events: []coreiface.AddEvent{
418
- {Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"},
419
- {Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"},
427
+ {Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
428
+ {Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
429
},
430
wrap: "t",
431
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
@@ -426,11 +435,11 @@ func TestAdd(t *testing.T) {
435
data: twoLevelDir(),
436
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
437
events: []coreiface.AddEvent{
429
- {Name: "t/abc/def", Hash: "QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk", Size: "13"},
430
- {Name: "t/bar", Hash: "QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY", Size: "14"},
431
- {Name: "t/foo", Hash: "QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ", Size: "14"},
432
- {Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"},
433
- {Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"},
438
+ {Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
439
+ {Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
440
+ {Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
441
+ {Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
442
+ {Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
443
},
444
wrap: "t",
445
},
@@ -445,7 +454,7 @@ func TestAdd(t *testing.T) {
454
{Name: "", Bytes: 524288},
455
{Name: "", Bytes: 786432},
456
{Name: "", Bytes: 1000000},
448
- {Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Hash: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Size: "1000256"},
457
+ {Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"},
458
},
459
wrap: "",
460
opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)},
@@ -497,8 +506,12 @@ func TestAdd(t *testing.T) {
506
t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name)
507
}
508
500
- if expected[0].Hash != event.Hash {
501
- t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Hash, event.Hash)
509
+ if expected[0].Path != nil && event.Path != nil {
510
+ if expected[0].Path.Cid().String() != event.Path.Cid().String() {
511
+ t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
512
+ }
513
+ } else if event.Path != expected[0].Path {
514
+ t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
515
}
516
if expected[0].Bytes != event.Bytes {
517
t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes)
core/coreunix/add.go
+5
-19
@@ -41,12 +41,6 @@ type Link struct {
41
Size uint64
42
}
43
44
-type Object struct {
45
- Hash string
46
- Links []Link
47
- Size string
48
-}
49
-
44
// NewAdder Returns a new Adder used for a file add operation.
45
func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCLocker, ds ipld.DAGService) (*Adder, error) {
46
bufferedDS := ipld.NewBufferedDAG(ctx, ds)
@@ -580,7 +574,7 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
574
}
575
576
out <- &coreiface.AddEvent{
583
- Hash: o.Hash,
577
+ Path: o.Path,
578
Name: name,
579
Size: o.Size,
580
}
@@ -589,24 +583,16 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
583
}
584
585
// from core/commands/object.go
592
-func getOutput(dagnode ipld.Node) (*Object, error) {
586
+func getOutput(dagnode ipld.Node) (*coreiface.AddEvent, error) {
587
c := dagnode.Cid()
588
s, err := dagnode.Size()
589
if err != nil {
590
return nil, err
591
}
592
599
- output := &Object{
600
- Hash: c.String(),
601
- Size: strconv.FormatUint(s, 10),
602
- Links: make([]Link, len(dagnode.Links())),
603
- }
604
-
605
- for i, link := range dagnode.Links() {
606
- output.Links[i] = Link{
607
- Name: link.Name,
608
- Size: link.Size,
609
- }
593
+ output := &coreiface.AddEvent{
594
+ Path: coreiface.IpfsPath(c),
595
+ Size: strconv.FormatUint(s, 10),
596
}
597
598
return output, nil
core/coreunix/add_test.go
+3
-3
@@ -100,7 +100,7 @@ func TestAddGCLive(t *testing.T) {
100
addedHashes := make(map[string]struct{})
101
select {
102
case o := <-out:
103
- addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
103
+ addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
104
case <-addDone:
105
t.Fatal("add shouldnt complete yet")
106
}
@@ -128,7 +128,7 @@ func TestAddGCLive(t *testing.T) {
128
129
// receive next object from adder
130
o := <-out
131
- addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
131
+ addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
132
133
<-gcstarted
134
@@ -144,7 +144,7 @@ func TestAddGCLive(t *testing.T) {
144
var last cid.Cid
145
for a := range out {
146
// wait for it to finish
147
- c, err := cid.Decode(a.(*coreiface.AddEvent).Hash)
147
+ c, err := cid.Decode(a.(*coreiface.AddEvent).Path.Cid().String())
148
if err != nil {
149
t.Fatal(err)
150
}