mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-19 09:52:03 +08:00

- updated go-ctxgroup and goprocess ctxgroup: AddChildGroup was changed to AddChild. Used in two files: - p2p/net/mock/mock_net.go - routing/dht/dht.go - updated context from hg repo to git prev. commit in hg was ad01a6fcc8a19d3a4478c836895ffe883bd2ceab. (context: make parentCancelCtx iterative) represents commit 84f8955a887232b6308d79c68b8db44f64df455c in git repo - updated context to master (b6fdb7d8a4ccefede406f8fe0f017fb58265054c) Aaron Jacobs (2): net/context: Don't accept a context in the DoSomethingSlow example. context: Be clear that users must cancel the result of WithCancel. Andrew Gerrand (1): go.net: use golang.org/x/... import paths Bryan C. Mills (1): net/context: Don't leak goroutines in Done example. Damien Neil (1): context: fix removal of cancelled timer contexts from parent David Symonds (2): context: Fix WithValue example code. net: add import comments. Sameer Ajmani (1): context: fix TestAllocs to account for ints in interfaces
188 lines
4.1 KiB
Go
188 lines
4.1 KiB
Go
package commands
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"strings"
|
|
|
|
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
"github.com/jbenet/go-ipfs/blocks"
|
|
cmds "github.com/jbenet/go-ipfs/commands"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
)
|
|
|
|
type BlockStat struct {
|
|
Key string
|
|
Size int
|
|
}
|
|
|
|
func (bs BlockStat) String() string {
|
|
return fmt.Sprintf("Key: %s\nSize: %d\n", bs.Key, bs.Size)
|
|
}
|
|
|
|
var BlockCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Manipulate raw IPFS blocks",
|
|
ShortDescription: `
|
|
'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
|
|
Reads from stdin or writes to stdout, and <key> is a base58 encoded
|
|
multihash.
|
|
`,
|
|
},
|
|
|
|
Subcommands: map[string]*cmds.Command{
|
|
"stat": blockStatCmd,
|
|
"get": blockGetCmd,
|
|
"put": blockPutCmd,
|
|
},
|
|
}
|
|
|
|
var blockStatCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Print information of a raw IPFS block",
|
|
ShortDescription: `
|
|
'ipfs block stat' is a plumbing command for retreiving information
|
|
on raw ipfs blocks. It outputs the following to stdout:
|
|
|
|
Key - the base58 encoded multihash
|
|
Size - the size of the block in bytes
|
|
|
|
`,
|
|
},
|
|
|
|
Arguments: []cmds.Argument{
|
|
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get").EnableStdin(),
|
|
},
|
|
Run: func(req cmds.Request, res cmds.Response) {
|
|
b, err := getBlockForKey(req, req.Arguments()[0])
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
res.SetOutput(&BlockStat{
|
|
Key: b.Key().Pretty(),
|
|
Size: len(b.Data),
|
|
})
|
|
},
|
|
Type: BlockStat{},
|
|
Marshalers: cmds.MarshalerMap{
|
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
|
bs := res.Output().(*BlockStat)
|
|
return strings.NewReader(bs.String()), nil
|
|
},
|
|
},
|
|
}
|
|
|
|
var blockGetCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Get a raw IPFS block",
|
|
ShortDescription: `
|
|
'ipfs block get' is a plumbing command for retreiving raw ipfs blocks.
|
|
It outputs to stdout, and <key> is a base58 encoded multihash.
|
|
`,
|
|
},
|
|
|
|
Arguments: []cmds.Argument{
|
|
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get").EnableStdin(),
|
|
},
|
|
Run: func(req cmds.Request, res cmds.Response) {
|
|
b, err := getBlockForKey(req, req.Arguments()[0])
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
res.SetOutput(bytes.NewReader(b.Data))
|
|
},
|
|
}
|
|
|
|
var blockPutCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Stores input as an IPFS block",
|
|
ShortDescription: `
|
|
ipfs block put is a plumbing command for storing raw ipfs blocks.
|
|
It reads from stdin, and <key> is a base58 encoded multihash.
|
|
`,
|
|
},
|
|
|
|
Arguments: []cmds.Argument{
|
|
cmds.FileArg("data", true, false, "The data to be stored as an IPFS block").EnableStdin(),
|
|
},
|
|
Run: func(req cmds.Request, res cmds.Response) {
|
|
n, err := req.Context().GetNode()
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
file, err := req.Files().NextFile()
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
err = file.Close()
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
b := blocks.NewBlock(data)
|
|
log.Debugf("BlockPut key: '%q'", b.Key())
|
|
|
|
k, err := n.Blocks.AddBlock(b)
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
res.SetOutput(&BlockStat{
|
|
Key: k.String(),
|
|
Size: len(data),
|
|
})
|
|
},
|
|
Marshalers: cmds.MarshalerMap{
|
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
|
bs := res.Output().(*BlockStat)
|
|
return strings.NewReader(bs.Key + "\n"), nil
|
|
},
|
|
},
|
|
Type: BlockStat{},
|
|
}
|
|
|
|
func getBlockForKey(req cmds.Request, key string) (*blocks.Block, error) {
|
|
n, err := req.Context().GetNode()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !u.IsValidHash(key) {
|
|
return nil, errors.New("Not a valid hash")
|
|
}
|
|
|
|
h, err := mh.FromB58String(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
k := u.Key(h)
|
|
b, err := n.Blocks.GetBlock(context.TODO(), k)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Debugf("ipfs block: got block with key: %q", b.Key())
|
|
return b, nil
|
|
}
|