[Asterisk-code-review] xmldoc.c: Fix dump of xml document (asterisk[13])

Jenkins2 asteriskteam at digium.com
Mon Jul 23 06:42:31 CDT 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/9541 )

Change subject: xmldoc.c:  Fix dump of xml document
......................................................................

xmldoc.c:  Fix dump of xml document

The "xmldoc dump" cli command was simply concatenating xml documents
into the output file.  The resulting file had multiple "xml"
processing instructions and multiple root elements which is illegal.
Normally this isn't an issue because Asterisk has only 1 main xml
documentation file but codec_opus has its own file so if it's
downloaded and you do "xmldoc dump", the result is invalid.

* Added 2 new functions to xml.c:
    ast_xml_copy_node_list creates a copy of a list of children.
    ast_xml_add_child_list adds a list to an existing list.

* Modified handle_dump_docs to create a new output document and
  add to it the children from each input file.  It then dumps the
  new document to the output file.

Change-Id: I3f182d38c75776aee76413dadd2d489d54a85c07
---
M include/asterisk/xml.h
M main/xml.c
M main/xmldoc.c
3 files changed, 78 insertions(+), 5 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, but someone else must approve
  Jenkins2: Looks good to me, approved; Approved for Submit



diff --git a/include/asterisk/xml.h b/include/asterisk/xml.h
index d80fb26..7221558 100644
--- a/include/asterisk/xml.h
+++ b/include/asterisk/xml.h
@@ -83,6 +83,23 @@
 struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child);
 
 /*!
+ * \brief Add a list of child nodes, to a specified parent node.
+ * \param parent Where to add the child node.
+ * \param child The child list to add.
+ * \retval NULL on error.
+ * \retval non-NULL The added child list on success.
+ */
+struct ast_xml_node *ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child);
+
+/*!
+ * \brief Create a copy of a n ode list.
+ * \param list The list to copy.
+ * \retval NULL on error.
+ * \retval non-NULL The copied list.
+ */
+struct ast_xml_node *ast_xml_copy_node_list(struct ast_xml_node *list);
+
+/*!
  * \brief Close an already open document and free the used
  *        structure.
  * \retval doc The document reference.
diff --git a/main/xml.c b/main/xml.c
index 46fe198..2bcda80 100644
--- a/main/xml.c
+++ b/main/xml.c
@@ -144,6 +144,22 @@
 	return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
 }
 
+struct ast_xml_node *ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
+{
+	if (!parent || !child) {
+		return NULL;
+	}
+	return (struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
+}
+
+struct ast_xml_node *ast_xml_copy_node_list(struct ast_xml_node *list)
+{
+	if (!list) {
+		return NULL;
+	}
+	return (struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
+}
+
 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
 {
 	xmlDoc *doc;
diff --git a/main/xmldoc.c b/main/xmldoc.c
index 86ccec0..d242453 100644
--- a/main/xmldoc.c
+++ b/main/xmldoc.c
@@ -2785,6 +2785,8 @@
 static char *handle_dump_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct documentation_tree *doctree;
+	struct ast_xml_doc *dumpdoc;
+	struct ast_xml_node *dumproot;
 	FILE *f;
 
 	switch (cmd) {
@@ -2801,15 +2803,53 @@
 	if (a->argc != 3) {
 		return CLI_SHOWUSAGE;
 	}
+
+	dumpdoc = ast_xml_new();
+	if (!dumpdoc) {
+		ast_log(LOG_ERROR, "Could not create new XML document\n");
+		return CLI_FAILURE;
+	}
+
+	dumproot = ast_xml_new_node("docs");
+	if (!dumproot) {
+		ast_xml_close(dumpdoc);
+		ast_log(LOG_ERROR, "Could not create new XML root node\n");
+		return CLI_FAILURE;
+	}
+
+	ast_xml_set_root(dumpdoc, dumproot);
+
+	AST_RWLIST_RDLOCK(&xmldoc_tree);
+	AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
+		struct ast_xml_node *root_node = ast_xml_get_root(doctree->doc);
+		struct ast_xml_node *kids = ast_xml_node_get_children(root_node);
+		struct ast_xml_node *kids_copy;
+
+		/* If there are no kids someone screwed up, but we check anyway. */
+		if (!kids) {
+			continue;
+		}
+
+		kids_copy = ast_xml_copy_node_list(kids);
+		if (!kids_copy) {
+			ast_xml_close(dumpdoc);
+			ast_log(LOG_ERROR, "Could not create copy of XML node list\n");
+			return CLI_FAILURE;
+		}
+
+		ast_xml_add_child_list(dumproot, kids_copy);
+	}
+	AST_RWLIST_UNLOCK(&xmldoc_tree);
+
 	if (!(f = fopen(a->argv[2], "w"))) {
+		ast_xml_close(dumpdoc);
 		ast_log(LOG_ERROR, "Could not open file '%s': %s\n", a->argv[2], strerror(errno));
 		return CLI_FAILURE;
 	}
-	AST_RWLIST_RDLOCK(&xmldoc_tree);
-	AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
-		ast_xml_doc_dump_file(f, doctree->doc);
-	}
-	AST_RWLIST_UNLOCK(&xmldoc_tree);
+
+	ast_xml_doc_dump_file(f, dumpdoc);
+	ast_xml_close(dumpdoc);
+
 	fclose(f);
 	return CLI_SUCCESS;
 }

-- 
To view, visit https://gerrit.asterisk.org/9541
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: merged
Gerrit-Change-Id: I3f182d38c75776aee76413dadd2d489d54a85c07
Gerrit-Change-Number: 9541
Gerrit-PatchSet: 1
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180723/e81af408/attachment-0001.html>


More information about the asterisk-code-review mailing list