From 98402e27a7899c5b386e5ebf7c99951053b9bc96 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Tue, 20 Sep 2016 04:30:43 +0200 Subject: [PATCH] coreapi: add Add() License: MIT Signed-off-by: Lars Gierth --- core/coreapi/interface/interface.go | 1 + core/coreapi/unixfs.go | 12 +++++ core/coreapi/unixfs_test.go | 71 ++++++++++++++++++++++++++--- core/coreunix/add.go | 4 ++ 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index 694a116a525..297e8bfede2 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -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) } diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 15b15fd2071..24977a4c238 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -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 { @@ -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 { diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index 63469f0383b..61270551045 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -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" @@ -41,6 +45,56 @@ 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) @@ -48,25 +102,28 @@ func TestCatBasic(t *testing.T) { 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) } } diff --git a/core/coreunix/add.go b/core/coreunix/add.go index 5e224808db6..5221b3472db 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -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)