"block rm": add "--force" and "--quiet" option
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Aug 17, 2016 at 01:29 UTC
8679af7a02fd356c251e4bdd9092ec5e87fe1261
2 files changed
+65
-4
core/commands/block.go
+21
-4
@@ -13,6 +13,7 @@ import (
13
key "github.com/ipfs/go-ipfs/blocks/key"
14
cmds "github.com/ipfs/go-ipfs/commands"
15
"github.com/ipfs/go-ipfs/pin"
16
+ ds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore"
17
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
18
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
19
)
@@ -200,6 +201,10 @@ It takes a list of base58 encoded multihashs to remove.
201
Arguments: []cmds.Argument{
202
cmds.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
203
},
204
+ Options: []cmds.Option{
205
+ cmds.BoolOption("force", "f", "Ignore nonexistent blocks.").Default(false),
206
+ cmds.BoolOption("quiet", "q", "Write minimal output.").Default(false),
207
+ },
208
Run: func(req cmds.Request, res cmds.Response) {
209
n, err := req.InvocContext().GetNode()
210
if err != nil {
@@ -207,6 +212,8 @@ It takes a list of base58 encoded multihashs to remove.
212
return
213
}
214
hashes := req.Arguments()
215
+ force, _, _ := req.Option("force").Bool()
216
+ quiet, _, _ := req.Option("quiet").Bool()
217
keys := make([]key.Key, 0, len(hashes))
218
for _, hash := range hashes {
219
k := key.B58KeyDecode(hash)
@@ -217,7 +224,10 @@ It takes a list of base58 encoded multihashs to remove.
224
go func() {
225
defer close(outChan)
226
pinning := n.Pinning
220
- err := rmBlocks(n.Blockstore, pinning, outChan, keys)
227
+ err := rmBlocks(n.Blockstore, pinning, outChan, keys, rmBlocksOpts{
228
+ quiet: quiet,
229
+ force: force,
230
+ })
231
if err != nil {
232
outChan <- &RemovedBlock{Error: err.Error()}
233
}
@@ -260,7 +270,12 @@ type RemovedBlock struct {
270
Error string `json:",omitempty"`
271
}
272
263
-func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out chan<- interface{}, keys []key.Key) error {
273
+type rmBlocksOpts struct {
274
+ quiet bool
275
+ force bool
276
+}
277
+
278
+func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out chan<- interface{}, keys []key.Key, opts rmBlocksOpts) error {
279
unlocker := blocks.GCLock()
280
defer unlocker.Unlock()
281
@@ -271,9 +286,11 @@ func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out chan<- interface{}, k
286
287
for _, k := range stillOkay {
288
err := blocks.DeleteBlock(k)
274
- if err != nil {
289
+ if err != nil && opts.force && (err == bs.ErrNotFound || err == ds.ErrNotFound) {
290
+ // ignore non-existent blocks
291
+ } else if err != nil {
292
out <- &RemovedBlock{Hash: k.String(), Error: err.Error()}
276
- } else {
293
+ } else if !opts.quiet {
294
out <- &RemovedBlock{Hash: k.String()}
295
}
296
}
test/sharness/t0050-block.sh
+44
@@ -12,6 +12,10 @@ test_init_ipfs
12
13
HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
14
15
+#
16
+# "block put tests"
17
+#
18
+
19
test_expect_success "'ipfs block put' succeeds" '
20
echo "Hello Mars!" >expected_in &&
21
ipfs block put <expected_in >actual_out
@@ -22,6 +26,10 @@ test_expect_success "'ipfs block put' output looks good" '
26
test_cmp expected_out actual_out
27
'
28
29
+#
30
+# "block get" tests
31
+#
32
+
33
test_expect_success "'ipfs block get' succeeds" '
34
ipfs block get $HASH >actual_in
35
'
@@ -30,6 +38,10 @@ test_expect_success "'ipfs block get' output looks good" '
38
test_cmp expected_in actual_in
39
'
40
41
+#
42
+# "block stat" tests
43
+#
44
+
45
test_expect_success "'ipfs block stat' succeeds" '
46
ipfs block stat $HASH >actual_stat
47
'
@@ -40,6 +52,10 @@ test_expect_success "'ipfs block stat' output looks good" '
52
test_cmp expected_stat actual_stat
53
'
54
55
+#
56
+# "block rm" tests
57
+#
58
+
59
test_expect_success "'ipfs block rm' succeeds" '
60
ipfs block rm $HASH >actual_rm
61
'
@@ -129,6 +145,34 @@ test_expect_success "error reported on removing non-existent block" '
145
grep -q "cannot remove $RANDOMHASH" block_rm_err
146
'
147
148
+test_expect_success "'add some blocks' succeeds" '
149
+ echo "Hello Mars!" | ipfs block put &&
150
+ echo "Hello Venus!" | ipfs block put
151
+'
152
+
153
+test_expect_success "multi-block 'ipfs block rm -f' with non existent blocks succeed" '
154
+ ipfs block rm -f $HASH $RANDOMHASH $HASH2
155
+'
156
+
157
+test_expect_success "existent blocks removed" '
158
+ test_must_fail ipfs block stat $HASH &&
159
+ test_must_fail ipfs block stat $HASH2
160
+'
161
+
162
+test_expect_success "'add some blocks' succeeds" '
163
+ echo "Hello Mars!" | ipfs block put &&
164
+ echo "Hello Venus!" | ipfs block put
165
+'
166
+
167
+test_expect_success "multi-block 'ipfs block rm -q' produces no output" '
168
+ ipfs block rm -q $HASH $HASH2 > block_rm_out &&
169
+ test ! -s block_rm_out
170
+'
171
+
172
+#
173
+# Misc tests
174
+#
175
+
176
test_expect_success "'ipfs block stat' with nothing from stdin doesnt crash" '
177
test_expect_code 1 ipfs block stat < /dev/null 2> stat_out
178
'