kubo/core/coreapi/block.go
Adin Schmahmann 52c177ced9
feat: go-libp2p 0.16, UnixFS autosharding and go-datastore with contexts (#8563)
* plumb through go-datastore context changes

* update go-libp2p to v0.16.0
* use LIBP2P_TCP_REUSEPORT instead of IPFS_REUSEPORT
* use relay config
* making deprecation notice match the go-ipfs-config key
* docs(config): circuit relay v2
* docs(config): fix links and headers
* feat(config): Internal.Libp2pForceReachability

This switches to config that supports setting and reading
Internal.Libp2pForceReachability OptionalString flag

* use configuration option for static relays

* chore: go-ipfs-config v0.18.0

https://github.com/ipfs/go-ipfs-config/releases/tag/v0.18.0

* feat: circuit v1 migration prompt when Swarm.EnableRelayHop is set (#8559)
* exit when Swarm.EnableRelayHop is set
* docs: Experimental.ShardingEnabled migration

This ensures existing users of global sharding experiment get notified
that the flag no longer works + that autosharding happens automatically.

For people who NEED to keep the old behavior (eg. have no time to
migrate today) there is a note about restoring it with
`UnixFSShardingSizeThreshold`.

* chore: add dag-jose code to the cid command output

* add support for setting automatic unixfs sharding threshold from the config
* test: have tests use low cutoff for sharding to mimic old behavior
* test: change error message to match the current error
* test: Add automatic sharding/unsharding tests (#8547)
* test: refactored naming in the sharding sharness tests to make more sense

* ci: set interop test executor to convenience image for Go1.16 + Node
* ci: use interop master

Co-authored-by: Marcin Rataj <lidel@lidel.org>
Co-authored-by: Marten Seemann <martenseemann@gmail.com>
Co-authored-by: Marcin Rataj <lidel@lidel.org>
Co-authored-by: Gus Eggert <gus@gus.dev>
Co-authored-by: Lucas Molas <schomatis@gmail.com>
2021-11-29 19:58:05 +01:00

147 lines
2.8 KiB
Go

package coreapi
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
pin "github.com/ipfs/go-ipfs-pinner"
coreiface "github.com/ipfs/interface-go-ipfs-core"
caopts "github.com/ipfs/interface-go-ipfs-core/options"
path "github.com/ipfs/interface-go-ipfs-core/path"
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
)
type BlockAPI CoreAPI
type BlockStat struct {
path path.Resolved
size int
}
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.BlockStat, error) {
settings, pref, err := caopts.BlockPutOptions(opts...)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
bcid, err := pref.Sum(data)
if err != nil {
return nil, err
}
b, err := blocks.NewBlockWithCid(data, bcid)
if err != nil {
return nil, err
}
if settings.Pin {
defer api.blockstore.PinLock(ctx).Unlock(ctx)
}
err = api.blocks.AddBlock(ctx, b)
if err != nil {
return nil, err
}
if settings.Pin {
api.pinning.PinWithMode(b.Cid(), pin.Recursive)
if err := api.pinning.Flush(ctx); err != nil {
return nil, err
}
}
return &BlockStat{path: path.IpldPath(b.Cid()), size: len(data)}, nil
}
func (api *BlockAPI) Get(ctx context.Context, p path.Path) (io.Reader, error) {
rp, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
b, err := api.blocks.GetBlock(ctx, rp.Cid())
if err != nil {
return nil, err
}
return bytes.NewReader(b.RawData()), nil
}
func (api *BlockAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.BlockRmOption) error {
rp, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}
settings, err := caopts.BlockRmOptions(opts...)
if err != nil {
return err
}
cids := []cid.Cid{rp.Cid()}
o := util.RmBlocksOpts{Force: settings.Force}
out, err := util.RmBlocks(ctx, api.blockstore, api.pinning, cids, o)
if err != nil {
return err
}
select {
case res, ok := <-out:
if !ok {
return nil
}
remBlock, ok := res.(*util.RemovedBlock)
if !ok {
return errors.New("got unexpected output from util.RmBlocks")
}
if remBlock.Error != "" {
return errors.New(remBlock.Error)
}
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (api *BlockAPI) Stat(ctx context.Context, p path.Path) (coreiface.BlockStat, error) {
rp, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
b, err := api.blocks.GetBlock(ctx, rp.Cid())
if err != nil {
return nil, err
}
return &BlockStat{
path: path.IpldPath(b.Cid()),
size: len(b.RawData()),
}, nil
}
func (bs *BlockStat) Size() int {
return bs.size
}
func (bs *BlockStat) Path() path.Resolved {
return bs.path
}
func (api *BlockAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api)
}