diff --git a/merkledag/utils/utils.go b/merkledag/utils/utils.go index b073d4bf7..7985edcc0 100644 --- a/merkledag/utils/utils.go +++ b/merkledag/utils/utils.go @@ -151,3 +151,32 @@ func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []strin return root, nil } + +func (e *Editor) WriteOutputTo(ds dag.DAGService) error { + return copyDag(e.GetNode(), e.ds, ds) +} + +func copyDag(nd *dag.Node, from, to dag.DAGService) error { + _, err := to.Add(nd) + if err != nil { + return err + } + + for _, lnk := range nd.Links { + child, err := lnk.GetNode(context.Background(), from) + if err != nil { + if err == dag.ErrNotFound { + // not found means we didnt modify it, and it should + // already be in the target datastore + continue + } + return err + } + + err = copyDag(child, from, to) + if err != nil { + return err + } + } + return nil +}