Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "ipfs block rm" command. #2962

Merged
merged 7 commits into from
Aug 18, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
"block rm": add "--force" and "--quiet" option
License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed Aug 17, 2016
commit 8679af7a02fd356c251e4bdd9092ec5e87fe1261
25 changes: 21 additions & 4 deletions core/commands/block.go
Original file line number Diff line number Diff line change
@@ -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,13 +201,19 @@ 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 {
res.SetError(err, cmds.ErrNormal)
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reference will be bound such that it will always be called on the noop lock, even when the value in the unlocker variable is changed. If you want to do things this way, you will need to capture the variable in a closure:

defer func() {
    unlocker.Unlock()
}()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Will Fix!


@@ -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()}
}
}
44 changes: 44 additions & 0 deletions test/sharness/t0050-block.sh
Original file line number Diff line number Diff line change
@@ -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
'