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

import (
keks's avatar
keks committed
4
	"errors"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"fmt"
6
	"io"
Jan Winkelmann's avatar
Jan Winkelmann committed
7
	"os"
8

9
	util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
10
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jan Winkelmann's avatar
Jan Winkelmann committed
11
	e "github.com/ipfs/go-ipfs/core/commands/e"
Łukasz Magiera's avatar
Łukasz Magiera committed
12 13
	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
14

Steven Allen's avatar
Steven Allen committed
15
	cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
16
	cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
Steven Allen's avatar
Steven Allen committed
17
	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
18 19
)

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

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

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

39
	Subcommands: map[string]*cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41 42
		"stat": blockStatCmd,
		"get":  blockGetCmd,
		"put":  blockPutCmd,
Kevin Atkinson's avatar
Kevin Atkinson committed
43
		"rm":   blockRmCmd,
44 45 46
	},
}

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

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

57 58
`,
	},
59

Jan Winkelmann's avatar
Jan Winkelmann committed
60 61
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("key", true, false, "The base58 multihash of an existing block to stat.").EnableStdin(),
62
	},
keks's avatar
keks committed
63
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
64 65
		api, err := cmdenv.GetApi(env)
		if err != nil {
keks's avatar
keks committed
66
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
67 68 69 70
		}

		p, err := coreiface.ParsePath(req.Arguments[0])
		if err != nil {
keks's avatar
keks committed
71
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
72 73 74
		}

		b, err := api.Block().Stat(req.Context, p)
75
		if err != nil {
keks's avatar
keks committed
76
			return err
77
		}
78

keks's avatar
keks committed
79
		return cmds.EmitOnce(res, &BlockStat{
Łukasz Magiera's avatar
Łukasz Magiera committed
80 81
			Key:  b.Path().Cid().String(),
			Size: b.Size(),
82
		})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84
	},
	Type: BlockStat{},
Jan Winkelmann's avatar
Jan Winkelmann committed
85
	Encoders: cmds.EncoderMap{
86
		cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
87 88 89 90 91 92 93
			bs, ok := v.(*BlockStat)
			if !ok {
				return e.TypeErr(bs, v)
			}
			_, err := fmt.Fprintf(w, "%s", bs)
			return err
		}),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94 95
	},
}
96

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97
var blockGetCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
98
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
99
		Tagline: "Get a raw IPFS block.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100
		ShortDescription: `
101
'ipfs block get' is a plumbing command for retrieving raw IPFS blocks.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102 103 104
It outputs to stdout, and <key> is a base58 encoded multihash.
`,
	},
105

Jan Winkelmann's avatar
Jan Winkelmann committed
106 107
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("key", true, false, "The base58 multihash of an existing block to get.").EnableStdin(),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
	},
keks's avatar
keks committed
109
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
110 111
		api, err := cmdenv.GetApi(env)
		if err != nil {
keks's avatar
keks committed
112
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
113 114 115 116
		}

		p, err := coreiface.ParsePath(req.Arguments[0])
		if err != nil {
keks's avatar
keks committed
117
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
118 119 120
		}

		r, err := api.Block().Get(req.Context, p)
121
		if err != nil {
keks's avatar
keks committed
122
			return err
123 124
		}

keks's avatar
keks committed
125
		return res.Emit(r)
126 127 128
	},
}

Kejie Zhang's avatar
Kejie Zhang committed
129 130 131 132 133 134
const (
	blockFormatOptionName = "format"
	mhtypeOptionName      = "mhtype"
	mhlenOptionName       = "mhlen"
)

135
var blockPutCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
136
	Helptext: cmdkit.HelpText{
137
		Tagline: "Store input as an IPFS block.",
138
		ShortDescription: `
139
'ipfs block put' is a plumbing command for storing raw IPFS blocks.
140
It reads from stdin, and <key> is a base58 encoded multihash.
141 142 143

By default CIDv0 is going to be generated. Setting 'mhtype' to anything other
than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
144 145
`,
	},
146

Jan Winkelmann's avatar
Jan Winkelmann committed
147 148
	Arguments: []cmdkit.Argument{
		cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
149
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
150
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
151 152 153
		cmdkit.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."),
		cmdkit.StringOption(mhtypeOptionName, "multihash hash function").WithDefault("sha2-256"),
		cmdkit.IntOption(mhlenOptionName, "multihash hash length").WithDefault(-1),
154
	},
keks's avatar
keks committed
155
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
156
		api, err := cmdenv.GetApi(env)
157
		if err != nil {
keks's avatar
keks committed
158
			return err
159
		}
160

161
		file, err := req.Files.NextFile()
162
		if err != nil {
keks's avatar
keks committed
163
			return err
164 165
		}

Kejie Zhang's avatar
Kejie Zhang committed
166
		mhtype, _ := req.Options[mhtypeOptionName].(string)
167 168
		mhtval, ok := mh.Names[mhtype]
		if !ok {
keks's avatar
keks committed
169
			return fmt.Errorf("unrecognized multihash function: %s", mhtype)
170 171
		}

Kejie Zhang's avatar
Kejie Zhang committed
172
		mhlen, ok := req.Options[mhlenOptionName].(int)
Łukasz Magiera's avatar
Łukasz Magiera committed
173
		if !ok {
keks's avatar
keks committed
174
			return errors.New("missing option \"mhlen\"")
Łukasz Magiera's avatar
Łukasz Magiera committed
175
		}
176

Kejie Zhang's avatar
Kejie Zhang committed
177
		format, formatSet := req.Options[blockFormatOptionName].(string)
178
		if !formatSet {
179
			if mhtval != mh.SHA2_256 || (mhlen != -1 && mhlen != 32) {
180
				format = "protobuf"
181 182
			} else {
				format = "v0"
183
			}
184
		}
185

Łukasz Magiera's avatar
Łukasz Magiera committed
186
		p, err := api.Block().Put(req.Context, file, options.Block.Hash(mhtval, mhlen), options.Block.Format(format))
187
		if err != nil {
keks's avatar
keks committed
188
			return err
189 190
		}

keks's avatar
keks committed
191
		return cmds.EmitOnce(res, &BlockStat{
Łukasz Magiera's avatar
Łukasz Magiera committed
192 193
			Key:  p.Path().Cid().String(),
			Size: p.Size(),
194
		})
195
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
196
	Encoders: cmds.EncoderMap{
197
		cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
198 199 200 201 202 203 204
			bs, ok := v.(*BlockStat)
			if !ok {
				return e.TypeErr(bs, v)
			}
			_, err := fmt.Fprintf(w, "%s\n", bs.Key)
			return err
		}),
205
	},
Matt Bell's avatar
Matt Bell committed
206
	Type: BlockStat{},
207
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
208

Kejie Zhang's avatar
Kejie Zhang committed
209 210 211 212 213
const (
	forceOptionName      = "force"
	blockQuietOptionName = "quiet"
)

Kevin Atkinson's avatar
Kevin Atkinson committed
214
var blockRmCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
215
	Helptext: cmdkit.HelpText{
Kevin Atkinson's avatar
Kevin Atkinson committed
216 217 218
		Tagline: "Remove IPFS block(s).",
		ShortDescription: `
'ipfs block rm' is a plumbing command for removing raw ipfs blocks.
Łukasz Magiera's avatar
Łukasz Magiera committed
219
It takes a list of base58 encoded multihashes to remove.
Kevin Atkinson's avatar
Kevin Atkinson committed
220 221
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
222 223
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
Kevin Atkinson's avatar
Kevin Atkinson committed
224
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
225
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
226 227
		cmdkit.BoolOption(forceOptionName, "f", "Ignore nonexistent blocks."),
		cmdkit.BoolOption(blockQuietOptionName, "q", "Write minimal output."),
228
	},
keks's avatar
keks committed
229
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
230
		api, err := cmdenv.GetApi(env)
Kevin Atkinson's avatar
Kevin Atkinson committed
231
		if err != nil {
keks's avatar
keks committed
232
			return err
Kevin Atkinson's avatar
Kevin Atkinson committed
233
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
234

Kejie Zhang's avatar
Kejie Zhang committed
235 236
		force, _ := req.Options[forceOptionName].(bool)
		quiet, _ := req.Options[blockQuietOptionName].(bool)
Łukasz Magiera's avatar
Łukasz Magiera committed
237 238 239 240

		// TODO: use batching coreapi when done
		for _, b := range req.Arguments {
			p, err := coreiface.ParsePath(b)
Jeromy's avatar
Jeromy committed
241
			if err != nil {
keks's avatar
keks committed
242
				return err
Jeromy's avatar
Jeromy committed
243 244
			}

Łukasz Magiera's avatar
Łukasz Magiera committed
245 246
			rp, err := api.ResolvePath(req.Context, p)
			if err != nil {
keks's avatar
keks committed
247
				return err
Łukasz Magiera's avatar
Łukasz Magiera committed
248
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
249

Łukasz Magiera's avatar
Łukasz Magiera committed
250
			err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
251
			if err != nil {
keks's avatar
keks committed
252
				err := res.Emit(&util.RemovedBlock{
Łukasz Magiera's avatar
Łukasz Magiera committed
253 254 255
					Hash:  rp.Cid().String(),
					Error: err.Error(),
				})
keks's avatar
keks committed
256 257 258
				if err != nil {
					return err
				}
Łukasz Magiera's avatar
Łukasz Magiera committed
259
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
260

261
			if !quiet {
keks's avatar
keks committed
262
				err := res.Emit(&util.RemovedBlock{
263 264
					Hash: rp.Cid().String(),
				})
keks's avatar
keks committed
265 266 267
				if err != nil {
					return err
				}
268
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
269
		}
keks's avatar
keks committed
270 271

		return nil
Kevin Atkinson's avatar
Kevin Atkinson committed
272
	},
273
	PostRun: cmds.PostRunMap{
keks's avatar
keks committed
274 275
		cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
			return util.ProcRmOutput(res.Next, os.Stdout, os.Stderr)
276
		},
Kevin Atkinson's avatar
Kevin Atkinson committed
277
	},
278
	Type: util.RemovedBlock{},
Kevin Atkinson's avatar
Kevin Atkinson committed
279
}