[libvirt] [PATCH] xml: Use virXMLParse* helpers everywhere

Cole Robinson crobinso at redhat.com
Thu May 12 21:47:43 UTC 2011


virt-aa-helper isn't even compile tested since I don't have the setup for
it.

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 src/conf/domain_conf.c        |   33 ++++--------------
 src/conf/nwfilter_conf.c      |   76 ++++-------------------------------------
 src/conf/storage_conf.c       |    8 +----
 src/esx/esx_vi.c              |   14 +------
 src/security/virt-aa-helper.c |   43 +----------------------
 src/test/test_driver.c        |   17 +--------
 6 files changed, 21 insertions(+), 170 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index f7e4959..a0eb43e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -4877,26 +4877,17 @@ virDomainDeviceDefPtr virDomainDeviceDefParse(virCapsPtr caps,
     xmlXPathContextPtr ctxt = NULL;
     virDomainDeviceDefPtr dev = NULL;
 
-    if (!(xml = xmlReadDoc(BAD_CAST xmlStr, "device.xml", NULL,
-                           XML_PARSE_NOENT | XML_PARSE_NONET |
-                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
-        virDomainReportError(VIR_ERR_XML_ERROR, NULL);
+    if (!(xml = virXMLParseString(xmlStr, "device.xml"))) {
         goto error;
     }
-
     node = xmlDocGetRootElement(xml);
-    if (node == NULL) {
-        virDomainReportError(VIR_ERR_XML_ERROR,
-                             "%s", _("missing root element"));
-        goto error;
-    }
 
     ctxt = xmlXPathNewContext(xml);
     if (ctxt == NULL) {
         virReportOOMError();
         goto error;
     }
-    ctxt->node = node;
+    ctxt->node = xmlDocGetRootElement(xml);
 
     if (VIR_ALLOC(dev) < 0) {
         virReportOOMError();
@@ -9048,7 +9039,6 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
 {
     xmlXPathContextPtr ctxt = NULL;
     xmlDocPtr xml = NULL;
-    xmlNodePtr root;
     virDomainSnapshotDefPtr def = NULL;
     virDomainSnapshotDefPtr ret = NULL;
     char *creation = NULL, *state = NULL;
@@ -9056,22 +9046,9 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
 
     xml = virXMLParse(NULL, xmlStr, "domainsnapshot.xml");
     if (!xml) {
-        virDomainReportError(VIR_ERR_XML_ERROR,
-                             "%s",_("failed to parse snapshot xml document"));
         return NULL;
     }
 
-    if ((root = xmlDocGetRootElement(xml)) == NULL) {
-        virDomainReportError(VIR_ERR_INTERNAL_ERROR,
-                              "%s", _("missing root element"));
-        goto cleanup;
-    }
-
-    if (!xmlStrEqual(root->name, BAD_CAST "domainsnapshot")) {
-        virDomainReportError(VIR_ERR_XML_ERROR, "%s", _("domainsnapshot"));
-        goto cleanup;
-    }
-
     ctxt = xmlXPathNewContext(xml);
     if (ctxt == NULL) {
         virReportOOMError();
@@ -9083,7 +9060,11 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
         goto cleanup;
     }
 
-    ctxt->node = root;
+    ctxt->node = xmlDocGetRootElement(xml);
+    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "domainsnapshot")) {
+        virDomainReportError(VIR_ERR_XML_ERROR, "%s", _("domainsnapshot"));
+        goto cleanup;
+    }
 
     gettimeofday(&tv, NULL);
 
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index eb75bad..a32bdb3 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2066,28 +2066,6 @@ virNWFilterDefParseXML(xmlXPathContextPtr ctxt) {
 }
 
 
-/* Called from SAX on parsing errors in the XML. */
-static void
-catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
-{
-    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
-
-    if (ctxt) {
-        virConnectPtr conn = ctxt->_private;
-
-        if (conn &&
-            conn->err.code == VIR_ERR_NONE &&
-            ctxt->lastError.level == XML_ERR_FATAL &&
-            ctxt->lastError.message != NULL) {
-            virNWFilterReportError(VIR_ERR_XML_DETAIL,
-                                   _("at line %d: %s"),
-                                   ctxt->lastError.line,
-                                   ctxt->lastError.message);
-        }
-    }
-}
-
-
 virNWFilterDefPtr
 virNWFilterDefParseNode(xmlDocPtr xml,
                         xmlNodePtr root) {
@@ -2117,58 +2095,18 @@ cleanup:
 
 
 static virNWFilterDefPtr
-virNWFilterDefParse(virConnectPtr conn,
+virNWFilterDefParse(virConnectPtr conn ATTRIBUTE_UNUSED,
                     const char *xmlStr,
                     const char *filename) {
-    virNWFilterDefPtr ret = NULL;
-    xmlParserCtxtPtr pctxt;
-    xmlDocPtr xml = NULL;
-    xmlNodePtr node = NULL;
-
-    /* Set up a parser context so we can catch the details of XML errors. */
-    pctxt = xmlNewParserCtxt ();
-    if (!pctxt || !pctxt->sax)
-        goto cleanup;
-    pctxt->sax->error = catchXMLError;
-    pctxt->_private = conn;
-
-    if (conn) virResetError (&conn->err);
-    if (filename) {
-        xml = xmlCtxtReadFile (pctxt, filename, NULL,
-                               XML_PARSE_NOENT | XML_PARSE_NONET |
-                               XML_PARSE_NOWARNING);
-    } else {
-        xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr,
-                              "nwfilter.xml", NULL,
-                              XML_PARSE_NOENT | XML_PARSE_NONET |
-                              XML_PARSE_NOWARNING);
-    }
-
-    if (!xml) {
-        if (conn && conn->err.code == VIR_ERR_NONE)
-              virNWFilterReportError(VIR_ERR_XML_ERROR,
-                                     "%s",_("failed to parse xml document"));
-        goto cleanup;
-    }
+    virNWFilterDefPtr def = NULL;
+    xmlDocPtr xml;
 
-    node = xmlDocGetRootElement(xml);
-    if (node == NULL) {
-        virNWFilterReportError(VIR_ERR_XML_ERROR,
-                               "%s", _("missing root element"));
-        goto cleanup;
+    if ((xml = virXMLParse(filename, xmlStr, "nwfilter.xml"))) {
+        def = virNWFilterDefParseNode(xml, xmlDocGetRootElement(xml));
+        xmlFreeDoc(xml);
     }
 
-    ret = virNWFilterDefParseNode(xml, node);
-
-    xmlFreeParserCtxt (pctxt);
-    xmlFreeDoc(xml);
-
-    return ret;
-
- cleanup:
-    xmlFreeParserCtxt (pctxt);
-    xmlFreeDoc(xml);
-    return NULL;
+    return def;
 }
 
 
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index ed7d300..ca86f19 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -505,13 +505,7 @@ virStoragePoolDefParseSourceString(const char *srcSpec,
     xmlXPathContextPtr xpath_ctxt = NULL;
     virStoragePoolSourcePtr def = NULL, ret = NULL;
 
-    doc = xmlReadDoc((const xmlChar *)srcSpec, "srcSpec.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
-
-    if (doc == NULL) {
-        virStorageReportError(VIR_ERR_XML_ERROR,
-                              "%s", _("bad <source> spec"));
+    if (!(doc = virXMLParseString(srcSpec, "srcSpec.xml"))) {
         goto cleanup;
     }
 
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index ca5376e..381b547 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -894,20 +894,10 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
     (*response)->content = virBufferContentAndReset(&buffer);
 
     if ((*response)->responseCode == 500 || (*response)->responseCode == 200) {
-        (*response)->document = xmlReadDoc(BAD_CAST (*response)->content, "",
-                                           NULL, XML_PARSE_NONET);
+        (*response)->document = virXMLParseString((*response)->content,
+                                                  "esx.xml");
 
         if ((*response)->document == NULL) {
-            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
-                         _("Response for call to '%s' could not be parsed"),
-                         methodName);
-            goto cleanup;
-        }
-
-        if (xmlDocGetRootElement((*response)->document) == NULL) {
-            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
-                         _("Response for call to '%s' is an empty XML document"),
-                         methodName);
             goto cleanup;
         }
 
diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c
index e481095..a14fb77 100644
--- a/src/security/virt-aa-helper.c
+++ b/src/security/virt-aa-helper.c
@@ -592,29 +592,6 @@ valid_path(const char *path, const bool readonly)
     return 0;
 }
 
-/* Called from SAX on parsing errors in the XML. */
-static void
-catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
-{
-    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
-
-    if (ctxt) {
-        if (virGetLastError() == NULL &&
-            ctxt->lastError.level == XML_ERR_FATAL &&
-            ctxt->lastError.message != NULL) {
-                char *err_str = NULL;
-                if (virAsprintf(&err_str, "XML error at line %d: %s",
-                                ctxt->lastError.line,
-                                ctxt->lastError.message) == -1)
-                    vah_error(NULL, 0, _("could not get XML error"));
-                else {
-                    vah_error(NULL, 0, err_str);
-                    VIR_FREE(err_str);
-                }
-        }
-    }
-}
-
 static int
 verify_xpath_context(xmlXPathContextPtr ctxt)
 {
@@ -658,31 +635,15 @@ static int
 caps_mockup(vahControl * ctl, const char *xmlStr)
 {
     int rc = -1;
-    xmlParserCtxtPtr pctxt = NULL;
     xmlDocPtr xml = NULL;
     xmlXPathContextPtr ctxt = NULL;
     xmlNodePtr root;
 
-    /* Set up a parser context so we can catch the details of XML errors. */
-    pctxt = xmlNewParserCtxt ();
-    if (!pctxt || !pctxt->sax)
-        goto cleanup;
-    pctxt->sax->error = catchXMLError;
-
-    xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "domain.xml", NULL,
-                          XML_PARSE_NOENT | XML_PARSE_NONET |
-                          XML_PARSE_NOWARNING);
-    if (!xml) {
-        if (virGetLastError() == NULL)
-            vah_error(NULL, 0, _("failed to parse xml document"));
-        goto cleanup;
-    }
-
-    if ((root = xmlDocGetRootElement(xml)) == NULL) {
-        vah_error(NULL, 0, _("missing root element"));
+    if (!(xml = virXMLParseString(xmlStr, "domain.xml"))) {
         goto cleanup;
     }
 
+    root = xmlDocGetRootElement(xml);
     if (!xmlStrEqual(root->name, BAD_CAST "domain")) {
         vah_error(NULL, 0, _("incorrect root element"));
         goto cleanup;
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 58ff250..119b027 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -746,7 +746,7 @@ error:
 
 static int testOpenFromFile(virConnectPtr conn,
                             const char *file) {
-    int fd = -1, i, ret;
+    int i, ret;
     long l;
     char *str;
     xmlDocPtr xml = NULL;
@@ -779,22 +779,10 @@ static int testOpenFromFile(virConnectPtr conn,
     if (!(privconn->caps = testBuildCapabilities(conn)))
         goto error;
 
-    if ((fd = open(file, O_RDONLY)) < 0) {
-        virReportSystemError(errno,
-                             _("loading host definition file '%s'"),
-                             file);
+    if (!(xml = virXMLParseFile(file))) {
         goto error;
     }
 
-    if (!(xml = xmlReadFd(fd, file, NULL,
-                          XML_PARSE_NOENT | XML_PARSE_NONET |
-                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
-        testError(VIR_ERR_INTERNAL_ERROR,
-                  _("Invalid XML in file '%s'"), file);
-        goto error;
-    }
-    VIR_FORCE_CLOSE(fd);
-
     root = xmlDocGetRootElement(xml);
     if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
         testError(VIR_ERR_XML_ERROR, "%s",
@@ -1100,7 +1088,6 @@ static int testOpenFromFile(virConnectPtr conn,
     VIR_FREE(networks);
     VIR_FREE(ifaces);
     VIR_FREE(pools);
-    VIR_FORCE_CLOSE(fd);
     virDomainObjListDeinit(&privconn->domains);
     virNetworkObjListFree(&privconn->networks);
     virInterfaceObjListFree(&privconn->ifaces);
-- 
1.7.4.4




More information about the libvir-list mailing list