mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 01:52:26 +08:00
make code-climate happier
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
This commit is contained in:
@ -22,6 +22,8 @@ var log = logging.Logger("blockservice")
|
||||
|
||||
var ErrNotFound = errors.New("blockservice: key not found")
|
||||
|
||||
// BlockGetter is the common interface shared between blockservice sessions and
|
||||
// the blockservice.
|
||||
type BlockGetter interface {
|
||||
// GetBlock gets the requested block.
|
||||
GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error)
|
||||
@ -95,12 +97,14 @@ func NewWriteThrough(bs blockstore.Blockstore, rem exchange.Interface) BlockServ
|
||||
}
|
||||
}
|
||||
|
||||
func (bs *blockService) Blockstore() blockstore.Blockstore {
|
||||
return bs.blockstore
|
||||
// Blockstore returns the blockstore behind this blockservice.
|
||||
func (s *blockService) Blockstore() blockstore.Blockstore {
|
||||
return s.blockstore
|
||||
}
|
||||
|
||||
func (bs *blockService) Exchange() exchange.Interface {
|
||||
return bs.exchange
|
||||
// Exchange returns the exchange behind this blockservice.
|
||||
func (s *blockService) Exchange() exchange.Interface {
|
||||
return s.exchange
|
||||
}
|
||||
|
||||
// NewSession creates a bitswap session that allows for controlled exchange of
|
||||
@ -286,3 +290,5 @@ func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error
|
||||
func (s *Session) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
|
||||
return getBlocks(ctx, ks, s.bs, s.ses)
|
||||
}
|
||||
|
||||
var _ BlockGetter = (*Session)(nil)
|
||||
|
@ -65,10 +65,12 @@ func (n *UnixfsNode) SetPrefix(prefix *cid.Prefix) {
|
||||
n.node.SetPrefix(prefix)
|
||||
}
|
||||
|
||||
// NumChildren returns the number of children referenced by this UnixfsNode.
|
||||
func (n *UnixfsNode) NumChildren() int {
|
||||
return n.ufmt.NumChildren()
|
||||
}
|
||||
|
||||
// Set replaces this UnixfsNode with another UnixfsNode
|
||||
func (n *UnixfsNode) Set(other *UnixfsNode) {
|
||||
n.node = other.node
|
||||
n.raw = other.raw
|
||||
@ -78,6 +80,7 @@ func (n *UnixfsNode) Set(other *UnixfsNode) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetChild gets the ith child of this node from the given DAGService.
|
||||
func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds node.DAGService) (*UnixfsNode, error) {
|
||||
nd, err := n.node.Links()[i].GetNode(ctx, ds)
|
||||
if err != nil {
|
||||
@ -92,8 +95,8 @@ func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds node.DAGService) (*
|
||||
return NewUnixfsNodeFromDag(pbn)
|
||||
}
|
||||
|
||||
// addChild will add the given UnixfsNode as a child of the receiver.
|
||||
// the passed in DagBuilderHelper is used to store the child node an
|
||||
// AddChild adds the given UnixfsNode as a child of the receiver.
|
||||
// The passed in DagBuilderHelper is used to store the child node an
|
||||
// pin it locally so it doesnt get lost
|
||||
func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
|
||||
n.ufmt.AddBlockSize(child.FileSize())
|
||||
@ -115,7 +118,7 @@ func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Removes the child node at the given index
|
||||
// RemoveChild removes the child node at the given index
|
||||
func (n *UnixfsNode) RemoveChild(index int, dbh *DagBuilderHelper) {
|
||||
n.ufmt.RemoveBlockSize(index)
|
||||
n.node.SetLinks(append(n.node.Links()[:index], n.node.Links()[index+1:]...))
|
||||
@ -140,7 +143,7 @@ func (n *UnixfsNode) SetPosInfo(offset uint64, fullPath string, stat os.FileInfo
|
||||
}
|
||||
}
|
||||
|
||||
// getDagNode fills out the proper formatting for the unixfs node
|
||||
// GetDagNode fills out the proper formatting for the unixfs node
|
||||
// inside of a DAG node and returns the dag node
|
||||
func (n *UnixfsNode) GetDagNode() (node.Node, error) {
|
||||
nd, err := n.getBaseDagNode()
|
||||
|
@ -1,5 +1,5 @@
|
||||
// package importer implements utilities used to create IPFS DAGs from files
|
||||
// and readers
|
||||
// Package importer implements utilities used to create IPFS DAGs from files
|
||||
// and readers.
|
||||
package importer
|
||||
|
||||
import (
|
||||
@ -15,8 +15,8 @@ import (
|
||||
node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
|
||||
)
|
||||
|
||||
// Builds a DAG from the given file, writing created blocks to disk as they are
|
||||
// created
|
||||
// BuildDagFromFile builds a DAG from the given file, writing created blocks to
|
||||
// disk as they are created
|
||||
func BuildDagFromFile(fpath string, ds node.DAGService) (node.Node, error) {
|
||||
stat, err := os.Lstat(fpath)
|
||||
if err != nil {
|
||||
@ -36,6 +36,8 @@ func BuildDagFromFile(fpath string, ds node.DAGService) (node.Node, error) {
|
||||
return BuildDagFromReader(ds, chunk.DefaultSplitter(f))
|
||||
}
|
||||
|
||||
// BuildDagFromReader builds a DAG from the chunks returned by the given chunk
|
||||
// splitter.
|
||||
func BuildDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, error) {
|
||||
dbp := h.DagBuilderParams{
|
||||
Dagserv: ds,
|
||||
@ -45,6 +47,7 @@ func BuildDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, erro
|
||||
return bal.BalancedLayout(dbp.New(spl))
|
||||
}
|
||||
|
||||
// BuildTrickleDagFromReader is similar to BuildDagFromReader but uses the trickle layout.
|
||||
func BuildTrickleDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, error) {
|
||||
dbp := h.DagBuilderParams{
|
||||
Dagserv: ds,
|
||||
|
@ -124,7 +124,7 @@ func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove a link on this node by the given name
|
||||
// RemoveNodeLink removes a link on this node by the given name.
|
||||
func (n *ProtoNode) RemoveNodeLink(name string) error {
|
||||
n.encoded = nil
|
||||
good := make([]*node.Link, 0, len(n.links))
|
||||
@ -146,7 +146,7 @@ func (n *ProtoNode) RemoveNodeLink(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Return a copy of the link with given name
|
||||
// GetNodeLink returns a copy of the link with the given name.
|
||||
func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
|
||||
for _, l := range n.links {
|
||||
if l.Name == name {
|
||||
@ -160,6 +160,7 @@ func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
|
||||
return nil, ErrLinkNotFound
|
||||
}
|
||||
|
||||
// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
|
||||
func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService, name string) (*ProtoNode, error) {
|
||||
nd, err := n.GetLinkedNode(ctx, ds, name)
|
||||
if err != nil {
|
||||
@ -174,6 +175,7 @@ func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService,
|
||||
return pbnd, nil
|
||||
}
|
||||
|
||||
// GetLinkedNode returns a copy of the IPLD Node with the given name.
|
||||
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds node.DAGService, name string) (node.Node, error) {
|
||||
lnk, err := n.GetNodeLink(name)
|
||||
if err != nil {
|
||||
|
@ -11,10 +11,12 @@ import (
|
||||
node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
|
||||
)
|
||||
|
||||
// Mock returns a new thread-safe, mock DAGService.
|
||||
func Mock() node.DAGService {
|
||||
return dag.NewDAGService(Bserv())
|
||||
}
|
||||
|
||||
// Bserv returns a new, thread-safe, mock BlockService.
|
||||
func Bserv() bsrv.BlockService {
|
||||
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
|
||||
return bsrv.New(bstore, offline.Exchange(bstore))
|
||||
|
@ -37,6 +37,7 @@ func (c *Change) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// ApplyChange applies the requested changes to the given node in the given dag.
|
||||
func ApplyChange(ctx context.Context, ds node.DAGService, nd *dag.ProtoNode, cs []*Change) (*dag.ProtoNode, error) {
|
||||
e := NewDagEditor(nd, ds)
|
||||
for _, c := range cs {
|
||||
|
@ -27,6 +27,7 @@ type Editor struct {
|
||||
src node.DAGService
|
||||
}
|
||||
|
||||
// NewMemoryDagService returns a new, thread-safe in-memory DAGService.
|
||||
func NewMemoryDagService() node.DAGService {
|
||||
// build mem-datastore for editor's intermediary nodes
|
||||
bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
|
||||
@ -34,7 +35,10 @@ func NewMemoryDagService() node.DAGService {
|
||||
return dag.NewDAGService(bsrv)
|
||||
}
|
||||
|
||||
// root is the node to be modified, source is the dagstore to pull nodes from (optional)
|
||||
// NewDagEditor returns an ProtoNode editor.
|
||||
//
|
||||
// * root is the node to be modified
|
||||
// * source is the dagstore to pull nodes from (optional)
|
||||
func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
|
||||
return &Editor{
|
||||
root: root,
|
||||
@ -43,17 +47,19 @@ func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
|
||||
}
|
||||
}
|
||||
|
||||
// GetNode returns the a copy of the root node being edited.
|
||||
func (e *Editor) GetNode() *dag.ProtoNode {
|
||||
return e.root.Copy().(*dag.ProtoNode)
|
||||
}
|
||||
|
||||
// GetDagService returns the DAGService used by this editor.
|
||||
func (e *Editor) GetDagService() node.DAGService {
|
||||
return e.tmp
|
||||
}
|
||||
|
||||
func addLink(ctx context.Context, ds node.DAGService, root *dag.ProtoNode, childname string, childnd node.Node) (*dag.ProtoNode, error) {
|
||||
if childname == "" {
|
||||
return nil, errors.New("cannot create link with no name!")
|
||||
return nil, errors.New("cannot create link with no name")
|
||||
}
|
||||
|
||||
// ensure that the node we are adding is in the dagservice
|
||||
@ -188,6 +194,8 @@ func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string)
|
||||
return root, nil
|
||||
}
|
||||
|
||||
// Finalize writes the new DAG to the given DAGService and returns the modified
|
||||
// root node.
|
||||
func (e *Editor) Finalize(ctx context.Context, ds node.DAGService) (*dag.ProtoNode, error) {
|
||||
nd := e.GetNode()
|
||||
err := copyDag(ctx, nd, e.tmp, ds)
|
||||
|
@ -70,8 +70,8 @@ func TestInsertNode(t *testing.T) {
|
||||
testInsert(t, e, "a/b/c/d/f", "baz", true, "")
|
||||
testInsert(t, e, "a/b/c/d/f", "bar", true, "")
|
||||
|
||||
testInsert(t, e, "", "bar", true, "cannot create link with no name!")
|
||||
testInsert(t, e, "////", "slashes", true, "cannot create link with no name!")
|
||||
testInsert(t, e, "", "bar", true, "cannot create link with no name")
|
||||
testInsert(t, e, "////", "slashes", true, "cannot create link with no name")
|
||||
|
||||
c := e.GetNode().Cid()
|
||||
|
||||
|
@ -40,6 +40,10 @@ type Directory struct {
|
||||
name string
|
||||
}
|
||||
|
||||
// NewDirectory constructs a new MFS directory.
|
||||
//
|
||||
// You probably don't want to call this directly. Instead, construct a new root
|
||||
// using NewRoot.
|
||||
func NewDirectory(ctx context.Context, name string, node node.Node, parent childCloser, dserv node.DAGService) (*Directory, error) {
|
||||
db, err := uio.NewDirectoryFromNode(dserv, node)
|
||||
if err != nil {
|
||||
|
@ -40,6 +40,7 @@ type Resolver struct {
|
||||
ResolveOnce func(ctx context.Context, ds node.DAGService, nd node.Node, names []string) (*node.Link, []string, error)
|
||||
}
|
||||
|
||||
// NewBasicResolver constructs a new basic resolver.
|
||||
func NewBasicResolver(ds node.DAGService) *Resolver {
|
||||
return &Resolver{
|
||||
DAG: ds,
|
||||
|
@ -34,6 +34,8 @@ func marshalHeader(h *tar.Header) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// ImportTar imports a tar file into the given DAGService and returns the root
|
||||
// node.
|
||||
func ImportTar(ctx context.Context, r io.Reader, ds node.DAGService) (*dag.ProtoNode, error) {
|
||||
tr := tar.NewReader(r)
|
||||
|
||||
@ -194,6 +196,8 @@ func (tr *tarReader) Read(b []byte) (int, error) {
|
||||
return tr.Read(b)
|
||||
}
|
||||
|
||||
// ExportTar exports the passed DAG as a tar file. This function is the inverse
|
||||
// of ImportTar.
|
||||
func ExportTar(ctx context.Context, root *dag.ProtoNode, ds node.DAGService) (io.Reader, error) {
|
||||
if string(root.Data()) != "ipfs/tar" {
|
||||
return nil, errors.New("not an IPFS tarchive")
|
||||
|
@ -66,6 +66,7 @@ type child interface {
|
||||
Label() string
|
||||
}
|
||||
|
||||
// NewHamtShard creates a new, empty HAMT shard with the given size.
|
||||
func NewHamtShard(dserv node.DAGService, size int) (*HamtShard, error) {
|
||||
ds, err := makeHamtShard(dserv, size)
|
||||
if err != nil {
|
||||
@ -93,6 +94,7 @@ func makeHamtShard(ds node.DAGService, size int) (*HamtShard, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewHamtFromDag creates new a HAMT shard from the given DAG.
|
||||
func NewHamtFromDag(dserv node.DAGService, nd node.Node) (*HamtShard, error) {
|
||||
pbnd, ok := nd.(*dag.ProtoNode)
|
||||
if !ok {
|
||||
|
@ -51,6 +51,8 @@ func NewDirectory(dserv node.DAGService) *Directory {
|
||||
// ErrNotADir implies that the given node was not a unixfs directory
|
||||
var ErrNotADir = fmt.Errorf("merkledag node was not a directory or shard")
|
||||
|
||||
// NewDirectoryFromNode loads a unixfs directory from the given IPLD node and
|
||||
// DAGService.
|
||||
func NewDirectoryFromNode(dserv node.DAGService, nd node.Node) (*Directory, error) {
|
||||
pbnd, ok := nd.(*mdag.ProtoNode)
|
||||
if !ok {
|
||||
|
@ -50,6 +50,7 @@ type pbDagReader struct {
|
||||
|
||||
var _ DagReader = (*pbDagReader)(nil)
|
||||
|
||||
// NewPBFileReader constructs a new PBFileReader.
|
||||
func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv node.DAGService) *pbDagReader {
|
||||
fctx, cancel := context.WithCancel(ctx)
|
||||
curLinks := getLinkCids(n)
|
||||
|
@ -27,6 +27,7 @@ func SizeSplitterGen(size int64) chunk.SplitterGen {
|
||||
}
|
||||
}
|
||||
|
||||
// GetDAGServ returns a mock DAGService.
|
||||
func GetDAGServ() node.DAGService {
|
||||
return mdagmock.Mock()
|
||||
}
|
||||
@ -51,6 +52,7 @@ func init() {
|
||||
UseBlake2b256.Prefix.MhLength = -1
|
||||
}
|
||||
|
||||
// GetNode returns a unixfs file node with the specified data.
|
||||
func GetNode(t testing.TB, dserv node.DAGService, data []byte, opts NodeOpts) node.Node {
|
||||
in := bytes.NewReader(data)
|
||||
|
||||
@ -69,10 +71,12 @@ func GetNode(t testing.TB, dserv node.DAGService, data []byte, opts NodeOpts) no
|
||||
return node
|
||||
}
|
||||
|
||||
// GetEmptyNode returns an empty unixfs file node.
|
||||
func GetEmptyNode(t testing.TB, dserv node.DAGService, opts NodeOpts) node.Node {
|
||||
return GetNode(t, dserv, []byte{}, opts)
|
||||
}
|
||||
|
||||
// GetRandomNode returns a random unixfs file node.
|
||||
func GetRandomNode(t testing.TB, dserv node.DAGService, size int64, opts NodeOpts) ([]byte, node.Node) {
|
||||
in := io.LimitReader(u.NewTimeSeededRand(), size)
|
||||
buf, err := ioutil.ReadAll(in)
|
||||
@ -96,6 +100,7 @@ func ArrComp(a, b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintDag pretty-prints the given dag to stdout.
|
||||
func PrintDag(nd *mdag.ProtoNode, ds node.DAGService, indent int) {
|
||||
pbd, err := ft.FromBytes(nd.Data())
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user