Add "ipfs block rm" command.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Jul 13, 2016 at 16:01 UTC
453be22dfb0f2fe83d8025e077f7360ce2b3438e
2 files changed
+167
-1
core/commands/block.go
+99
@@ -9,8 +9,10 @@ import (
9
"strings"
10
11
"github.com/ipfs/go-ipfs/blocks"
12
+ bs "github.com/ipfs/go-ipfs/blocks/blockstore"
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
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
17
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
18
)
@@ -38,6 +40,7 @@ multihash.
40
"stat": blockStatCmd,
41
"get": blockGetCmd,
42
"put": blockPutCmd,
43
+ "rm": blockRmCmd,
44
},
45
}
46
@@ -185,3 +188,99 @@ func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) {
188
log.Debugf("ipfs block: got block with key: %q", b.Key())
189
return b, nil
190
}
191
+
192
+var blockRmCmd = &cmds.Command{
193
+ Helptext: cmds.HelpText{
194
+ Tagline: "Remove IPFS block(s).",
195
+ ShortDescription: `
196
+'ipfs block rm' is a plumbing command for removing raw ipfs blocks.
197
+It takes a list of base58 encoded multihashs to remove.
198
+`,
199
+ },
200
+ Arguments: []cmds.Argument{
201
+ cmds.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
202
+ },
203
+ Options: []cmds.Option{
204
+ cmds.BoolOption("ignore-pins", "Ignore pins.").Default(false),
205
+ },
206
+ Run: func(req cmds.Request, res cmds.Response) {
207
+ ignorePins, _, err := req.Option("ignore-pins").Bool()
208
+ if err != nil {
209
+ res.SetError(err, cmds.ErrNormal)
210
+ return
211
+ }
212
+ n, err := req.InvocContext().GetNode()
213
+ if err != nil {
214
+ res.SetError(err, cmds.ErrNormal)
215
+ return
216
+ }
217
+ hashes := req.Arguments()
218
+ keys := make([]key.Key, 0, len(hashes))
219
+ for _, hash := range hashes {
220
+ k := key.B58KeyDecode(hash)
221
+ keys = append(keys, k)
222
+ }
223
+ rdr, wtr := io.Pipe()
224
+ go func() {
225
+ pinning := n.Pinning
226
+ if ignorePins {
227
+ pinning = nil
228
+ }
229
+ err := rmBlocks(n.Blockstore, pinning, wtr, keys)
230
+ if err != nil {
231
+ wtr.CloseWithError(fmt.Errorf("Some blocks not deleted: %s", err))
232
+ } else {
233
+ wtr.Close()
234
+ }
235
+ }()
236
+ res.SetOutput(rdr)
237
+ return
238
+ },
239
+ Marshalers: cmds.MarshalerMap{
240
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
241
+ return res.(io.Reader), nil
242
+ },
243
+ },
244
+}
245
+
246
+// pins may be nil
247
+func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out io.Writer, keys []key.Key) error {
248
+ var unlocker bs.Unlocker
249
+ defer func() {
250
+ if unlocker != nil {
251
+ unlocker.Unlock()
252
+ }
253
+ }()
254
+ if pins != nil {
255
+ // Need to make sure that some operation that is
256
+ // finishing with a pin is ocurr simultaneously.
257
+ unlocker = blocks.GCLock()
258
+ err := checkIfPinned(pins, keys)
259
+ if err != nil {
260
+ return err
261
+ }
262
+ }
263
+ for _, k := range keys {
264
+ err := blocks.DeleteBlock(k)
265
+ if err != nil {
266
+ return fmt.Errorf("%s: %s", k, err)
267
+ }
268
+ if out != nil {
269
+ fmt.Fprintf(out, "deleted %s\n", k)
270
+ }
271
+ }
272
+ return nil
273
+}
274
+
275
+func checkIfPinned(pins pin.Pinner, keys []key.Key) error {
276
+ for _, k := range keys {
277
+ reason, pinned, err := pins.IsPinned(k)
278
+ if err != nil {
279
+ return err
280
+ }
281
+ if pinned {
282
+ return fmt.Errorf("%s pinned via %s", k, reason)
283
+ }
284
+ }
285
+ return nil
286
+}
test/sharness/t0050-block.sh
+68
-1
@@ -33,12 +33,79 @@ test_expect_success "'ipfs block stat' succeeds" '
33
ipfs block stat $HASH >actual_stat
34
'
35
36
-test_expect_success "'ipfs block get' output looks good" '
36
+test_expect_success "'ipfs block stat' output looks good" '
37
echo "Key: $HASH" >expected_stat &&
38
echo "Size: 12" >>expected_stat &&
39
test_cmp expected_stat actual_stat
40
'
41
42
+test_expect_success "'ipfs block rm' succeeds" '
43
+ ipfs block rm $HASH >actual_rm
44
+'
45
+
46
+test_expect_success "'ipfs block rm' output looks good" '
47
+ echo "deleted $HASH" > expected_rm &&
48
+ test_cmp expected_rm actual_rm
49
+'
50
+
51
+test_expect_success "'ipfs block rm' block actually removed" '
52
+ test_must_fail ipfs block stat $HASH
53
+'
54
+
55
+DIRHASH=QmdWmVmM6W2abTgkEfpbtA1CJyTWS2rhuUB9uP1xV8Uwtf
56
+FILE1HASH=Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz
57
+FILE2HASH=QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF
58
+FILE3HASH=Qmesmmf1EEG1orJb6XdK6DabxexsseJnCfw8pqWgonbkoj
59
+
60
+test_expect_success "add and pin directory" '
61
+ mkdir adir &&
62
+ echo "file1" > adir/file1 &&
63
+ echo "file2" > adir/file2 &&
64
+ echo "file3" > adir/file3 &&
65
+ ipfs add -r adir
66
+ ipfs pin add -r $DIRHASH
67
+'
68
+
69
+test_expect_success "can't remove pinned block" '
70
+ test_must_fail ipfs block rm $DIRHASH 2> block_rm_err
71
+'
72
+
73
+test_expect_success "can't remove pinned block: output looks good" '
74
+ grep -q "$DIRHASH pinned via recursive" block_rm_err
75
+'
76
+
77
+test_expect_success "can't remove indirectly pinned block" '
78
+ test_must_fail ipfs block rm $FILE1HASH 2> block_rm_err
79
+'
80
+
81
+test_expect_success "can't remove indirectly pinned block: output looks good" '
82
+ grep -q "$FILE1HASH pinned via $DIRHASH" block_rm_err
83
+'
84
+
85
+test_expect_success "multi-block 'ipfs block rm --ignore-pins' succeeds" '
86
+ ipfs block rm --ignore-pins $DIRHASH >actual_rm
87
+'
88
+
89
+test_expect_success "multi-block 'ipfs block rm --ignore-pins' output looks good" '
90
+ echo "deleted $DIRHASH" > expected_rm &&
91
+ test_cmp expected_rm actual_rm
92
+'
93
+
94
+test_expect_success "fix up pins" '
95
+ ipfs pin rm -r $DIRHASH
96
+'
97
+
98
+test_expect_success "multi-block 'ipfs block rm' succeeds" '
99
+ ipfs block rm $FILE1HASH $FILE2HASH $FILE3HASH > actual_rm
100
+'
101
+
102
+test_expect_success "multi-block 'ipfs block rm' output looks good" '
103
+ echo "deleted $FILE1HASH" > expected_rm &&
104
+ echo "deleted $FILE2HASH" >> expected_rm &&
105
+ echo "deleted $FILE3HASH" >> expected_rm &&
106
+ test_cmp expected_rm actual_rm
107
+'
108
+
109
test_expect_success "'ipfs block stat' with nothing from stdin doesnt crash" '
110
test_expect_code 1 ipfs block stat < /dev/null 2> stat_out
111
'