Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dee23b5

Browse files
committedJul 21, 2015
allow patch to optionally create intermediate dirs
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent d37ecbb commit dee23b5

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed
 

‎core/commands/object.go

+28-17
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ This removes the link named foo from the hash in $FOO_BAR and returns the
453453
resulting object hash.
454454
`,
455455
},
456-
Options: []cmds.Option{},
456+
Options: []cmds.Option{
457+
cmds.BoolOption("create", "p", "create intermediate directories on add-link"),
458+
},
457459
Arguments: []cmds.Argument{
458460
cmds.StringArg("root", true, false, "the hash of the node to modify"),
459461
cmds.StringArg("command", true, false, "the operation to perform"),
@@ -610,7 +612,12 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
610612

611613
parts := strings.Split(path, "/")
612614

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)
614621
if err != nil {
615622
return "", err
616623
}
@@ -619,12 +626,14 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
619626

620627
func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname string, childk key.Key) (*dag.Node, error) {
621628
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
629+
defer cancel()
622630
childnd, err := ds.Get(ctx, childk)
623631
if err != nil {
624-
cancel()
625632
return nil, err
626633
}
627-
cancel()
634+
635+
// ensure no link with that name already exists
636+
_ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound
628637

629638
err = root.AddNodeLinkClean(childname, childnd)
630639
if err != nil {
@@ -638,31 +647,33 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
638647
return root, nil
639648
}
640649

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) {
642651
if len(path) == 1 {
643652
return addLink(ctx, ds, root, path[0], toinsert)
644653
}
645654

655+
var nd *dag.Node
646656
child, err := root.GetNodeLink(path[0])
647657
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+
}
659669
}
660670

661-
err = root.RemoveNodeLink(path[0])
671+
ndprime, err := insertNodeAtPath(ctx, ds, nd, path[1:], toinsert, create)
662672
if err != nil {
663673
return nil, err
664674
}
665675

676+
_ = root.RemoveNodeLink(path[0])
666677
err = root.AddNodeLinkClean(path[0], ndprime)
667678
if err != nil {
668679
return nil, err

‎test/sharness/t0051-object.sh

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ test_object_cmd() {
145145
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn > rmlink_exp &&
146146
test_cmp rmlink_exp rmlink_output
147147
'
148+
149+
test_expect_success "object patch --create works" '
150+
OUT=$(ipfs object patch --create $EMPTY add-link a/b/c $FILE)
151+
'
152+
153+
test_expect_success "result looks good" '
154+
ipfs cat $OUT/a/b/c > p2_hwfile &&
155+
test_cmp hwfile p2_hwfile
156+
'
157+
148158
}
149159

150160
# should work offline

0 commit comments

Comments
 (0)
Please sign in to comment.