提交 8679af7a 作者: Kevin Atkinson

"block rm": add "--force" and "--quiet" option

License: MIT
Signed-off-by: 's avatarKevin Atkinson <k@kevina.org>
上级 92f6747a
......@@ -13,6 +13,7 @@ import (
key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/pin"
ds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore"
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)
......@@ -200,6 +201,10 @@ 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."),
},
Options: []cmds.Option{
cmds.BoolOption("force", "f", "Ignore nonexistent blocks.").Default(false),
cmds.BoolOption("quiet", "q", "Write minimal output.").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
......@@ -207,6 +212,8 @@ It takes a list of base58 encoded multihashs to remove.
return
}
hashes := req.Arguments()
force, _, _ := req.Option("force").Bool()
quiet, _, _ := req.Option("quiet").Bool()
keys := make([]key.Key, 0, len(hashes))
for _, hash := range hashes {
k := key.B58KeyDecode(hash)
......@@ -217,7 +224,10 @@ It takes a list of base58 encoded multihashs to remove.
go func() {
defer close(outChan)
pinning := n.Pinning
err := rmBlocks(n.Blockstore, pinning, outChan, keys)
err := rmBlocks(n.Blockstore, pinning, outChan, keys, rmBlocksOpts{
quiet: quiet,
force: force,
})
if err != nil {
outChan <- &RemovedBlock{Error: err.Error()}
}
......@@ -260,7 +270,12 @@ type RemovedBlock struct {
Error string `json:",omitempty"`
}
func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out chan<- interface{}, keys []key.Key) error {
type rmBlocksOpts struct {
quiet bool
force bool
}
func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out chan<- interface{}, keys []key.Key, opts rmBlocksOpts) error {
unlocker := blocks.GCLock()
defer unlocker.Unlock()
......@@ -271,9 +286,11 @@ func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out chan<- interface{}, k
for _, k := range stillOkay {
err := blocks.DeleteBlock(k)
if err != nil {
if err != nil && opts.force && (err == bs.ErrNotFound || err == ds.ErrNotFound) {
// ignore non-existent blocks
} else if err != nil {
out <- &RemovedBlock{Hash: k.String(), Error: err.Error()}
} else {
} else if !opts.quiet {
out <- &RemovedBlock{Hash: k.String()}
}
}
......
......@@ -12,6 +12,10 @@ test_init_ipfs
HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
#
# "block put tests"
#
test_expect_success "'ipfs block put' succeeds" '
echo "Hello Mars!" >expected_in &&
ipfs block put <expected_in >actual_out
......@@ -22,6 +26,10 @@ test_expect_success "'ipfs block put' output looks good" '
test_cmp expected_out actual_out
'
#
# "block get" tests
#
test_expect_success "'ipfs block get' succeeds" '
ipfs block get $HASH >actual_in
'
......@@ -30,6 +38,10 @@ test_expect_success "'ipfs block get' output looks good" '
test_cmp expected_in actual_in
'
#
# "block stat" tests
#
test_expect_success "'ipfs block stat' succeeds" '
ipfs block stat $HASH >actual_stat
'
......@@ -40,6 +52,10 @@ test_expect_success "'ipfs block stat' output looks good" '
test_cmp expected_stat actual_stat
'
#
# "block rm" tests
#
test_expect_success "'ipfs block rm' succeeds" '
ipfs block rm $HASH >actual_rm
'
......@@ -129,6 +145,34 @@ test_expect_success "error reported on removing non-existent block" '
grep -q "cannot remove $RANDOMHASH" block_rm_err
'
test_expect_success "'add some blocks' succeeds" '
echo "Hello Mars!" | ipfs block put &&
echo "Hello Venus!" | ipfs block put
'
test_expect_success "multi-block 'ipfs block rm -f' with non existent blocks succeed" '
ipfs block rm -f $HASH $RANDOMHASH $HASH2
'
test_expect_success "existent blocks removed" '
test_must_fail ipfs block stat $HASH &&
test_must_fail ipfs block stat $HASH2
'
test_expect_success "'add some blocks' succeeds" '
echo "Hello Mars!" | ipfs block put &&
echo "Hello Venus!" | ipfs block put
'
test_expect_success "multi-block 'ipfs block rm -q' produces no output" '
ipfs block rm -q $HASH $HASH2 > block_rm_out &&
test ! -s block_rm_out
'
#
# Misc tests
#
test_expect_success "'ipfs block stat' with nothing from stdin doesnt crash" '
test_expect_code 1 ipfs block stat < /dev/null 2> stat_out
'
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论