Skip to content

Commit ee7029f

Browse files
committed
coreapi/unixfs: Use path instead of raw hash in AddEvent
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 5866e6a commit ee7029f

File tree

7 files changed

+63
-43
lines changed

7 files changed

+63
-43
lines changed

core/commands/add.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -20,6 +21,13 @@ import (
2021
// ErrDepthLimitExceeded indicates that the max depth has been exceeded.
2122
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
2223

24+
type AddEvent struct {
25+
Name string
26+
Hash string `json:",omitempty"`
27+
Bytes int64 `json:",omitempty"`
28+
Size string `json:",omitempty"`
29+
}
30+
2331
const (
2432
quietOptionName = "quiet"
2533
quieterOptionName = "quieter"
@@ -213,6 +221,20 @@ You can now check what blocks have been created by:
213221
_, err = api.Unixfs().Add(req.Context, req.Files, opts...)
214222
}()
215223

224+
for event := range events {
225+
output, ok := event.(*coreiface.AddEvent)
226+
if !ok {
227+
return errors.New("unknown event type")
228+
}
229+
230+
res.Emit(&AddEvent{
231+
Name: output.Name,
232+
Hash: output.Path.Cid().String(),
233+
Bytes: output.Bytes,
234+
Size: output.Size,
235+
})
236+
}
237+
216238
err = res.Emit(events)
217239
if err != nil {
218240
return err
@@ -279,7 +301,7 @@ You can now check what blocks have been created by:
279301

280302
break LOOP
281303
}
282-
output := out.(*coreiface.AddEvent)
304+
output := out.(*AddEvent)
283305
if len(output.Hash) > 0 {
284306
lastHash = output.Hash
285307
if quieter {
@@ -367,5 +389,5 @@ You can now check what blocks have been created by:
367389
}
368390
},
369391
},
370-
Type: coreiface.AddEvent{},
392+
Type: AddEvent{},
371393
}

core/commands/tar.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
core "github.com/ipfs/go-ipfs/core"
88
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9-
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
109
tar "github.com/ipfs/go-ipfs/tar"
1110

1211
cmds "gx/ipfs/QmPdvMtgpnMuU68mWhGtzCxnddXJoV96tT9aPcNbQsqPaM/go-ipfs-cmds"
@@ -57,14 +56,14 @@ represent it.
5756
c := node.Cid()
5857

5958
fi.FileName()
60-
return cmds.EmitOnce(res, &coreiface.AddEvent{
59+
return cmds.EmitOnce(res, &AddEvent{
6160
Name: fi.FileName(),
6261
Hash: c.String(),
6362
})
6463
},
65-
Type: coreiface.AddEvent{},
64+
Type: AddEvent{},
6665
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 {
6867
fmt.Fprintln(w, out.Hash)
6968
return nil
7069
}),

core/coreapi/interface/path.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type ResolvedPath interface {
4646
// cidRoot := {"A": {"/": cidA }}
4747
//
4848
// And resolve paths:
49+
//
4950
// * "/ipfs/${cidRoot}"
5051
// * Calling Cid() will return `cidRoot`
5152
// * Calling Root() will return `cidRoot`

core/coreapi/interface/unixfs.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
1111
)
1212

13-
// TODO: ideas on making this more coreapi-ish without breaking the http API?
1413
type AddEvent struct {
1514
Name string
16-
Hash string `json:",omitempty"`
17-
Bytes int64 `json:",omitempty"`
18-
Size string `json:",omitempty"`
15+
Path ResolvedPath `json:",omitempty"`
16+
Bytes int64 `json:",omitempty"`
17+
Size string `json:",omitempty"`
1918
}
2019

2120
type UnixfsFile interface {

core/coreapi/unixfs_test.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/base64"
77
"fmt"
8+
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
89
"io"
910
"io/ioutil"
1011
"math"
@@ -172,6 +173,14 @@ func TestAdd(t *testing.T) {
172173
t.Error(err)
173174
}
174175

176+
p := func(h string) coreiface.ResolvedPath {
177+
c, err := cid.Parse(h)
178+
if err != nil {
179+
t.Fatal(err)
180+
}
181+
return coreiface.IpfsPath(c)
182+
}
183+
175184
cases := []struct {
176185
name string
177186
data func() files.File
@@ -384,7 +393,7 @@ func TestAdd(t *testing.T) {
384393
data: strFile(helloStr),
385394
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
386395
events: []coreiface.AddEvent{
387-
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Hash: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Size: strconv.Itoa(len(helloStr))},
396+
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Path: p("zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd"), Size: strconv.Itoa(len(helloStr))},
388397
},
389398
opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)},
390399
},
@@ -393,8 +402,8 @@ func TestAdd(t *testing.T) {
393402
data: twoLevelDir(),
394403
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
395404
events: []coreiface.AddEvent{
396-
{Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"},
397-
{Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"},
405+
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
406+
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
398407
},
399408
recursive: true,
400409
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
@@ -404,11 +413,11 @@ func TestAdd(t *testing.T) {
404413
data: twoLevelDir(),
405414
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
406415
events: []coreiface.AddEvent{
407-
{Name: "t/abc/def", Hash: "QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk", Size: "13"},
408-
{Name: "t/bar", Hash: "QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY", Size: "14"},
409-
{Name: "t/foo", Hash: "QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ", Size: "14"},
410-
{Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"},
411-
{Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"},
416+
{Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
417+
{Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
418+
{Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
419+
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
420+
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
412421
},
413422
recursive: true,
414423
},
@@ -424,7 +433,7 @@ func TestAdd(t *testing.T) {
424433
{Name: "", Bytes: 524288},
425434
{Name: "", Bytes: 786432},
426435
{Name: "", Bytes: 1000000},
427-
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Hash: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Size: "1000256"},
436+
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"},
428437
},
429438
recursive: true,
430439
opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)},
@@ -476,8 +485,12 @@ func TestAdd(t *testing.T) {
476485
t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name)
477486
}
478487

479-
if expected[0].Hash != event.Hash {
480-
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Hash, event.Hash)
488+
if expected[0].Path != nil && event.Path != nil {
489+
if expected[0].Path.Cid().String() != event.Path.Cid().String() {
490+
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
491+
}
492+
} else if event.Path != expected[0].Path {
493+
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
481494
}
482495
if expected[0].Bytes != event.Bytes {
483496
t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes)

core/coreunix/add.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ type Link struct {
4141
Size uint64
4242
}
4343

44-
type Object struct {
45-
Hash string
46-
Links []Link
47-
Size string
48-
}
49-
5044
// NewAdder Returns a new Adder used for a file add operation.
5145
func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds ipld.DAGService) (*Adder, error) {
5246
bufferedDS := ipld.NewBufferedDAG(ctx, ds)
@@ -582,7 +576,7 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
582576
}
583577

584578
out <- &coreiface.AddEvent{
585-
Hash: o.Hash,
579+
Path: o.Path,
586580
Name: name,
587581
Size: o.Size,
588582
}
@@ -591,24 +585,16 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
591585
}
592586

593587
// from core/commands/object.go
594-
func getOutput(dagnode ipld.Node) (*Object, error) {
588+
func getOutput(dagnode ipld.Node) (*coreiface.AddEvent, error) {
595589
c := dagnode.Cid()
596590
s, err := dagnode.Size()
597591
if err != nil {
598592
return nil, err
599593
}
600594

601-
output := &Object{
602-
Hash: c.String(),
603-
Size: strconv.FormatUint(s, 10),
604-
Links: make([]Link, len(dagnode.Links())),
605-
}
606-
607-
for i, link := range dagnode.Links() {
608-
output.Links[i] = Link{
609-
Name: link.Name,
610-
Size: link.Size,
611-
}
595+
output := &coreiface.AddEvent{
596+
Path: coreiface.IpfsPath(c),
597+
Size: strconv.FormatUint(s, 10),
612598
}
613599

614600
return output, nil

core/coreunix/add_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestAddGCLive(t *testing.T) {
9797
addedHashes := make(map[string]struct{})
9898
select {
9999
case o := <-out:
100-
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
100+
addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
101101
case <-addDone:
102102
t.Fatal("add shouldnt complete yet")
103103
}
@@ -125,7 +125,7 @@ func TestAddGCLive(t *testing.T) {
125125

126126
// receive next object from adder
127127
o := <-out
128-
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
128+
addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
129129

130130
<-gcstarted
131131

@@ -141,7 +141,7 @@ func TestAddGCLive(t *testing.T) {
141141
var last cid.Cid
142142
for a := range out {
143143
// wait for it to finish
144-
c, err := cid.Decode(a.(*coreiface.AddEvent).Hash)
144+
c, err := cid.Decode(a.(*coreiface.AddEvent).Path.Cid().String())
145145
if err != nil {
146146
t.Fatal(err)
147147
}

0 commit comments

Comments
 (0)