From bfe4e4be4f0b2b07df21a24bb2d32eca8e5caf39 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 21 Jul 2015 17:19:17 -0700 Subject: [PATCH] let rm understand paths License: MIT Signed-off-by: Jeromy --- core/commands/object.go | 52 +++++++++++++++++++++++++++++++---- test/sharness/t0051-object.sh | 10 ++++++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/core/commands/object.go b/core/commands/object.go index 99e9c90d8..d123083bb 100644 --- a/core/commands/object.go +++ b/core/commands/object.go @@ -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() +} + +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 } - return newkey, 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) { diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index e3dd69c88..2e1d442d5 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -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