block.go 4.16 KB
Newer Older
1 2 3 4
package commands

import (
	"bytes"
5
	"errors"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	"fmt"
7
	"io"
8
	"io/ioutil"
9
	"strings"
10

11
	"github.com/ipfs/go-ipfs/blocks"
12
	key "github.com/ipfs/go-ipfs/blocks/key"
13
	cmds "github.com/ipfs/go-ipfs/commands"
Jeromy's avatar
Jeromy committed
14
	mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
15
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
16 17
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21 22 23 24
type BlockStat struct {
	Key  string
	Size int
}

func (bs BlockStat) String() string {
	return fmt.Sprintf("Key: %s\nSize: %d\n", bs.Key, bs.Size)
25 26
}

27
var BlockCmd = &cmds.Command{
28
	Helptext: cmds.HelpText{
rht's avatar
rht committed
29
		Tagline: "Manipulate raw IPFS blocks.",
30 31
		ShortDescription: `
'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
32
Reads from stdin or writes to stdout, and <key> is a base58 encoded
33 34 35 36
multihash.
`,
	},

37
	Subcommands: map[string]*cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40
		"stat": blockStatCmd,
		"get":  blockGetCmd,
		"put":  blockPutCmd,
41 42 43
	},
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
var blockStatCmd = &cmds.Command{
45
	Helptext: cmds.HelpText{
rht's avatar
rht committed
46
		Tagline: "Print information of a raw IPFS block.",
47
		ShortDescription: `
rht's avatar
rht committed
48
'ipfs block stat' is a plumbing command for retrieving information
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50 51 52 53
on raw ipfs blocks. It outputs the following to stdout:

	Key  - the base58 encoded multihash
	Size - the size of the block in bytes

54 55
`,
	},
56

57
	Arguments: []cmds.Argument{
58
		cmds.StringArg("key", true, false, "The base58 multihash of an existing block to stat.").EnableStdin(),
59
	},
60
	Run: func(req cmds.Request, res cmds.Response) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
		b, err := getBlockForKey(req, req.Arguments()[0])
62
		if err != nil {
63 64
			res.SetError(err, cmds.ErrNormal)
			return
65
		}
66

67
		res.SetOutput(&BlockStat{
Michael Muré's avatar
Michael Muré committed
68
			Key:  b.Key().B58String(),
69
			Size: len(b.Data()),
70
		})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71 72 73 74 75 76 77 78 79
	},
	Type: BlockStat{},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			bs := res.Output().(*BlockStat)
			return strings.NewReader(bs.String()), nil
		},
	},
}
80

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82
var blockGetCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
83
		Tagline: "Get a raw IPFS block.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
		ShortDescription: `
rht's avatar
rht committed
85
'ipfs block get' is a plumbing command for retrieving raw ipfs blocks.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87 88
It outputs to stdout, and <key> is a base58 encoded multihash.
`,
	},
89

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
	Arguments: []cmds.Argument{
91
		cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get.").EnableStdin(),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
	},
93
	Run: func(req cmds.Request, res cmds.Response) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
		b, err := getBlockForKey(req, req.Arguments()[0])
95
		if err != nil {
96 97
			res.SetError(err, cmds.ErrNormal)
			return
98 99
		}

100
		res.SetOutput(bytes.NewReader(b.Data()))
101 102 103 104
	},
}

var blockPutCmd = &cmds.Command{
105
	Helptext: cmds.HelpText{
rht's avatar
rht committed
106
		Tagline: "Stores input as an IPFS block.",
107
		ShortDescription: `
108
'ipfs block put' is a plumbing command for storing raw ipfs blocks.
109 110 111
It reads from stdin, and <key> is a base58 encoded multihash.
`,
	},
112

113
	Arguments: []cmds.Argument{
114
		cmds.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
115
	},
116
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
117
		n, err := req.InvocContext().GetNode()
118
		if err != nil {
119 120
			res.SetError(err, cmds.ErrNormal)
			return
121
		}
122

123 124
		file, err := req.Files().NextFile()
		if err != nil {
125 126
			res.SetError(err, cmds.ErrNormal)
			return
127 128 129 130
		}

		data, err := ioutil.ReadAll(file)
		if err != nil {
131 132
			res.SetError(err, cmds.ErrNormal)
			return
133 134
		}

135
		err = file.Close()
136
		if err != nil {
137 138
			res.SetError(err, cmds.ErrNormal)
			return
139 140 141
		}

		b := blocks.NewBlock(data)
142
		log.Debugf("BlockPut key: '%q'", b.Key())
143 144 145

		k, err := n.Blocks.AddBlock(b)
		if err != nil {
146 147
			res.SetError(err, cmds.ErrNormal)
			return
148 149
		}

150
		res.SetOutput(&BlockStat{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151 152
			Key:  k.String(),
			Size: len(data),
153
		})
154
	},
155
	Marshalers: cmds.MarshalerMap{
156
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157 158
			bs := res.Output().(*BlockStat)
			return strings.NewReader(bs.Key + "\n"), nil
159 160
		},
	},
Matt Bell's avatar
Matt Bell committed
161
	Type: BlockStat{},
162
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
163

164
func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) {
Jeromy's avatar
Jeromy committed
165
	n, err := req.InvocContext().GetNode()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
166 167 168 169
	if err != nil {
		return nil, err
	}

170
	if !u.IsValidHash(skey) {
171
		return nil, errors.New("Not a valid hash")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172 173
	}

174
	h, err := mh.FromB58String(skey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
175 176 177 178
	if err != nil {
		return nil, err
	}

179
	k := key.Key(h)
Jeromy's avatar
Jeromy committed
180
	b, err := n.Blocks.GetBlock(req.Context(), k)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181 182 183
	if err != nil {
		return nil, err
	}
184

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185 186 187
	log.Debugf("ipfs block: got block with key: %q", b.Key())
	return b, nil
}