block.go 1.18 KB
Newer Older
1 2 3
package commands

import (
Henry's avatar
Henry committed
4
	"fmt"
5 6 7 8
	"io"
	"io/ioutil"
	"os"

Henry's avatar
Henry committed
9
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10 11 12 13 14
	"github.com/jbenet/go-ipfs/blocks"
	"github.com/jbenet/go-ipfs/core"
	u "github.com/jbenet/go-ipfs/util"
)

Henry's avatar
Henry committed
15
// BlockGet retrives a raw ipfs block from the node's BlockService
16 17
func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {

Henry's avatar
Henry committed
18 19 20 21 22 23 24 25 26 27 28
	if !u.IsValidHash(args[0]) {
		return fmt.Errorf("block get: not a valid hash")
	}

	h, err := mh.FromB58String(args[0])
	if err != nil {
		return fmt.Errorf("block get: %v", err)
	}

	k := u.Key(h)
	log.Debug("BlockGet key: '%q'", k)
29 30
	b, err := n.Blocks.GetBlock(k)
	if err != nil {
Henry's avatar
Henry committed
31
		return fmt.Errorf("block get: %v", err)
32 33
	}

Henry's avatar
Henry committed
34 35
	_, err = out.Write(b.Data)
	return err
36 37
}

Henry's avatar
Henry committed
38
// BlockPut reads everything from conn and saves the data to the nodes BlockService
39 40 41 42 43 44 45
func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}

	b := blocks.NewBlock(data)
Henry's avatar
Henry committed
46
	log.Debug("BlockPut key: '%q'", b.Key())
47

Henry's avatar
Henry committed
48 49
	_, err = n.Blocks.AddBlock(b)
	log.Debug("BlockPut Done. Err: %q", err)
50

Henry's avatar
Henry committed
51
	return err
52
}