Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
49946c69
提交
49946c69
authored
9月 20, 2018
作者:
Łukasz Magiera
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
coreapi unixfs: pin/local/hash-only options
License: MIT Signed-off-by:
Łukasz Magiera
<
magik6k@gmail.com
>
上级
c056e5a8
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
138 行增加
和
4 行删除
+138
-4
unixfs.go
core/coreapi/interface/options/unixfs.go
+29
-0
unixfs.go
core/coreapi/unixfs.go
+53
-4
unixfs_test.go
core/coreapi/unixfs_test.go
+56
-0
没有找到文件。
core/coreapi/interface/options/unixfs.go
浏览文件 @
49946c69
...
...
@@ -21,6 +21,10 @@ type UnixfsAddSettings struct {
Chunker
string
Layout
Layout
Pin
bool
OnlyHash
bool
Local
bool
}
type
UnixfsAddOption
func
(
*
UnixfsAddSettings
)
error
...
...
@@ -36,6 +40,10 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
Chunker
:
"size-262144"
,
Layout
:
BalancedLayout
,
Pin
:
false
,
OnlyHash
:
false
,
Local
:
false
,
}
for
_
,
opt
:=
range
opts
{
...
...
@@ -94,3 +102,24 @@ func (unixfsOpts) Layout(layout Layout) UnixfsAddOption {
return
nil
}
}
func
(
unixfsOpts
)
Pin
(
pin
bool
)
UnixfsAddOption
{
return
func
(
settings
*
UnixfsAddSettings
)
error
{
settings
.
Pin
=
pin
return
nil
}
}
func
(
unixfsOpts
)
HashOnly
(
hashOnly
bool
)
UnixfsAddOption
{
return
func
(
settings
*
UnixfsAddSettings
)
error
{
settings
.
OnlyHash
=
hashOnly
return
nil
}
}
func
(
unixfsOpts
)
Local
(
local
bool
)
UnixfsAddOption
{
return
func
(
settings
*
UnixfsAddSettings
)
error
{
settings
.
Local
=
local
return
nil
}
}
core/coreapi/unixfs.go
浏览文件 @
49946c69
...
...
@@ -4,18 +4,25 @@ import (
"context"
"errors"
"fmt"
"github.com/ipfs/go-ipfs/core"
"io"
coreiface
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
"github.com/ipfs/go-ipfs/core/coreunix"
uio
"gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
mh
"gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
cidutil
"gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
offline
"gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
ft
"gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio
"gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
mfs
"gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs"
dag
"gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
dagtest
"gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag/test"
blockservice
"gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
ipld
"gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
bstore
"gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore"
)
type
UnixfsAPI
CoreAPI
...
...
@@ -59,7 +66,33 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
prefix
.
MhType
=
settings
.
MhType
prefix
.
MhLength
=
-
1
fileAdder
,
err
:=
coreunix
.
NewAdder
(
ctx
,
api
.
node
.
Pinning
,
api
.
node
.
Blockstore
,
api
.
node
.
DAG
)
n
:=
api
.
node
if
settings
.
OnlyHash
{
nilnode
,
err
:=
core
.
NewNode
(
ctx
,
&
core
.
BuildCfg
{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo
:
true
,
})
if
err
!=
nil
{
return
nil
,
err
}
n
=
nilnode
}
addblockstore
:=
n
.
Blockstore
//if !(fscache || nocopy) {
addblockstore
=
bstore
.
NewGCBlockstore
(
n
.
BaseBlocks
,
n
.
GCLocker
)
//}
exch
:=
n
.
Exchange
if
settings
.
Local
{
exch
=
offline
.
Exchange
(
addblockstore
)
}
bserv
:=
blockservice
.
New
(
addblockstore
,
exch
)
// hash security 001
dserv
:=
dag
.
NewDAGService
(
bserv
)
fileAdder
,
err
:=
coreunix
.
NewAdder
(
ctx
,
n
.
Pinning
,
n
.
Blockstore
,
dserv
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -68,7 +101,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
//fileAdder.Progress = progress
//fileAdder.Hidden = hidden
//fileAdder.Wrap = wrap
//fileAdder.Pin = dop
in
fileAdder
.
Pin
=
settings
.
P
in
fileAdder
.
Silent
=
true
fileAdder
.
RawLeaves
=
settings
.
RawLeaves
//fileAdder.NoCopy = nocopy
...
...
@@ -91,6 +124,19 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
}
}
if
settings
.
OnlyHash
{
md
:=
dagtest
.
Mock
()
emptyDirNode
:=
ft
.
EmptyDirNode
()
// Use the same prefix for the "empty" MFS root as for the file adder.
emptyDirNode
.
SetCidBuilder
(
fileAdder
.
CidBuilder
)
mr
,
err
:=
mfs
.
NewRoot
(
ctx
,
md
,
emptyDirNode
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
fileAdder
.
SetMfsRoot
(
mr
)
}
err
=
fileAdder
.
AddFile
(
files
.
NewReaderFile
(
""
,
""
,
r
,
nil
))
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -101,8 +147,11 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
return
nil
,
err
}
return
coreiface
.
IpfsPath
(
nd
.
Cid
()),
err
if
settings
.
Pin
{
err
=
fileAdder
.
PinRoot
()
}
return
coreiface
.
IpfsPath
(
nd
.
Cid
()),
err
}
// Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.
...
...
core/coreapi/unixfs_test.go
浏览文件 @
49946c69
...
...
@@ -204,6 +204,13 @@ func TestAdd(t *testing.T) {
path
:
"/ipfs/QmNNhDGttafX3M1wKWixGre6PrLFGjnoPEDXjBYpTv93HP"
,
opts
:
[]
options
.
UnixfsAddOption
{
options
.
Unixfs
.
Chunker
(
"size-4"
),
options
.
Unixfs
.
Layout
(
options
.
TrickleLeyout
)},
},
// Local
{
name
:
"addLocal"
,
// better cases in sharness
data
:
helloStr
,
path
:
hello
,
opts
:
[]
options
.
UnixfsAddOption
{
options
.
Unixfs
.
Local
(
true
)},
},
}
for
_
,
testCase
:=
range
cases
{
...
...
@@ -244,6 +251,55 @@ func TestAdd(t *testing.T) {
}
}
func
TestAddPinned
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
_
,
api
,
err
:=
makeAPI
(
ctx
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
str
:=
strings
.
NewReader
(
helloStr
)
_
,
err
=
api
.
Unixfs
()
.
Add
(
ctx
,
ioutil
.
NopCloser
(
str
),
options
.
Unixfs
.
Pin
(
true
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
pins
,
err
:=
api
.
Pin
()
.
Ls
(
ctx
)
if
len
(
pins
)
!=
1
{
t
.
Fatalf
(
"expected 1 pin, got %d"
,
len
(
pins
))
}
if
pins
[
0
]
.
Path
()
.
String
()
!=
"/ipld/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
{
t
.
Fatalf
(
"got unexpected pin: %s"
,
pins
[
0
]
.
Path
()
.
String
())
}
}
func
TestAddHashOnly
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
_
,
api
,
err
:=
makeAPI
(
ctx
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
str
:=
strings
.
NewReader
(
helloStr
)
p
,
err
:=
api
.
Unixfs
()
.
Add
(
ctx
,
ioutil
.
NopCloser
(
str
),
options
.
Unixfs
.
HashOnly
(
true
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
p
.
String
()
!=
hello
{
t
.
Errorf
(
"unxepected path: %s"
,
p
.
String
())
}
_
,
err
=
api
.
Block
()
.
Get
(
ctx
,
p
)
if
err
==
nil
{
t
.
Fatal
(
"expected an error"
)
}
if
err
.
Error
()
!=
"blockservice: key not found"
{
t
.
Errorf
(
"unxepected error: %s"
,
err
.
Error
())
}
}
func
TestCatEmptyFile
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
node
,
api
,
err
:=
makeAPI
(
ctx
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论