block.go 6.89 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"
Łukasz Magiera's avatar
Łukasz Magiera committed
11 12
	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13

Łukasz Magiera's avatar
Łukasz Magiera committed
14
	files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
15
	cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/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{
Overbool's avatar
Overbool committed
86
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
87 88 89
			_, err := fmt.Fprintf(w, "%s", bs)
			return err
		}),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91
	},
}
92

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

Jan Winkelmann's avatar
Jan Winkelmann committed
102 103
	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
104
	},
keks's avatar
keks committed
105
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
106 107
		api, err := cmdenv.GetApi(env)
		if err != nil {
keks's avatar
keks committed
108
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
109 110 111 112
		}

		p, err := coreiface.ParsePath(req.Arguments[0])
		if err != nil {
keks's avatar
keks committed
113
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
114 115 116
		}

		r, err := api.Block().Get(req.Context, p)
117
		if err != nil {
keks's avatar
keks committed
118
			return err
119 120
		}

keks's avatar
keks committed
121
		return res.Emit(r)
122 123 124
	},
}

Kejie Zhang's avatar
Kejie Zhang committed
125 126 127 128 129 130
const (
	blockFormatOptionName = "format"
	mhtypeOptionName      = "mhtype"
	mhlenOptionName       = "mhlen"
)

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

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.
140 141
`,
	},
142

Jan Winkelmann's avatar
Jan Winkelmann committed
143 144
	Arguments: []cmdkit.Argument{
		cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
145
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
146
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
147 148 149
		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),
150
	},
keks's avatar
keks committed
151
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
152
		api, err := cmdenv.GetApi(env)
153
		if err != nil {
keks's avatar
keks committed
154
			return err
155
		}
156

157
		it := req.Files.Entries()
158 159 160
		if !it.Next() && it.Err() != nil {
			return it.Err()
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
161
		if files.FileFromEntry(it) == nil {
162
			return fmt.Errorf("expected a regular file")
163 164
		}

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

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

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

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

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

Kejie Zhang's avatar
Kejie Zhang committed
204 205 206 207 208
const (
	forceOptionName      = "force"
	blockQuietOptionName = "quiet"
)

Kevin Atkinson's avatar
Kevin Atkinson committed
209
var blockRmCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
210
	Helptext: cmdkit.HelpText{
Kevin Atkinson's avatar
Kevin Atkinson committed
211 212 213
		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
214
It takes a list of base58 encoded multihashes to remove.
Kevin Atkinson's avatar
Kevin Atkinson committed
215 216
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
217 218
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
Kevin Atkinson's avatar
Kevin Atkinson committed
219
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
220
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
221 222
		cmdkit.BoolOption(forceOptionName, "f", "Ignore nonexistent blocks."),
		cmdkit.BoolOption(blockQuietOptionName, "q", "Write minimal output."),
223
	},
keks's avatar
keks committed
224
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
225
		api, err := cmdenv.GetApi(env)
Kevin Atkinson's avatar
Kevin Atkinson committed
226
		if err != nil {
keks's avatar
keks committed
227
			return err
Kevin Atkinson's avatar
Kevin Atkinson committed
228
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
229

Kejie Zhang's avatar
Kejie Zhang committed
230 231
		force, _ := req.Options[forceOptionName].(bool)
		quiet, _ := req.Options[blockQuietOptionName].(bool)
Łukasz Magiera's avatar
Łukasz Magiera committed
232 233 234 235

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

Łukasz Magiera's avatar
Łukasz Magiera committed
240 241
			rp, err := api.ResolvePath(req.Context, p)
			if err != nil {
keks's avatar
keks committed
242
				return err
Łukasz Magiera's avatar
Łukasz Magiera committed
243
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
244

Łukasz Magiera's avatar
Łukasz Magiera committed
245
			err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
246
			if err != nil {
Overbool's avatar
Overbool committed
247
				if err := res.Emit(&util.RemovedBlock{
Łukasz Magiera's avatar
Łukasz Magiera committed
248 249
					Hash:  rp.Cid().String(),
					Error: err.Error(),
Overbool's avatar
Overbool committed
250
				}); err != nil {
keks's avatar
keks committed
251 252
					return err
				}
Overbool's avatar
Overbool committed
253
				continue
Łukasz Magiera's avatar
Łukasz Magiera committed
254
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
255

256
			if !quiet {
keks's avatar
keks committed
257
				err := res.Emit(&util.RemovedBlock{
258 259
					Hash: rp.Cid().String(),
				})
keks's avatar
keks committed
260 261 262
				if err != nil {
					return err
				}
263
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
264
		}
keks's avatar
keks committed
265 266

		return nil
Kevin Atkinson's avatar
Kevin Atkinson committed
267
	},
268
	PostRun: cmds.PostRunMap{
keks's avatar
keks committed
269 270
		cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
			return util.ProcRmOutput(res.Next, os.Stdout, os.Stderr)
271
		},
Kevin Atkinson's avatar
Kevin Atkinson committed
272
	},
273
	Type: util.RemovedBlock{},
Kevin Atkinson's avatar
Kevin Atkinson committed
274
}