block.go 5.89 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
	util "github.com/ipfs/go-ipfs/blocks/blockstore/util"
13
	cmds "github.com/ipfs/go-ipfs/commands"
14
	cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
15
	u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/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{
29
		Tagline: "Interact with 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,
Kevin Atkinson's avatar
Kevin Atkinson committed
41
		"rm":   blockRmCmd,
42 43 44
	},
}

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

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

55 56
`,
	},
57

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Jeromy's avatar
Jeromy committed
175
	c, err := cid.Decode(skey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
176 177 178 179
	if err != nil {
		return nil, err
	}

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

185
	log.Debugf("ipfs block: got block with key: %s", b.Cid())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186 187
	return b, nil
}
Kevin Atkinson's avatar
Kevin Atkinson committed
188 189 190 191 192 193 194 195 196 197 198 199

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."),
	},
200 201 202 203
	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
204 205 206 207 208 209 210
	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()
211 212
		force, _, _ := req.Option("force").Bool()
		quiet, _, _ := req.Option("quiet").Bool()
Jeromy's avatar
Jeromy committed
213
		cids := make([]*cid.Cid, 0, len(hashes))
Kevin Atkinson's avatar
Kevin Atkinson committed
214
		for _, hash := range hashes {
Jeromy's avatar
Jeromy committed
215 216 217 218 219 220 221
			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
222
		}
223
		outChan := make(chan interface{})
224 225 226 227 228 229 230 231
		err = util.RmBlocks(n.Blockstore, n.Pinning, outChan, cids, util.RmBlocksOpts{
			Quiet: quiet,
			Force: force,
		})
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
232
		res.SetOutput((<-chan interface{})(outChan))
Kevin Atkinson's avatar
Kevin Atkinson committed
233
	},
234 235 236 237 238 239 240 241 242 243 244
	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)

245 246 247
		err := util.ProcRmOutput(outChan, res.Stdout(), res.Stderr())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
248
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
249
	},
250
	Type: util.RemovedBlock{},
Kevin Atkinson's avatar
Kevin Atkinson committed
251
}