let rm understand paths
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jul 21, 2015 at 17:19 UTC
bfe4e4be4f0b2b07df21a24bb2d32eca8e5caf39
2 files changed
+54
-6
core/commands/object.go
+45
-5
@@ -582,19 +582,59 @@ func rmLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
582
return "", err
583
}
584
585
- name := req.Arguments()[2]
585
+ path := strings.Split(req.Arguments()[2], "/")
586
587
- err = root.RemoveNodeLink(name)
587
+ nnode, err := rmLink(req.Context(), nd.DAG, root, path)
588
if err != nil {
589
return "", err
590
}
591
592
- newkey, err := nd.DAG.Add(root)
592
+ return nnode.Key()
593
+}
594
+
595
+func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []string) (*dag.Node, error) {
596
+ if len(path) == 1 {
597
+ // base case, remove node in question
598
+ err := root.RemoveNodeLink(path[0])
599
+ if err != nil {
600
+ return nil, err
601
+ }
602
+
603
+ _, err = ds.Add(root)
604
+ if err != nil {
605
+ return nil, err
606
+ }
607
+
608
+ return root, nil
609
+ }
610
+
611
+ nchild, err := root.GetNodeLink(path[0])
612
if err != nil {
594
- return "", err
613
+ return nil, err
614
}
615
597
- return newkey, nil
616
+ nd, err := nchild.GetNode(ctx, ds)
617
+ if err != nil {
618
+ return nil, err
619
+ }
620
+
621
+ nnode, err := rmLink(ctx, ds, nd, path[1:])
622
+ if err != nil {
623
+ return nil, err
624
+ }
625
+
626
+ _ = root.RemoveNodeLink(path[0])
627
+ err = root.AddNodeLinkClean(path[0], nnode)
628
+ if err != nil {
629
+ return nil, err
630
+ }
631
+
632
+ _, err = ds.Add(root)
633
+ if err != nil {
634
+ return nil, err
635
+ }
636
+
637
+ return root, nil
638
}
639
640
func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
test/sharness/t0051-object.sh
+9
-1
@@ -146,6 +146,15 @@ test_object_cmd() {
146
test_cmp rmlink_exp rmlink_output
147
'
148
149
+ test_expect_success "multilayer rm-link should work" '
150
+ ipfs object patch $(cat multi_patch) rm-link a/b/c > multi_link_rm_out
151
+ '
152
+
153
+ test_expect_success "output looks good" '
154
+ echo "QmZD3r9cZjzU8huNY2JS9TC6n8daDfT8TmE8zBSqG31Wvq" > multi_link_rm_exp &&
155
+ test_cmp multi_link_rm_out multi_link_rm_exp
156
+ '
157
+
158
test_expect_success "object patch --create works" '
159
OUT=$(ipfs object patch --create $EMPTY add-link a/b/c $FILE)
160
'
@@ -154,7 +163,6 @@ test_object_cmd() {
163
ipfs cat $OUT/a/b/c > p2_hwfile &&
164
test_cmp hwfile p2_hwfile
165
'
157
-
166
}
167
168
# should work offline