block.go 6.56 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"
11

Jakub Sztandera's avatar
Jakub Sztandera committed
12 13 14
	cmdkit "github.com/ipfs/go-ipfs-cmdkit"
	cmds "github.com/ipfs/go-ipfs-cmds"
	options "github.com/ipfs/interface-go-ipfs-core/options"
15
	path "github.com/ipfs/interface-go-ipfs-core/path"
Jakub Sztandera's avatar
Jakub Sztandera committed
16
	mh "github.com/multiformats/go-multihash"
17 18
)

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

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

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

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

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

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

56 57
`,
	},
58

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

68
		b, err := api.Block().Stat(req.Context, path.New(req.Arguments[0]))
69
		if err != nil {
keks's avatar
keks committed
70
			return err
71
		}
72

keks's avatar
keks committed
73
		return cmds.EmitOnce(res, &BlockStat{
Łukasz Magiera's avatar
Łukasz Magiera committed
74 75
			Key:  b.Path().Cid().String(),
			Size: b.Size(),
76
		})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77 78
	},
	Type: BlockStat{},
Jan Winkelmann's avatar
Jan Winkelmann committed
79
	Encoders: cmds.EncoderMap{
Overbool's avatar
Overbool committed
80
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
81 82 83
			_, err := fmt.Fprintf(w, "%s", bs)
			return err
		}),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85
	},
}
86

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

Jan Winkelmann's avatar
Jan Winkelmann committed
96 97
	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
98
	},
keks's avatar
keks committed
99
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
100
		api, err := cmdenv.GetApi(env, req)
Łukasz Magiera's avatar
Łukasz Magiera committed
101
		if err != nil {
keks's avatar
keks committed
102
			return err
Łukasz Magiera's avatar
Łukasz Magiera committed
103 104
		}

105
		r, err := api.Block().Get(req.Context, path.New(req.Arguments[0]))
106
		if err != nil {
keks's avatar
keks committed
107
			return err
108 109
		}

keks's avatar
keks committed
110
		return res.Emit(r)
111 112 113
	},
}

Kejie Zhang's avatar
Kejie Zhang committed
114 115 116 117 118 119
const (
	blockFormatOptionName = "format"
	mhtypeOptionName      = "mhtype"
	mhlenOptionName       = "mhlen"
)

120
var blockPutCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
121
	Helptext: cmdkit.HelpText{
122
		Tagline: "Store input as an IPFS block.",
123
		ShortDescription: `
124
'ipfs block put' is a plumbing command for storing raw IPFS blocks.
125
It reads from stdin, and <key> is a base58 encoded multihash.
126 127 128

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.
129 130
`,
	},
131

Jan Winkelmann's avatar
Jan Winkelmann committed
132 133
	Arguments: []cmdkit.Argument{
		cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
134
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
135
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
136 137 138
		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),
Łukasz Magiera's avatar
Łukasz Magiera committed
139
		cmdkit.BoolOption(pinOptionName, "pin added blocks recursively").WithDefault(false),
140
	},
keks's avatar
keks committed
141
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
142
		api, err := cmdenv.GetApi(env, req)
143
		if err != nil {
keks's avatar
keks committed
144
			return err
145
		}
146

147 148 149
		file, err := cmdenv.GetFileArg(req.Files.Entries())
		if err != nil {
			return err
150 151
		}

Kejie Zhang's avatar
Kejie Zhang committed
152
		mhtype, _ := req.Options[mhtypeOptionName].(string)
153 154
		mhtval, ok := mh.Names[mhtype]
		if !ok {
keks's avatar
keks committed
155
			return fmt.Errorf("unrecognized multihash function: %s", mhtype)
156 157
		}

Kejie Zhang's avatar
Kejie Zhang committed
158
		mhlen, ok := req.Options[mhlenOptionName].(int)
Łukasz Magiera's avatar
Łukasz Magiera committed
159
		if !ok {
keks's avatar
keks committed
160
			return errors.New("missing option \"mhlen\"")
Łukasz Magiera's avatar
Łukasz Magiera committed
161
		}
162

Kejie Zhang's avatar
Kejie Zhang committed
163
		format, formatSet := req.Options[blockFormatOptionName].(string)
164
		if !formatSet {
165
			if mhtval != mh.SHA2_256 || (mhlen != -1 && mhlen != 32) {
166
				format = "protobuf"
167 168
			} else {
				format = "v0"
169
			}
170
		}
171

Łukasz Magiera's avatar
Łukasz Magiera committed
172 173 174 175 176 177
		pin, _ := req.Options[pinOptionName].(bool)

		p, err := api.Block().Put(req.Context, file,
			options.Block.Hash(mhtval, mhlen),
			options.Block.Format(format),
			options.Block.Pin(pin))
178
		if err != nil {
keks's avatar
keks committed
179
			return err
180 181
		}

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

Kejie Zhang's avatar
Kejie Zhang committed
196 197 198 199 200
const (
	forceOptionName      = "force"
	blockQuietOptionName = "quiet"
)

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

Kejie Zhang's avatar
Kejie Zhang committed
222 223
		force, _ := req.Options[forceOptionName].(bool)
		quiet, _ := req.Options[blockQuietOptionName].(bool)
Łukasz Magiera's avatar
Łukasz Magiera committed
224 225 226

		// TODO: use batching coreapi when done
		for _, b := range req.Arguments {
227
			rp, err := api.ResolvePath(req.Context, path.New(b))
Jan Winkelmann's avatar
Jan Winkelmann committed
228

Łukasz Magiera's avatar
Łukasz Magiera committed
229
			err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
230
			if err != nil {
Overbool's avatar
Overbool committed
231
				if err := res.Emit(&util.RemovedBlock{
Łukasz Magiera's avatar
Łukasz Magiera committed
232 233
					Hash:  rp.Cid().String(),
					Error: err.Error(),
Overbool's avatar
Overbool committed
234
				}); err != nil {
keks's avatar
keks committed
235 236
					return err
				}
Overbool's avatar
Overbool committed
237
				continue
Łukasz Magiera's avatar
Łukasz Magiera committed
238
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
239

240
			if !quiet {
keks's avatar
keks committed
241
				err := res.Emit(&util.RemovedBlock{
242 243
					Hash: rp.Cid().String(),
				})
keks's avatar
keks committed
244 245 246
				if err != nil {
					return err
				}
247
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
248
		}
keks's avatar
keks committed
249 250

		return nil
Kevin Atkinson's avatar
Kevin Atkinson committed
251
	},
252
	PostRun: cmds.PostRunMap{
keks's avatar
keks committed
253 254
		cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
			return util.ProcRmOutput(res.Next, os.Stdout, os.Stderr)
255
		},
Kevin Atkinson's avatar
Kevin Atkinson committed
256
	},
257
	Type: util.RemovedBlock{},
Kevin Atkinson's avatar
Kevin Atkinson committed
258
}