Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
453be22d
提交
453be22d
authored
7月 13, 2016
作者:
Kevin Atkinson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add "ipfs block rm" command.
License: MIT Signed-off-by:
Kevin Atkinson
<
k@kevina.org
>
上级
16f85704
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
167 行增加
和
1 行删除
+167
-1
block.go
core/commands/block.go
+99
-0
t0050-block.sh
test/sharness/t0050-block.sh
+68
-1
没有找到文件。
core/commands/block.go
浏览文件 @
453be22d
...
@@ -9,8 +9,10 @@ import (
...
@@ -9,8 +9,10 @@ import (
"strings"
"strings"
"github.com/ipfs/go-ipfs/blocks"
"github.com/ipfs/go-ipfs/blocks"
bs
"github.com/ipfs/go-ipfs/blocks/blockstore"
key
"github.com/ipfs/go-ipfs/blocks/key"
key
"github.com/ipfs/go-ipfs/blocks/key"
cmds
"github.com/ipfs/go-ipfs/commands"
cmds
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/pin"
mh
"gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
mh
"gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)
)
...
@@ -38,6 +40,7 @@ multihash.
...
@@ -38,6 +40,7 @@ multihash.
"stat"
:
blockStatCmd
,
"stat"
:
blockStatCmd
,
"get"
:
blockGetCmd
,
"get"
:
blockGetCmd
,
"put"
:
blockPutCmd
,
"put"
:
blockPutCmd
,
"rm"
:
blockRmCmd
,
},
},
}
}
...
@@ -185,3 +188,99 @@ func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) {
...
@@ -185,3 +188,99 @@ func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) {
log
.
Debugf
(
"ipfs block: got block with key: %q"
,
b
.
Key
())
log
.
Debugf
(
"ipfs block: got block with key: %q"
,
b
.
Key
())
return
b
,
nil
return
b
,
nil
}
}
var
blockRmCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Remove IPFS block(s)."
,
ShortDescription
:
`
'ipfs block rm' is a plumbing command for removing raw ipfs blocks.
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
(
"ignore-pins"
,
"Ignore pins."
)
.
Default
(
false
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
ignorePins
,
_
,
err
:=
req
.
Option
(
"ignore-pins"
)
.
Bool
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
hashes
:=
req
.
Arguments
()
keys
:=
make
([]
key
.
Key
,
0
,
len
(
hashes
))
for
_
,
hash
:=
range
hashes
{
k
:=
key
.
B58KeyDecode
(
hash
)
keys
=
append
(
keys
,
k
)
}
rdr
,
wtr
:=
io
.
Pipe
()
go
func
()
{
pinning
:=
n
.
Pinning
if
ignorePins
{
pinning
=
nil
}
err
:=
rmBlocks
(
n
.
Blockstore
,
pinning
,
wtr
,
keys
)
if
err
!=
nil
{
wtr
.
CloseWithError
(
fmt
.
Errorf
(
"Some blocks not deleted: %s"
,
err
))
}
else
{
wtr
.
Close
()
}
}()
res
.
SetOutput
(
rdr
)
return
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
return
res
.
(
io
.
Reader
),
nil
},
},
}
// pins may be nil
func
rmBlocks
(
blocks
bs
.
GCBlockstore
,
pins
pin
.
Pinner
,
out
io
.
Writer
,
keys
[]
key
.
Key
)
error
{
var
unlocker
bs
.
Unlocker
defer
func
()
{
if
unlocker
!=
nil
{
unlocker
.
Unlock
()
}
}()
if
pins
!=
nil
{
// Need to make sure that some operation that is
// finishing with a pin is ocurr simultaneously.
unlocker
=
blocks
.
GCLock
()
err
:=
checkIfPinned
(
pins
,
keys
)
if
err
!=
nil
{
return
err
}
}
for
_
,
k
:=
range
keys
{
err
:=
blocks
.
DeleteBlock
(
k
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"%s: %s"
,
k
,
err
)
}
if
out
!=
nil
{
fmt
.
Fprintf
(
out
,
"deleted %s
\n
"
,
k
)
}
}
return
nil
}
func
checkIfPinned
(
pins
pin
.
Pinner
,
keys
[]
key
.
Key
)
error
{
for
_
,
k
:=
range
keys
{
reason
,
pinned
,
err
:=
pins
.
IsPinned
(
k
)
if
err
!=
nil
{
return
err
}
if
pinned
{
return
fmt
.
Errorf
(
"%s pinned via %s"
,
k
,
reason
)
}
}
return
nil
}
test/sharness/t0050-block.sh
浏览文件 @
453be22d
...
@@ -33,12 +33,79 @@ test_expect_success "'ipfs block stat' succeeds" '
...
@@ -33,12 +33,79 @@ test_expect_success "'ipfs block stat' succeeds" '
ipfs block stat $HASH >actual_stat
ipfs block stat $HASH >actual_stat
'
'
test_expect_success
"'ipfs block
ge
t' output looks good"
'
test_expect_success
"'ipfs block
sta
t' output looks good"
'
echo "Key: $HASH" >expected_stat &&
echo "Key: $HASH" >expected_stat &&
echo "Size: 12" >>expected_stat &&
echo "Size: 12" >>expected_stat &&
test_cmp expected_stat actual_stat
test_cmp expected_stat actual_stat
'
'
test_expect_success
"'ipfs block rm' succeeds"
'
ipfs block rm $HASH >actual_rm
'
test_expect_success
"'ipfs block rm' output looks good"
'
echo "deleted $HASH" > expected_rm &&
test_cmp expected_rm actual_rm
'
test_expect_success
"'ipfs block rm' block actually removed"
'
test_must_fail ipfs block stat $HASH
'
DIRHASH
=
QmdWmVmM6W2abTgkEfpbtA1CJyTWS2rhuUB9uP1xV8Uwtf
FILE1HASH
=
Qmae3RedM7SNkWGsdzYzsr6svmsFdsva4WoTvYYsWhUSVz
FILE2HASH
=
QmUtkGLvPf63NwVzLPKPUYgwhn8ZYPWF6vKWN3fZ2amfJF
FILE3HASH
=
Qmesmmf1EEG1orJb6XdK6DabxexsseJnCfw8pqWgonbkoj
test_expect_success
"add and pin directory"
'
mkdir adir &&
echo "file1" > adir/file1 &&
echo "file2" > adir/file2 &&
echo "file3" > adir/file3 &&
ipfs add -r adir
ipfs pin add -r $DIRHASH
'
test_expect_success
"can't remove pinned block"
'
test_must_fail ipfs block rm $DIRHASH 2> block_rm_err
'
test_expect_success
"can't remove pinned block: output looks good"
'
grep -q "$DIRHASH pinned via recursive" block_rm_err
'
test_expect_success
"can't remove indirectly pinned block"
'
test_must_fail ipfs block rm $FILE1HASH 2> block_rm_err
'
test_expect_success
"can't remove indirectly pinned block: output looks good"
'
grep -q "$FILE1HASH pinned via $DIRHASH" block_rm_err
'
test_expect_success
"multi-block 'ipfs block rm --ignore-pins' succeeds"
'
ipfs block rm --ignore-pins $DIRHASH >actual_rm
'
test_expect_success
"multi-block 'ipfs block rm --ignore-pins' output looks good"
'
echo "deleted $DIRHASH" > expected_rm &&
test_cmp expected_rm actual_rm
'
test_expect_success
"fix up pins"
'
ipfs pin rm -r $DIRHASH
'
test_expect_success
"multi-block 'ipfs block rm' succeeds"
'
ipfs block rm $FILE1HASH $FILE2HASH $FILE3HASH > actual_rm
'
test_expect_success
"multi-block 'ipfs block rm' output looks good"
'
echo "deleted $FILE1HASH" > expected_rm &&
echo "deleted $FILE2HASH" >> expected_rm &&
echo "deleted $FILE3HASH" >> expected_rm &&
test_cmp expected_rm actual_rm
'
test_expect_success
"'ipfs block stat' with nothing from stdin doesnt crash"
'
test_expect_success
"'ipfs block stat' with nothing from stdin doesnt crash"
'
test_expect_code 1 ipfs block stat < /dev/null 2> stat_out
test_expect_code 1 ipfs block stat < /dev/null 2> stat_out
'
'
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论