@@ -453,7 +453,9 @@ This removes the link named foo from the hash in $FOO_BAR and returns the
453
453
resulting object hash.
454
454
` ,
455
455
},
456
- Options : []cmds.Option {},
456
+ Options : []cmds.Option {
457
+ cmds .BoolOption ("create" , "p" , "create intermediate directories on add-link" ),
458
+ },
457
459
Arguments : []cmds.Argument {
458
460
cmds .StringArg ("root" , true , false , "the hash of the node to modify" ),
459
461
cmds .StringArg ("command" , true , false , "the operation to perform" ),
@@ -610,7 +612,12 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
610
612
611
613
parts := strings .Split (path , "/" )
612
614
613
- nnode , err := insertNodeAtPath (req .Context (), nd .DAG , root , parts , childk )
615
+ create , _ , err := req .Option ("create" ).Bool ()
616
+ if err != nil {
617
+ return "" , err
618
+ }
619
+
620
+ nnode , err := insertNodeAtPath (req .Context (), nd .DAG , root , parts , childk , create )
614
621
if err != nil {
615
622
return "" , err
616
623
}
@@ -619,12 +626,14 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
619
626
620
627
func addLink (ctx context.Context , ds dag.DAGService , root * dag.Node , childname string , childk key.Key ) (* dag.Node , error ) {
621
628
ctx , cancel := context .WithTimeout (ctx , time .Second * 30 )
629
+ defer cancel ()
622
630
childnd , err := ds .Get (ctx , childk )
623
631
if err != nil {
624
- cancel ()
625
632
return nil , err
626
633
}
627
- cancel ()
634
+
635
+ // ensure no link with that name already exists
636
+ _ = root .RemoveNodeLink (childname ) // ignore error, only option is ErrNotFound
628
637
629
638
err = root .AddNodeLinkClean (childname , childnd )
630
639
if err != nil {
@@ -638,31 +647,33 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
638
647
return root , nil
639
648
}
640
649
641
- func insertNodeAtPath (ctx context.Context , ds dag.DAGService , root * dag.Node , path []string , toinsert key.Key ) (* dag.Node , error ) {
650
+ func insertNodeAtPath (ctx context.Context , ds dag.DAGService , root * dag.Node , path []string , toinsert key.Key , create bool ) (* dag.Node , error ) {
642
651
if len (path ) == 1 {
643
652
return addLink (ctx , ds , root , path [0 ], toinsert )
644
653
}
645
654
655
+ var nd * dag.Node
646
656
child , err := root .GetNodeLink (path [0 ])
647
657
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
-
656
- ndprime , err := insertNodeAtPath ( ctx , ds , nd , path [ 1 :], toinsert )
657
- if err != nil {
658
- return nil , err
658
+ // if 'create' is true, we create directories on the way down as needed
659
+ if err == dag . ErrNotFound && create {
660
+ nd = & dag. Node { Data : ft . FolderPBData ()}
661
+ } else {
662
+ return nil , err
663
+ }
664
+ } else {
665
+ nd , err = child . GetNode ( ctx , ds )
666
+ if err != nil {
667
+ return nil , err
668
+ }
659
669
}
660
670
661
- err = root . RemoveNodeLink ( path [0 ] )
671
+ ndprime , err := insertNodeAtPath ( ctx , ds , nd , path [1 :], toinsert , create )
662
672
if err != nil {
663
673
return nil , err
664
674
}
665
675
676
+ _ = root .RemoveNodeLink (path [0 ])
666
677
err = root .AddNodeLinkClean (path [0 ], ndprime )
667
678
if err != nil {
668
679
return nil , err
0 commit comments