@cryptotaxi247 / kubo / commits / 036ca3a76

coreapi: add Add()

License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>

Lars Gierth committed Sep 20, 2016 at 04:30 UTC 036ca3a7649f5daa8b9532c67d3f3a200485b657
4 files changed +81 -7
core/coreapi/interface/interface.go
+1
@@ -25,6 +25,7 @@ type Reader interface {
25 }
26
27 type UnixfsAPI interface {
28 + Add(context.Context, io.Reader) (*cid.Cid, error)
29 Cat(context.Context, string) (Reader, error)
30 Ls(context.Context, string) ([]*Link, error)
31 }
core/coreapi/unixfs.go
+12
@@ -2,10 +2,14 @@ package coreapi
2
3 import (
4 "context"
5 + "io"
6
7 core "github.com/ipfs/go-ipfs/core"
8 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
9 + coreunix "github.com/ipfs/go-ipfs/core/coreunix"
10 uio "github.com/ipfs/go-ipfs/unixfs/io"
11 +
12 + cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
13 )
14
15 type UnixfsAPI struct {
@@ -17,6 +21,14 @@ func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
21 return api
22 }
23
24 +func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
25 + k, err := coreunix.AddWithContext(ctx, api.node, r)
26 + if err != nil {
27 + return nil, err
28 + }
29 + return cid.Decode(k)
30 +}
31 +
32 func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, error) {
33 dagnode, err := resolve(ctx, api.node, p)
34 if err != nil {
core/coreapi/unixfs_test.go
+64 -7
@@ -18,6 +18,10 @@ import (
18 unixfs "github.com/ipfs/go-ipfs/unixfs"
19 )
20
21 +// `echo -n 'hello, world!' | ipfs add`
22 +var hello = "QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
23 +var helloStr = "hello, world!"
24 +
25 // `ipfs object new unixfs-dir`
26 var emptyUnixfsDir = "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
27
@@ -41,6 +45,56 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
45 return node, api, nil
46 }
47
48 +func TestAdd(t *testing.T) {
49 + ctx := context.Background()
50 + _, api, err := makeAPI(ctx)
51 + if err != nil {
52 + t.Error(err)
53 + }
54 +
55 + str := strings.NewReader(helloStr)
56 + c, err := api.Add(ctx, str)
57 + if err != nil {
58 + t.Error(err)
59 + }
60 +
61 + if c.String() != hello {
62 + t.Fatalf("expected CID %s, got: %s", hello, c)
63 + }
64 +
65 + r, err := api.Cat(ctx, hello)
66 + if err != nil {
67 + t.Fatal(err)
68 + }
69 + buf := make([]byte, len(helloStr))
70 + _, err = io.ReadFull(r, buf)
71 + if err != nil {
72 + t.Error(err)
73 + }
74 +
75 + if string(buf) != helloStr {
76 + t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
77 + }
78 +}
79 +
80 +func TestAddEmptyFile(t *testing.T) {
81 + ctx := context.Background()
82 + _, api, err := makeAPI(ctx)
83 + if err != nil {
84 + t.Error(err)
85 + }
86 +
87 + str := strings.NewReader("")
88 + c, err := api.Add(ctx, str)
89 + if err != nil {
90 + t.Error(err)
91 + }
92 +
93 + if c.String() != emptyUnixfsFile {
94 + t.Fatalf("expected CID %s, got: %s", hello, c)
95 + }
96 +}
97 +
98 func TestCatBasic(t *testing.T) {
99 ctx := context.Background()
100 node, api, err := makeAPI(ctx)
@@ -48,25 +102,28 @@ func TestCatBasic(t *testing.T) {
102 t.Fatal(err)
103 }
104
51 - hello := "hello, world!"
52 - hr := strings.NewReader(hello)
105 + hr := strings.NewReader(helloStr)
106 k, err := coreunix.Add(node, hr)
107 if err != nil {
108 t.Fatal(err)
109 }
110
111 + if k != hello {
112 + t.Fatalf("expected CID %s, got: %s", hello, k)
113 + }
114 +
115 r, err := api.Cat(ctx, k)
116 if err != nil {
117 t.Fatal(err)
118 }
119
63 - buf := make([]byte, len(hello))
64 - n, err := io.ReadFull(r, buf)
65 - if err != nil && err != io.EOF {
120 + buf := make([]byte, len(helloStr))
121 + _, err = io.ReadFull(r, buf)
122 + if err != nil {
123 t.Error(err)
124 }
68 - if string(buf) != hello {
69 - t.Fatalf("expected [hello, world!], got [%s] [err=%s]", string(buf), n, err)
125 + if string(buf) != helloStr {
126 + t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
127 }
128 }
129
core/coreunix/add.go
+4
@@ -254,6 +254,10 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
254 // Add builds a merkledag from the a reader, pinning all objects to the local
255 // datastore. Returns a key representing the root node.
256 func Add(n *core.IpfsNode, r io.Reader) (string, error) {
257 + return AddWithContext(n.Context(), n, r)
258 +}
259 +
260 +func AddWithContext(ctx context.Context, n *core.IpfsNode, r io.Reader) (string, error) {
261 defer n.Blockstore.PinLock().Unlock()
262
263 fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)