Skip to content

Commit

Permalink
coreapi: add Add()
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <[email protected]>
  • Loading branch information
Lars Gierth committed Oct 28, 2016
1 parent 9917eb3 commit 98402e2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Reader interface {
}

type UnixfsAPI interface {
Add(context.Context, io.Reader) (*cid.Cid, error)
Cat(context.Context, string) (Reader, error)
Ls(context.Context, string) ([]*Link, error)
}
Expand Down
12 changes: 12 additions & 0 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package coreapi

import (
"context"
"io"

core "github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mdag "github.com/ipfs/go-ipfs/merkledag"
uio "github.com/ipfs/go-ipfs/unixfs/io"

cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
)

type UnixfsAPI struct {
Expand All @@ -18,6 +22,14 @@ func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
return api
}

func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
k, err := coreunix.AddWithContext(ctx, api.node, r)
if err != nil {
return nil, err
}
return cid.Decode(k)
}

func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, error) {
dagnode, err := resolve(ctx, api.node, p)
if err != nil {
Expand Down
71 changes: 64 additions & 7 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
unixfs "github.com/ipfs/go-ipfs/unixfs"
)

// `echo -n 'hello, world!' | ipfs add`
var hello = "QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
var helloStr = "hello, world!"

// `ipfs object new unixfs-dir`
var emptyUnixfsDir = "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"

Expand All @@ -41,32 +45,85 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
return node, api, nil
}

func TestAdd(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

str := strings.NewReader(helloStr)
c, err := api.Add(ctx, str)
if err != nil {
t.Error(err)
}

if c.String() != hello {
t.Fatalf("expected CID %s, got: %s", hello, c)
}

r, err := api.Cat(ctx, hello)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, len(helloStr))
_, err = io.ReadFull(r, buf)
if err != nil {
t.Error(err)
}

if string(buf) != helloStr {
t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
}
}

func TestAddEmptyFile(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

str := strings.NewReader("")
c, err := api.Add(ctx, str)
if err != nil {
t.Error(err)
}

if c.String() != emptyUnixfsFile {
t.Fatalf("expected CID %s, got: %s", hello, c)
}
}

func TestCatBasic(t *testing.T) {
ctx := context.Background()
node, api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}

hello := "hello, world!"
hr := strings.NewReader(hello)
hr := strings.NewReader(helloStr)
k, err := coreunix.Add(node, hr)
if err != nil {
t.Fatal(err)
}

if k != hello {
t.Fatalf("expected CID %s, got: %s", hello, k)
}

r, err := api.Cat(ctx, k)
if err != nil {
t.Fatal(err)
}

buf := make([]byte, len(hello))
n, err := io.ReadFull(r, buf)
if err != nil && err != io.EOF {
buf := make([]byte, len(helloStr))
_, err = io.ReadFull(r, buf)
if err != nil {
t.Error(err)
}
if string(buf) != hello {
t.Fatalf("expected [hello, world!], got [%s] [err=%s]", string(buf), n, err)
if string(buf) != helloStr {
t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
}
}

Expand Down
4 changes: 4 additions & 0 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
// Add builds a merkledag from the a reader, pinning all objects to the local
// datastore. Returns a key representing the root node.
func Add(n *core.IpfsNode, r io.Reader) (string, error) {
return AddWithContext(n.Context(), n, r)
}

func AddWithContext(ctx context.Context, n *core.IpfsNode, r io.Reader) (string, error) {
defer n.Blockstore.PinLock().Unlock()

fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
Expand Down

0 comments on commit 98402e2

Please sign in to comment.