From 4716b70d56e9861d6480c42696093603ddd5f2a9 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sun, 19 Jun 2016 02:39:57 +0200 Subject: [PATCH] coreapi: start implementing it License: MIT Signed-off-by: Lars Gierth --- core/coreapi/coreapi.go | 64 +++++++++++++++++++++++++++++ core/coreapi/interface/interface.go | 38 +++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 core/coreapi/coreapi.go create mode 100644 core/coreapi/interface/interface.go diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go new file mode 100644 index 00000000000..55c71318eab --- /dev/null +++ b/core/coreapi/coreapi.go @@ -0,0 +1,64 @@ +package coreapi + +import ( + core "github.com/ipfs/go-ipfs/core" + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + dag "github.com/ipfs/go-ipfs/merkledag" + path "github.com/ipfs/go-ipfs/path" + uio "github.com/ipfs/go-ipfs/unixfs/io" + context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" +) + +type CoreAPI struct { + ctx context.Context + node *core.IpfsNode +} + +func NewCoreAPI(ctx context.Context, node *core.IpfsNode) (*CoreAPI, error) { + api := &CoreAPI{ctx: ctx, node: node} + return api, nil +} + +func (api *CoreAPI) Context() context.Context { + return api.ctx +} + +func (api *CoreAPI) IpfsNode() *core.IpfsNode { + return api.node +} + +func (api *CoreAPI) resolve(p string) (*dag.Node, error) { + dagnode, err := core.Resolve(api.ctx, api.node, path.Path(p)) + if err == core.ErrNoNamesys && !api.node.OnlineMode() { + return nil, coreiface.ErrOffline + } else if err != nil { + return nil, err + } + return dagnode, nil +} + +func (api *CoreAPI) Cat(p string) (coreiface.Data, error) { + dagnode, err := api.resolve(p) + if err != nil { + return nil, err + } + r, err := uio.NewDagReader(api.ctx, dagnode, api.node.DAG) + if err == uio.ErrIsDir { + return nil, coreiface.ErrDir + } else if err != nil { + return nil, err + } + return r, nil +} + +func (api *CoreAPI) Ls(p string) ([]coreiface.Link, error) { + dagnode, err := api.resolve(p) + if err != nil { + return nil, err + } + links := make([]coreiface.Link, len(dagnode.Links)) + for i, l := range dagnode.Links { + links[i] = coreiface.Link{Name: l.Name, Size: l.Size, Hash: l.Hash} + } + return links, nil +} diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go new file mode 100644 index 00000000000..4c40cc456c1 --- /dev/null +++ b/core/coreapi/interface/interface.go @@ -0,0 +1,38 @@ +package iface + +import ( + "errors" + "io" + + core "github.com/ipfs/go-ipfs/core" + + mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash" + context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" +) + +type CoreAPI interface { + Context() context.Context + IpfsNode() *core.IpfsNode // XXX temporary + Cat(string) (Data, error) + Ls(string) ([]Link, error) +} + +type Object struct { + Links []Link + Data Data +} + +type Link struct { + Name string // utf-8 + Size uint64 + Hash mh.Multihash +} + +type Data interface { + io.Reader + io.Seeker + io.Closer +} + +var ErrDir = errors.New("object is a directory") +var ErrOffline = errors.New("can't resolve, ipfs node is offline")