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

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

10
	"github.com/ipfs/go-ipfs/blocks"
11
	util "github.com/ipfs/go-ipfs/blocks/blockstore/util"
12
	cmds "github.com/ipfs/go-ipfs/commands"
13
	cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
14
	u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
15 16
)

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

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

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

36
	Subcommands: map[string]*cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38 39
		"stat": blockStatCmd,
		"get":  blockGetCmd,
		"put":  blockPutCmd,
Kevin Atkinson's avatar
Kevin Atkinson committed
40
		"rm":   blockRmCmd,
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{
68
			Key:  b.Cid().String(),
Jeromy's avatar
Jeromy committed
69
			Size: len(b.RawData()),
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
		}

Jeromy's avatar
Jeromy committed
100
		res.SetOutput(bytes.NewReader(b.RawData()))
101 102 103 104
	},
}

var blockPutCmd = &cmds.Command{
105
	Helptext: cmds.HelpText{
106
		Tagline: "Store 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.Cid())
143

144
		k, err := n.Blocks.AddBlock(b)
145
		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
	}

Jeromy's avatar
Jeromy committed
170
	c, err := cid.Decode(skey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171 172 173 174
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
175
	b, err := n.Blocks.GetBlock(req.Context(), c)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
176 177 178
	if err != nil {
		return nil, err
	}
179

180
	log.Debugf("ipfs block: got block with key: %s", b.Cid())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181 182
	return b, nil
}
Kevin Atkinson's avatar
Kevin Atkinson committed
183 184 185 186 187 188 189 190 191 192 193 194

var blockRmCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Remove IPFS block(s).",
		ShortDescription: `
'ipfs block rm' is a plumbing command for removing raw ipfs blocks.
It takes a list of base58 encoded multihashs to remove.
`,
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
	},
195 196 197 198
	Options: []cmds.Option{
		cmds.BoolOption("force", "f", "Ignore nonexistent blocks.").Default(false),
		cmds.BoolOption("quiet", "q", "Write minimal output.").Default(false),
	},
Kevin Atkinson's avatar
Kevin Atkinson committed
199 200 201 202 203 204 205
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		hashes := req.Arguments()
206 207
		force, _, _ := req.Option("force").Bool()
		quiet, _, _ := req.Option("quiet").Bool()
Jeromy's avatar
Jeromy committed
208
		cids := make([]*cid.Cid, 0, len(hashes))
Kevin Atkinson's avatar
Kevin Atkinson committed
209
		for _, hash := range hashes {
Jeromy's avatar
Jeromy committed
210 211 212 213 214 215 216
			c, err := cid.Decode(hash)
			if err != nil {
				res.SetError(fmt.Errorf("invalid content id: %s (%s)", hash, err), cmds.ErrNormal)
				return
			}

			cids = append(cids, c)
Kevin Atkinson's avatar
Kevin Atkinson committed
217
		}
218
		outChan := make(chan interface{})
219 220 221 222 223 224 225 226
		err = util.RmBlocks(n.Blockstore, n.Pinning, outChan, cids, util.RmBlocksOpts{
			Quiet: quiet,
			Force: force,
		})
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
227
		res.SetOutput((<-chan interface{})(outChan))
Kevin Atkinson's avatar
Kevin Atkinson committed
228
	},
229 230 231 232 233 234 235 236 237 238 239
	PostRun: func(req cmds.Request, res cmds.Response) {
		if res.Error() != nil {
			return
		}
		outChan, ok := res.Output().(<-chan interface{})
		if !ok {
			res.SetError(u.ErrCast(), cmds.ErrNormal)
			return
		}
		res.SetOutput(nil)

240 241 242
		err := util.ProcRmOutput(outChan, res.Stdout(), res.Stderr())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
243
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
244
	},
245
	Type: util.RemovedBlock{},
Kevin Atkinson's avatar
Kevin Atkinson committed
246
}