Skip to content

Commit 5dfdf33

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

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"
@@ -19,6 +20,13 @@ import (
1920
// ErrDepthLimitExceeded indicates that the max depth has been exceeded.
2021
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
2122

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

221+
for event := range events {
222+
output, ok := event.(*coreiface.AddEvent)
223+
if !ok {
224+
return errors.New("unknown event type")
225+
}
226+
227+
res.Emit(&AddEvent{
228+
Name: output.Name,
229+
Hash: output.Path.Cid().String(),
230+
Bytes: output.Bytes,
231+
Size: output.Size,
232+
})
233+
}
234+
213235
err = res.Emit(events)
214236
if err != nil {
215237
return err
@@ -269,7 +291,7 @@ You can now check what blocks have been created by:
269291

270292
break LOOP
271293
}
272-
output := out.(*coreiface.AddEvent)
294+
output := out.(*AddEvent)
273295
if len(output.Hash) > 0 {
274296
lastHash = output.Hash
275297
if quieter {
@@ -357,5 +379,5 @@ You can now check what blocks have been created by:
357379
}
358380
},
359381
},
360-
Type: coreiface.AddEvent{},
382+
Type: AddEvent{},
361383
}

core/commands/tar.go

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

77
"github.com/ipfs/go-ipfs/core"
88
"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
"gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
@@ -57,14 +56,14 @@ represent it.
5756

5857
c := node.Cid()
5958

60-
return cmds.EmitOnce(res, &coreiface.AddEvent{
59+
return cmds.EmitOnce(res, &AddEvent{
6160
Name: it.Name(),
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
@@ -9,12 +9,11 @@ import (
99
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
1010
)
1111

12-
// TODO: ideas on making this more coreapi-ish without breaking the http API?
1312
type AddEvent struct {
1413
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"`
1817
}
1918

2019
// UnixfsAPI is the basic interface to immutable files in IPFS

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"
@@ -178,6 +179,14 @@ func TestAdd(t *testing.T) {
178179
t.Error(err)
179180
}
180181

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+
181190
cases := []struct {
182191
name string
183192
data func() files.Node
@@ -406,7 +415,7 @@ func TestAdd(t *testing.T) {
406415
data: strFile(helloStr),
407416
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
408417
events: []coreiface.AddEvent{
409-
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Hash: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Size: strconv.Itoa(len(helloStr))},
418+
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Path: p("zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd"), Size: strconv.Itoa(len(helloStr))},
410419
},
411420
opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)},
412421
},
@@ -415,8 +424,8 @@ func TestAdd(t *testing.T) {
415424
data: twoLevelDir(),
416425
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
417426
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"},
420429
},
421430
wrap: "t",
422431
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
@@ -426,11 +435,11 @@ func TestAdd(t *testing.T) {
426435
data: twoLevelDir(),
427436
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
428437
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"},
434443
},
435444
wrap: "t",
436445
},
@@ -445,7 +454,7 @@ func TestAdd(t *testing.T) {
445454
{Name: "", Bytes: 524288},
446455
{Name: "", Bytes: 786432},
447456
{Name: "", Bytes: 1000000},
448-
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Hash: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Size: "1000256"},
457+
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"},
449458
},
450459
wrap: "",
451460
opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)},
@@ -497,8 +506,12 @@ func TestAdd(t *testing.T) {
497506
t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name)
498507
}
499508

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)
502515
}
503516
if expected[0].Bytes != event.Bytes {
504517
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.GCLocker, ds ipld.DAGService) (*Adder, error) {
5246
bufferedDS := ipld.NewBufferedDAG(ctx, ds)
@@ -580,7 +574,7 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
580574
}
581575

582576
out <- &coreiface.AddEvent{
583-
Hash: o.Hash,
577+
Path: o.Path,
584578
Name: name,
585579
Size: o.Size,
586580
}
@@ -589,24 +583,16 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
589583
}
590584

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

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),
610596
}
611597

612598
return output, nil

core/coreunix/add_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestAddGCLive(t *testing.T) {
100100
addedHashes := make(map[string]struct{})
101101
select {
102102
case o := <-out:
103-
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
103+
addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
104104
case <-addDone:
105105
t.Fatal("add shouldnt complete yet")
106106
}
@@ -128,7 +128,7 @@ func TestAddGCLive(t *testing.T) {
128128

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

133133
<-gcstarted
134134

@@ -144,7 +144,7 @@ func TestAddGCLive(t *testing.T) {
144144
var last cid.Cid
145145
for a := range out {
146146
// 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())
148148
if err != nil {
149149
t.Fatal(err)
150150
}

0 commit comments

Comments
 (0)