mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-20 02:21:48 +08:00
let rm understand paths
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -582,19 +582,59 @@ func rmLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
name := req.Arguments()[2]
|
||||
path := strings.Split(req.Arguments()[2], "/")
|
||||
|
||||
err = root.RemoveNodeLink(name)
|
||||
nnode, err := rmLink(req.Context(), nd.DAG, root, path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
newkey, err := nd.DAG.Add(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nnode.Key()
|
||||
}
|
||||
|
||||
return newkey, 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])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = ds.Add(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return root, nil
|
||||
}
|
||||
|
||||
nchild, err := root.GetNodeLink(path[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nd, err := nchild.GetNode(ctx, ds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nnode, err := rmLink(ctx, ds, nd, path[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = root.RemoveNodeLink(path[0])
|
||||
err = root.AddNodeLinkClean(path[0], nnode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = ds.Add(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return root, nil
|
||||
}
|
||||
|
||||
func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
|
||||
|
@ -146,6 +146,15 @@ test_object_cmd() {
|
||||
test_cmp rmlink_exp rmlink_output
|
||||
'
|
||||
|
||||
test_expect_success "multilayer rm-link should work" '
|
||||
ipfs object patch $(cat multi_patch) rm-link a/b/c > multi_link_rm_out
|
||||
'
|
||||
|
||||
test_expect_success "output looks good" '
|
||||
echo "QmZD3r9cZjzU8huNY2JS9TC6n8daDfT8TmE8zBSqG31Wvq" > multi_link_rm_exp &&
|
||||
test_cmp multi_link_rm_out multi_link_rm_exp
|
||||
'
|
||||
|
||||
test_expect_success "object patch --create works" '
|
||||
OUT=$(ipfs object patch --create $EMPTY add-link a/b/c $FILE)
|
||||
'
|
||||
@ -154,7 +163,6 @@ test_object_cmd() {
|
||||
ipfs cat $OUT/a/b/c > p2_hwfile &&
|
||||
test_cmp hwfile p2_hwfile
|
||||
'
|
||||
|
||||
}
|
||||
|
||||
# should work offline
|
||||
|
Reference in New Issue
Block a user