Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
ae1eb0d7
提交
ae1eb0d7
authored
2月 04, 2019
作者:
Łukasz Magiera
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
block put --pin option
License: MIT Signed-off-by:
Łukasz Magiera
<
magik6k@gmail.com
>
上级
803e9a7e
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
66 行增加
和
2 行删除
+66
-2
block.go
core/commands/block.go
+7
-1
block.go
core/coreapi/block.go
+10
-1
block.go
core/coreapi/interface/options/block.go
+11
-0
block.go
core/coreapi/interface/tests/block.go
+38
-0
没有找到文件。
core/commands/block.go
浏览文件 @
ae1eb0d7
...
...
@@ -146,6 +146,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
cmdkit
.
StringOption
(
blockFormatOptionName
,
"f"
,
"cid format for blocks to be created with."
),
cmdkit
.
StringOption
(
mhtypeOptionName
,
"multihash hash function"
)
.
WithDefault
(
"sha2-256"
),
cmdkit
.
IntOption
(
mhlenOptionName
,
"multihash hash length"
)
.
WithDefault
(
-
1
),
cmdkit
.
BoolOption
(
pinOptionName
,
"pin added blocks recursively"
)
.
WithDefault
(
false
),
},
Run
:
func
(
req
*
cmds
.
Request
,
res
cmds
.
ResponseEmitter
,
env
cmds
.
Environment
)
error
{
api
,
err
:=
cmdenv
.
GetApi
(
env
,
req
)
...
...
@@ -178,7 +179,12 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
}
}
p
,
err
:=
api
.
Block
()
.
Put
(
req
.
Context
,
file
,
options
.
Block
.
Hash
(
mhtval
,
mhlen
),
options
.
Block
.
Format
(
format
))
pin
,
_
:=
req
.
Options
[
pinOptionName
]
.
(
bool
)
p
,
err
:=
api
.
Block
()
.
Put
(
req
.
Context
,
file
,
options
.
Block
.
Hash
(
mhtval
,
mhlen
),
options
.
Block
.
Format
(
format
),
options
.
Block
.
Pin
(
pin
))
if
err
!=
nil
{
return
err
}
...
...
core/coreapi/block.go
浏览文件 @
ae1eb0d7
...
...
@@ -10,6 +10,7 @@ import (
util
"github.com/ipfs/go-ipfs/blocks/blockstoreutil"
coreiface
"github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
pin
"github.com/ipfs/go-ipfs/pin"
cid
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
blocks
"gx/ipfs/QmWoXtvgC8inqFkAATB7cp2Dax7XBi9VDvSg9RCCZufmRk/go-block-format"
...
...
@@ -23,7 +24,7 @@ type BlockStat struct {
}
func
(
api
*
BlockAPI
)
Put
(
ctx
context
.
Context
,
src
io
.
Reader
,
opts
...
caopts
.
BlockPutOption
)
(
coreiface
.
BlockStat
,
error
)
{
_
,
pref
,
err
:=
caopts
.
BlockPutOptions
(
opts
...
)
settings
,
pref
,
err
:=
caopts
.
BlockPutOptions
(
opts
...
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -43,11 +44,19 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return
nil
,
err
}
if
settings
.
Pin
{
defer
api
.
blockstore
.
PinLock
()
.
Unlock
()
}
err
=
api
.
blocks
.
AddBlock
(
b
)
if
err
!=
nil
{
return
nil
,
err
}
if
settings
.
Pin
{
api
.
pinning
.
PinWithMode
(
b
.
Cid
(),
pin
.
Recursive
)
}
return
&
BlockStat
{
path
:
coreiface
.
IpldPath
(
b
.
Cid
()),
size
:
len
(
data
)},
nil
}
...
...
core/coreapi/interface/options/block.go
浏览文件 @
ae1eb0d7
...
...
@@ -10,6 +10,7 @@ type BlockPutSettings struct {
Codec
string
MhType
uint64
MhLength
int
Pin
bool
}
type
BlockRmSettings
struct
{
...
...
@@ -24,6 +25,7 @@ func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, err
Codec
:
""
,
MhType
:
mh
.
SHA2_256
,
MhLength
:
-
1
,
Pin
:
false
,
}
for
_
,
opt
:=
range
opts
{
...
...
@@ -105,6 +107,15 @@ func (blockOpts) Hash(mhType uint64, mhLen int) BlockPutOption {
}
}
// Pin is an option for Block.Put which specifies whether to (recursively) pin
// added blocks
func
(
blockOpts
)
Pin
(
pin
bool
)
BlockPutOption
{
return
func
(
settings
*
BlockPutSettings
)
error
{
settings
.
Pin
=
pin
return
nil
}
}
// Force is an option for Block.Rm which, when set to true, will ignore
// non-existing blocks
func
(
blockOpts
)
Force
(
force
bool
)
BlockRmOption
{
...
...
core/coreapi/interface/tests/block.go
浏览文件 @
ae1eb0d7
...
...
@@ -26,6 +26,7 @@ func (tp *provider) TestBlock(t *testing.T) {
t
.
Run
(
"TestBlockGet"
,
tp
.
TestBlockGet
)
t
.
Run
(
"TestBlockRm"
,
tp
.
TestBlockRm
)
t
.
Run
(
"TestBlockStat"
,
tp
.
TestBlockStat
)
t
.
Run
(
"TestBlockPin"
,
tp
.
TestBlockPin
)
}
func
(
tp
*
provider
)
TestBlockPut
(
t
*
testing
.
T
)
{
...
...
@@ -203,3 +204,40 @@ func (tp *provider) TestBlockStat(t *testing.T) {
t
.
Error
(
"length doesn't match"
)
}
}
func
(
tp
*
provider
)
TestBlockPin
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
api
,
err
:=
tp
.
makeAPI
(
ctx
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
_
,
err
=
api
.
Block
()
.
Put
(
ctx
,
strings
.
NewReader
(
`Hello`
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
pins
,
err
:=
api
.
Pin
()
.
Ls
(
ctx
);
err
!=
nil
||
len
(
pins
)
!=
0
{
t
.
Fatal
(
"expected 0 pins"
)
}
res
,
err
:=
api
.
Block
()
.
Put
(
ctx
,
strings
.
NewReader
(
`Hello`
),
opt
.
Block
.
Pin
(
true
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
pins
,
err
:=
api
.
Pin
()
.
Ls
(
ctx
)
if
err
!=
nil
{
return
}
if
len
(
pins
)
!=
1
{
t
.
Fatal
(
"expected 1 pin"
)
}
if
pins
[
0
]
.
Type
()
!=
"recursive"
{
t
.
Error
(
"expected a recursive pin"
)
}
if
pins
[
0
]
.
Path
()
.
String
()
!=
res
.
Path
()
.
String
()
{
t
.
Error
(
"pin path didn't match"
)
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论