@@ -18,6 +18,7 @@ import (
18
18
cmds "github.com/ipfs/go-ipfs/commands"
19
19
core "github.com/ipfs/go-ipfs/core"
20
20
dag "github.com/ipfs/go-ipfs/merkledag"
21
+ dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
21
22
path "github.com/ipfs/go-ipfs/path"
22
23
ft "github.com/ipfs/go-ipfs/unixfs"
23
24
u "github.com/ipfs/go-ipfs/util"
@@ -453,7 +454,9 @@ This removes the link named foo from the hash in $FOO_BAR and returns the
453
454
resulting object hash.
454
455
` ,
455
456
},
456
- Options : []cmds.Option {},
457
+ Options : []cmds.Option {
458
+ cmds .BoolOption ("create" , "p" , "create intermediate directories on add-link" ),
459
+ },
457
460
Arguments : []cmds.Argument {
458
461
cmds .StringArg ("root" , true , false , "the hash of the node to modify" ),
459
462
cmds .StringArg ("command" , true , false , "the operation to perform" ),
@@ -467,9 +470,13 @@ resulting object hash.
467
470
return
468
471
}
469
472
470
- rhash := key .B58KeyDecode (req .Arguments ()[0 ])
473
+ rootarg := req .Arguments ()[0 ]
474
+ if strings .HasPrefix (rootarg , "/ipfs/" ) {
475
+ rootarg = rootarg [6 :]
476
+ }
477
+ rhash := key .B58KeyDecode (rootarg )
471
478
if rhash == "" {
472
- res .SetError (fmt .Errorf ("incorrectly formatted root hash" ), cmds .ErrNormal )
479
+ res .SetError (fmt .Errorf ("incorrectly formatted root hash: %s" , req . Arguments ()[ 0 ] ), cmds .ErrNormal )
473
480
return
474
481
}
475
482
@@ -580,19 +587,18 @@ func rmLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
580
587
return "" , err
581
588
}
582
589
583
- name := req .Arguments ()[2 ]
590
+ path := req .Arguments ()[2 ]
584
591
585
- err = root .RemoveNodeLink (name )
586
- if err != nil {
587
- return "" , err
588
- }
592
+ e := dagutils .NewDagEditor (nd .DAG , root )
589
593
590
- newkey , err := nd . DAG . Add ( root )
594
+ err = e . RmLink ( req . Context (), path )
591
595
if err != nil {
592
596
return "" , err
593
597
}
594
598
595
- return newkey , nil
599
+ nnode := e .GetNode ()
600
+
601
+ return nnode .Key ()
596
602
}
597
603
598
604
func addLinkCaller (req cmds.Request , root * dag.Node ) (key.Key , error ) {
@@ -608,72 +614,28 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
608
614
path := req .Arguments ()[2 ]
609
615
childk := key .B58KeyDecode (req .Arguments ()[3 ])
610
616
611
- parts := strings .Split (path , "/" )
612
-
613
- nnode , err := insertNodeAtPath (req .Context (), nd .DAG , root , parts , childk )
617
+ create , _ , err := req .Option ("create" ).Bool ()
614
618
if err != nil {
615
619
return "" , err
616
620
}
617
- return nnode .Key ()
618
- }
619
-
620
- func addLink (ctx context.Context , ds dag.DAGService , root * dag.Node , childname string , childk key.Key ) (* dag.Node , error ) {
621
- ctx , cancel := context .WithTimeout (ctx , time .Second * 30 )
622
- childnd , err := ds .Get (ctx , childk )
623
- if err != nil {
624
- cancel ()
625
- return nil , err
626
- }
627
- cancel ()
628
-
629
- err = root .AddNodeLinkClean (childname , childnd )
630
- if err != nil {
631
- return nil , err
632
- }
633
-
634
- _ , err = ds .Add (root )
635
- if err != nil {
636
- return nil , err
637
- }
638
- return root , nil
639
- }
640
-
641
- func insertNodeAtPath (ctx context.Context , ds dag.DAGService , root * dag.Node , path []string , toinsert key.Key ) (* dag.Node , error ) {
642
- if len (path ) == 1 {
643
- return addLink (ctx , ds , root , path [0 ], toinsert )
644
- }
645
-
646
- child , err := root .GetNodeLink (path [0 ])
647
- if err != nil {
648
- return nil , err
649
- }
650
-
651
- nd , err := child .GetNode (ctx , ds )
652
- if err != nil {
653
- return nil , err
654
- }
655
621
656
- ndprime , err := insertNodeAtPath (ctx , ds , nd , path [1 :], toinsert )
657
- if err != nil {
658
- return nil , err
622
+ var createfunc func () * dag.Node
623
+ if create {
624
+ createfunc = func () * dag.Node {
625
+ return & dag.Node {Data : ft .FolderPBData ()}
626
+ }
659
627
}
660
628
661
- err = root .RemoveNodeLink (path [0 ])
662
- if err != nil {
663
- return nil , err
664
- }
629
+ e := dagutils .NewDagEditor (nd .DAG , root )
665
630
666
- err = root . AddNodeLinkClean ( path [ 0 ], ndprime )
631
+ err = e . InsertNodeAtPath ( req . Context (), path , childk , createfunc )
667
632
if err != nil {
668
- return nil , err
633
+ return "" , err
669
634
}
670
635
671
- _ , err = ds .Add (root )
672
- if err != nil {
673
- return nil , err
674
- }
636
+ nnode := e .GetNode ()
675
637
676
- return root , nil
638
+ return nnode . Key ()
677
639
}
678
640
679
641
func nodeFromTemplate (template string ) (* dag.Node , error ) {
0 commit comments