Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
b32d0ef1
提交
b32d0ef1
authored
7月 28, 2015
作者:
Jeromy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implement 'editor' abstraction
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
上级
314f7bbf
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
127 行增加
和
55 行删除
+127
-55
object.go
core/commands/object.go
+19
-5
node.go
merkledag/node.go
+15
-5
utils.go
merkledag/utils/utils.go
+54
-12
utils_test.go
merkledag/utils/utils_test.go
+39
-33
没有找到文件。
core/commands/object.go
浏览文件 @
b32d0ef1
...
...
@@ -587,13 +587,17 @@ func rmLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
return
""
,
err
}
path
:=
strings
.
Split
(
req
.
Arguments
()[
2
],
"/"
)
path
:=
req
.
Arguments
()[
2
]
e
:=
dagutils
.
NewDagEditor
(
nd
.
DAG
,
root
)
nnode
,
err
:=
dagutils
.
RmLink
(
req
.
Context
(),
nd
.
DAG
,
root
,
path
)
err
=
e
.
RmLink
(
req
.
Context
()
,
path
)
if
err
!=
nil
{
return
""
,
err
}
nnode
:=
e
.
GetNode
()
return
nnode
.
Key
()
}
...
...
@@ -610,17 +614,27 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
path
:=
req
.
Arguments
()[
2
]
childk
:=
key
.
B58KeyDecode
(
req
.
Arguments
()[
3
])
parts
:=
strings
.
Split
(
path
,
"/"
)
create
,
_
,
err
:=
req
.
Option
(
"create"
)
.
Bool
()
if
err
!=
nil
{
return
""
,
err
}
nnode
,
err
:=
dagutils
.
InsertNodeAtPath
(
req
.
Context
(),
nd
.
DAG
,
root
,
parts
,
childk
,
create
)
var
createfunc
func
()
*
dag
.
Node
if
create
{
createfunc
=
func
()
*
dag
.
Node
{
return
&
dag
.
Node
{
Data
:
ft
.
FolderPBData
()}
}
}
e
:=
dagutils
.
NewDagEditor
(
nd
.
DAG
,
root
)
err
=
e
.
InsertNodeAtPath
(
req
.
Context
(),
path
,
childk
,
createfunc
)
if
err
!=
nil
{
return
""
,
err
}
nnode
:=
e
.
GetNode
()
return
nnode
.
Key
()
}
...
...
merkledag/node.go
浏览文件 @
b32d0ef1
...
...
@@ -129,13 +129,23 @@ func (n *Node) AddRawLink(name string, l *Link) error {
// Remove a link on this node by the given name
func
(
n
*
Node
)
RemoveNodeLink
(
name
string
)
error
{
n
.
encoded
=
nil
for
i
,
l
:=
range
n
.
Links
{
if
l
.
Name
==
name
{
n
.
Links
=
append
(
n
.
Links
[
:
i
],
n
.
Links
[
i
+
1
:
]
...
)
return
nil
good
:=
make
([]
*
Link
,
0
,
len
(
n
.
Links
))
var
found
bool
for
_
,
l
:=
range
n
.
Links
{
if
l
.
Name
!=
name
{
good
=
append
(
good
,
l
)
}
else
{
found
=
true
}
}
return
ErrNotFound
n
.
Links
=
good
if
!
found
{
return
ErrNotFound
}
return
nil
}
// Return a copy of the link with given name
...
...
merkledag/utils/utils.go
浏览文件 @
b32d0ef1
...
...
@@ -2,22 +2,44 @@ package dagutils
import
(
"errors"
"
time
"
"
strings
"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
key
"github.com/ipfs/go-ipfs/blocks/key"
dag
"github.com/ipfs/go-ipfs/merkledag"
ft
"github.com/ipfs/go-ipfs/unixfs"
)
func
AddLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
childname
string
,
childk
key
.
Key
)
(
*
dag
.
Node
,
error
)
{
type
Editor
struct
{
root
*
dag
.
Node
ds
dag
.
DAGService
}
func
NewDagEditor
(
ds
dag
.
DAGService
,
root
*
dag
.
Node
)
*
Editor
{
return
&
Editor
{
root
:
root
,
ds
:
ds
,
}
}
func
(
e
*
Editor
)
GetNode
()
*
dag
.
Node
{
return
e
.
root
.
Copy
()
}
func
(
e
*
Editor
)
AddLink
(
ctx
context
.
Context
,
childname
string
,
childk
key
.
Key
)
error
{
nd
,
err
:=
addLink
(
ctx
,
e
.
ds
,
e
.
root
,
childname
,
childk
)
if
err
!=
nil
{
return
err
}
e
.
root
=
nd
return
nil
}
func
addLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
childname
string
,
childk
key
.
Key
)
(
*
dag
.
Node
,
error
)
{
if
childname
==
""
{
return
nil
,
errors
.
New
(
"cannot create link with no name!"
)
}
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
30
)
defer
cancel
()
childnd
,
err
:=
ds
.
Get
(
ctx
,
childk
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -38,22 +60,32 @@ func AddLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
return
root
,
nil
}
func
InsertNodeAtPath
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
,
toinsert
key
.
Key
,
create
bool
)
(
*
dag
.
Node
,
error
)
{
func
(
e
*
Editor
)
InsertNodeAtPath
(
ctx
context
.
Context
,
path
string
,
toinsert
key
.
Key
,
create
func
()
*
dag
.
Node
)
error
{
splpath
:=
strings
.
Split
(
path
,
"/"
)
nd
,
err
:=
insertNodeAtPath
(
ctx
,
e
.
ds
,
e
.
root
,
splpath
,
toinsert
,
create
)
if
err
!=
nil
{
return
err
}
e
.
root
=
nd
return
nil
}
func
insertNodeAtPath
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
,
toinsert
key
.
Key
,
create
func
()
*
dag
.
Node
)
(
*
dag
.
Node
,
error
)
{
if
len
(
path
)
==
1
{
return
A
ddLink
(
ctx
,
ds
,
root
,
path
[
0
],
toinsert
)
return
a
ddLink
(
ctx
,
ds
,
root
,
path
[
0
],
toinsert
)
}
nd
,
err
:=
root
.
GetLinkedNode
(
ctx
,
ds
,
path
[
0
])
if
err
!=
nil
{
// if 'create' is true, we create directories on the way down as needed
if
err
==
dag
.
ErrNotFound
&&
create
{
nd
=
&
dag
.
Node
{
Data
:
ft
.
FolderPBData
()}
if
err
==
dag
.
ErrNotFound
&&
create
!=
nil
{
nd
=
create
()
}
else
{
return
nil
,
err
}
}
ndprime
,
err
:=
I
nsertNodeAtPath
(
ctx
,
ds
,
nd
,
path
[
1
:
],
toinsert
,
create
)
ndprime
,
err
:=
i
nsertNodeAtPath
(
ctx
,
ds
,
nd
,
path
[
1
:
],
toinsert
,
create
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -72,7 +104,17 @@ func InsertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, pa
return
root
,
nil
}
func
RmLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
)
(
*
dag
.
Node
,
error
)
{
func
(
e
*
Editor
)
RmLink
(
ctx
context
.
Context
,
path
string
)
error
{
splpath
:=
strings
.
Split
(
path
,
"/"
)
nd
,
err
:=
rmLink
(
ctx
,
e
.
ds
,
e
.
root
,
splpath
)
if
err
!=
nil
{
return
err
}
e
.
root
=
nd
return
nil
}
func
rmLink
(
ctx
context
.
Context
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
)
(
*
dag
.
Node
,
error
)
{
if
len
(
path
)
==
1
{
// base case, remove node in question
err
:=
root
.
RemoveNodeLink
(
path
[
0
])
...
...
@@ -93,7 +135,7 @@ func RmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin
return
nil
,
err
}
nnode
,
err
:=
R
mLink
(
ctx
,
ds
,
nd
,
path
[
1
:
])
nnode
,
err
:=
r
mLink
(
ctx
,
ds
,
nd
,
path
[
1
:
])
if
err
!=
nil
{
return
nil
,
err
}
...
...
merkledag/utils/utils_test.go
浏览文件 @
b32d0ef1
package
dagutils
import
(
"strings"
"testing"
key
"github.com/ipfs/go-ipfs/blocks/key"
...
...
@@ -22,7 +23,7 @@ func TestAddLink(t *testing.T) {
}
nd
:=
new
(
dag
.
Node
)
nnode
,
err
:=
A
ddLink
(
context
.
Background
(),
ds
,
nd
,
"fish"
,
fk
)
nnode
,
err
:=
a
ddLink
(
context
.
Background
(),
ds
,
nd
,
"fish"
,
fk
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -42,9 +43,10 @@ func TestAddLink(t *testing.T) {
}
}
func
assertNodeAtPath
(
t
*
testing
.
T
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
[]
string
,
exp
key
.
Key
)
{
func
assertNodeAtPath
(
t
*
testing
.
T
,
ds
dag
.
DAGService
,
root
*
dag
.
Node
,
path
string
,
exp
key
.
Key
)
{
parts
:=
strings
.
Split
(
path
,
"/"
)
cur
:=
root
for
_
,
e
:=
range
pa
th
{
for
_
,
e
:=
range
pa
rts
{
nxt
,
err
:=
cur
.
GetLinkedNode
(
context
.
Background
(),
ds
,
e
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -66,53 +68,57 @@ func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path []st
func
TestInsertNode
(
t
*
testing
.
T
)
{
ds
:=
mdtest
.
Mock
(
t
)
root
:=
new
(
dag
.
Node
)
e
:=
NewDagEditor
(
ds
,
root
)
childa
:=
&
dag
.
Node
{
Data
:
[]
byte
(
"This is child A"
),
}
ak
,
err
:=
ds
.
Add
(
childa
)
testInsert
(
t
,
e
,
"a"
,
"anodefortesting"
,
false
,
""
)
testInsert
(
t
,
e
,
"a/b"
,
"data"
,
false
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/e"
,
"blah"
,
false
,
"merkledag: not found"
)
testInsert
(
t
,
e
,
"a/b/c/d/e"
,
"foo"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"baz"
,
true
,
""
)
testInsert
(
t
,
e
,
"a/b/c/d/f"
,
"bar"
,
true
,
""
)
testInsert
(
t
,
e
,
""
,
"bar"
,
true
,
"cannot create link with no name!"
)
testInsert
(
t
,
e
,
"////"
,
"slashes"
,
true
,
"cannot create link with no name!"
)
k
,
err
:=
e
.
GetNode
()
.
Key
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
path
:=
[]
string
{
"a"
,
"b"
,
"c"
,
"d"
}
root_a
,
err
:=
InsertNodeAtPath
(
context
.
Background
(),
ds
,
root
,
path
,
ak
,
true
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
if
k
.
B58String
()
!=
"QmThorWojP6YzLJwDukxiYCoKQSwyrMCvdt4WZ6rPm221t"
{
t
.
Fatal
(
"output was different than expected"
)
}
assertNodeAtPath
(
t
,
ds
,
root_a
,
path
,
ak
)
}
childb
:=
&
dag
.
Node
{
Data
:
[]
byte
(
"this is the second child"
)}
bk
,
err
:=
ds
.
Add
(
childb
)
func
testInsert
(
t
*
testing
.
T
,
e
*
Editor
,
path
,
data
string
,
create
bool
,
experr
string
)
{
child
:=
&
dag
.
Node
{
Data
:
[]
byte
(
data
)}
ck
,
err
:=
e
.
ds
.
Add
(
child
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// this one should fail, we are specifying a non-existant path
// with create == false
path2
:=
[]
string
{
"a"
,
"b"
,
"e"
,
"f"
}
_
,
err
=
InsertNodeAtPath
(
context
.
Background
(),
ds
,
root_a
,
path2
,
bk
,
false
)
if
err
==
nil
{
t
.
Fatal
(
"that shouldnt have worked"
)
}
if
err
!=
dag
.
ErrNotFound
{
t
.
Fatal
(
"expected this to fail with 'not found'"
)
var
c
func
()
*
dag
.
Node
if
create
{
c
=
func
()
*
dag
.
Node
{
return
&
dag
.
Node
{}
}
}
// inserting a path of length one should work with create == false
path3
:=
[]
string
{
"x"
}
root_b
,
err
:=
InsertNodeAtPath
(
context
.
Background
(),
ds
,
root_a
,
path3
,
bk
,
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
err
=
e
.
InsertNodeAtPath
(
context
.
TODO
(),
path
,
ck
,
c
)
if
experr
!=
""
{
var
got
string
if
err
!=
nil
{
got
=
err
.
Error
()
}
if
got
!=
experr
{
t
.
Fatalf
(
"expected '%s' but got '%s'"
,
experr
,
got
)
}
return
}
assertNodeAtPath
(
t
,
ds
,
root_b
,
path3
,
bk
)
// now try overwriting a path
root_c
,
err
:=
InsertNodeAtPath
(
context
.
Background
(),
ds
,
root_b
,
path
,
bk
,
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
assertNodeAtPath
(
t
,
ds
,
root_c
,
path
,
b
k
)
assertNodeAtPath
(
t
,
e
.
ds
,
e
.
root
,
path
,
c
k
)
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论