]> xenbits.xensource.com Git - libvirt.git/commitdiff
maint: simplify lots of libxml2 clients
authorEric Blake <eblake@redhat.com>
Thu, 18 Aug 2011 21:37:14 +0000 (15:37 -0600)
committerEric Blake <eblake@redhat.com>
Fri, 19 Aug 2011 15:13:55 +0000 (09:13 -0600)
Repetitive patterns should be factored.  The sign of a good
factorization is a change that kills 5x more lines than it adds :)

* src/conf/domain_conf.c (virDomainDeviceDefParse)
(virDomainSnapshotDefParseString): Use new convenience macros.
* src/conf/storage_conf.c (virStoragePoolDefParseSourceString):
Likewise.
* src/cpu/cpu.c (cpuCompareXML, cpuBaselineXML): Likewise.
* src/esx/esx_vi.c (esxVI_Context_Execute): Likewise.
* src/qemu/qemu_migration.c (qemuMigrationCookieXMLParseStr):
Likewise.
* src/security/virt-aa-helper.c (caps_mockup): Likewise.
* src/test/test_driver.c (testOpenFromFile): Likewise.
* tests/cputest.c (cpuTestLoadXML, cpuTestLoadMultiXML):
Likewise.
* tools/virsh.c (cmdFreecell, makeCloneXML, cmdVNCDisplay)
(cmdTTYConsole, cmdDetachInterface, cmdDetachDisk)
(cmdSnapshotCreate, cmdSnapshotCreateAs, cmdSnapshotCurrent)
(cmdSnapshotList, cmdSnapshotParent): Likewise.

src/conf/domain_conf.c
src/conf/storage_conf.c
src/cpu/cpu.c
src/esx/esx_vi.c
src/qemu/qemu_migration.c
src/security/virt-aa-helper.c
src/test/test_driver.c
tests/cputest.c
tools/virsh.c

index ce1f3c552f908b1d2c4a6ab3b9f25e3f9d6b2b96..0a2c9ebafb8d4d96ef8d2218d9360fc5dd1875da 100644 (file)
@@ -5385,17 +5385,10 @@ virDomainDeviceDefPtr virDomainDeviceDefParse(virCapsPtr caps,
     xmlXPathContextPtr ctxt = NULL;
     virDomainDeviceDefPtr dev = NULL;
 
-    if (!(xml = virXMLParseString(xmlStr, "device.xml"))) {
+    if (!(xml = virXMLParseStringCtxt(xmlStr, "device.xml", &ctxt))) {
         goto error;
     }
-    node = xmlDocGetRootElement(xml);
-
-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
-        goto error;
-    }
-    ctxt->node = xmlDocGetRootElement(xml);
+    node = ctxt->node;
 
     if (VIR_ALLOC(dev) < 0) {
         virReportOOMError();
@@ -10968,23 +10961,16 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
     char *creation = NULL, *state = NULL;
     struct timeval tv;
 
-    xml = virXMLParse(NULL, xmlStr, "domainsnapshot.xml");
+    xml = virXMLParseCtxt(NULL, xmlStr, "domainsnapshot.xml", &ctxt);
     if (!xml) {
         return NULL;
     }
 
-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
     if (VIR_ALLOC(def) < 0) {
         virReportOOMError();
         goto cleanup;
     }
 
-    ctxt->node = xmlDocGetRootElement(xml);
     if (!xmlStrEqual(ctxt->node->name, BAD_CAST "domainsnapshot")) {
         virDomainReportError(VIR_ERR_XML_ERROR, "%s", _("domainsnapshot"));
         goto cleanup;
index 995f9a6a314209b2b069385f74ac5a309cb56575..8d14e872ca44ac40e6b1c9c411a149953dc44eff 100644 (file)
@@ -505,13 +505,7 @@ virStoragePoolDefParseSourceString(const char *srcSpec,
     xmlXPathContextPtr xpath_ctxt = NULL;
     virStoragePoolSourcePtr def = NULL, ret = NULL;
 
-    if (!(doc = virXMLParseString(srcSpec, "srcSpec.xml"))) {
-        goto cleanup;
-    }
-
-    xpath_ctxt = xmlXPathNewContext(doc);
-    if (xpath_ctxt == NULL) {
-        virReportOOMError();
+    if (!(doc = virXMLParseStringCtxt(srcSpec, "srcSpec.xml", &xpath_ctxt))) {
         goto cleanup;
     }
 
index 16e0709a53486daccc53b31433b327b3e6035236..b47f07820e1ee27b452c1a93b87ea287a6548235 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * cpu.c: internal functions for CPU manipulation
  *
- * Copyright (C) 2009--2010 Red Hat, Inc.
+ * Copyright (C) 2009-2011 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -76,16 +76,9 @@ cpuCompareXML(virCPUDefPtr host,
 
     VIR_DEBUG("host=%p, xml=%s", host, NULLSTR(xml));
 
-    if (!(doc = virXMLParseString(xml, "cpu.xml")))
+    if (!(doc = virXMLParseStringCtxt(xml, "cpu.xml", &ctxt)))
         goto cleanup;
 
-    if ((ctxt = xmlXPathNewContext(doc)) == NULL) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    ctxt->node = xmlDocGetRootElement(doc);
-
     cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO);
     if (cpu == NULL)
         goto cleanup;
@@ -311,14 +304,9 @@ cpuBaselineXML(const char **xmlCPUs,
         goto no_memory;
 
     for (i = 0; i < ncpus; i++) {
-        if (!(doc = virXMLParseString(xmlCPUs[i], "cpu.xml")))
+        if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], "cpu.xml", &ctxt)))
             goto error;
 
-        if ((ctxt = xmlXPathNewContext(doc)) == NULL)
-            goto no_memory;
-
-        ctxt->node = xmlDocGetRootElement(doc);
-
         cpus[i] = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST);
         if (cpus[i] == NULL)
             goto error;
index c82094b49b5dc4578426cfebf550cfa3507b3b3a..5c8d79e56c025932098dabfa2c55b6fffdbc38b3 100644 (file)
@@ -910,21 +910,14 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
     (*response)->content = virBufferContentAndReset(&buffer);
 
     if ((*response)->responseCode == 500 || (*response)->responseCode == 200) {
-        (*response)->document = virXMLParseString((*response)->content,
-                                                  "esx.xml");
+        (*response)->document = virXMLParseStringCtxt((*response)->content,
+                                                      "esx.xml",
+                                                      &xpathContext);
 
         if ((*response)->document == NULL) {
             goto cleanup;
         }
 
-        xpathContext = xmlXPathNewContext((*response)->document);
-
-        if (xpathContext == NULL) {
-            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
-                         _("Could not create XPath context"));
-            goto cleanup;
-        }
-
         xmlXPathRegisterNs(xpathContext, BAD_CAST "soapenv",
                            BAD_CAST "http://schemas.xmlsoap.org/soap/envelope/");
         xmlXPathRegisterNs(xpathContext, BAD_CAST "vim", BAD_CAST "urn:vim25");
index 55104930fb87401398a4b23a6bf539e2d823af86..a84faf68fd4f36152a9ae263cc2240ee929a57d7 100644 (file)
@@ -602,16 +602,9 @@ qemuMigrationCookieXMLParseStr(qemuMigrationCookiePtr mig,
 
     VIR_DEBUG("xml=%s", NULLSTR(xml));
 
-    if (!(doc = virXMLParseString(xml, "qemumigration.xml")))
+    if (!(doc = virXMLParseStringCtxt(xml, "qemumigration.xml", &ctxt)))
         goto cleanup;
 
-    if ((ctxt = xmlXPathNewContext(doc)) == NULL) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    ctxt->node = xmlDocGetRootElement(doc);
-
     ret = qemuMigrationCookieXMLParse(mig, ctxt, flags);
 
 cleanup:
index 1e2feae8b1b3d9f5ff47f1161f7833c5b5fecd47..bb577d30234a2105ef7b2459a868a3718d8c04a3 100644 (file)
@@ -640,24 +640,16 @@ caps_mockup(vahControl * ctl, const char *xmlStr)
     int rc = -1;
     xmlDocPtr xml = NULL;
     xmlXPathContextPtr ctxt = NULL;
-    xmlNodePtr root;
 
-    if (!(xml = virXMLParseString(xmlStr, "domain.xml"))) {
+    if (!(xml = virXMLParseStringCtxt(xmlStr, "domain.xml", &ctxt))) {
         goto cleanup;
     }
 
-    root = xmlDocGetRootElement(xml);
-    if (!xmlStrEqual(root->name, BAD_CAST "domain")) {
+    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "domain")) {
         vah_error(NULL, 0, _("incorrect root element"));
         goto cleanup;
     }
 
-    if ((ctxt = xmlXPathNewContext(xml)) == NULL) {
-        vah_error(ctl, 0, _("could not allocate memory"));
-        goto cleanup;
-    }
-    ctxt->node = root;
-
     /* Quick sanity check for some required elements */
     if (verify_xpath_context(ctxt) != 0)
         goto cleanup;
index ec2bd75f4dd81f21ef9003baed64b20527bb6b9f..b3e24b45143acd2f0f0ed6980ef7a56e5ee6f86c 100644 (file)
@@ -742,7 +742,6 @@ static int testOpenFromFile(virConnectPtr conn,
     long l;
     char *str;
     xmlDocPtr xml = NULL;
-    xmlNodePtr root = NULL;
     xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
                *pools = NULL, *devs = NULL;
     xmlXPathContextPtr ctxt = NULL;
@@ -771,24 +770,16 @@ static int testOpenFromFile(virConnectPtr conn,
     if (!(privconn->caps = testBuildCapabilities(conn)))
         goto error;
 
-    if (!(xml = virXMLParseFile(file))) {
+    if (!(xml = virXMLParseFileCtxt(file, &ctxt))) {
         goto error;
     }
 
-    root = xmlDocGetRootElement(xml);
-    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
+    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
         testError(VIR_ERR_XML_ERROR, "%s",
                   _("Root element is not 'node'"));
         goto error;
     }
 
-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        testError(VIR_ERR_INTERNAL_ERROR, "%s",
-                  _("creating xpath context"));
-        goto error;
-    }
-
     privconn->nextDomID = 1;
     privconn->numCells = 0;
     if ((privconn->path = strdup(file)) == NULL) {
index 4dcf1aa4123455249c7ee1cc1c8cdc2a13587963..b5272c16249eb4fcc3ec960f3c05e26dc3b02899 100644 (file)
@@ -96,11 +96,9 @@ cpuTestLoadXML(const char *arch, const char *name)
     if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml", abs_srcdir, arch, name) < 0)
         goto cleanup;
 
-    if (!(doc = virXMLParseFile(xml)) ||
-        !(ctxt = xmlXPathNewContext(doc)))
+    if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
         goto cleanup;
 
-    ctxt->node = xmlDocGetRootElement(doc);
     cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO);
 
 cleanup:
@@ -127,12 +125,9 @@ cpuTestLoadMultiXML(const char *arch,
     if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml", abs_srcdir, arch, name) < 0)
         goto cleanup;
 
-    if (!(doc = virXMLParseFile(xml)) ||
-        !(ctxt = xmlXPathNewContext(doc)))
+    if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
         goto error;
 
-    ctxt->node = xmlDocGetRootElement(doc);
-
     n = virXPathNodeSet("/cpuTest/cpu", ctxt, &nodes);
     if (n <= 0 || !(cpus = calloc(n, sizeof(virCPUDefPtr))))
         goto error;
index c43de4c3225b9a608d88eaa6f0678bd01d92f358..1c67150299611311df152b8e401e5d087d2e865f 100644 (file)
@@ -67,6 +67,8 @@ static char *progname;
 #define VSH_PROMPT_RW    "virsh # "
 #define VSH_PROMPT_RO    "virsh > "
 
+#define VIR_FROM_THIS VIR_FROM_NONE
+
 #define GETTIMEOFDAY(T) gettimeofday(T, NULL)
 #define DIFF_MSEC(T, U) \
         ((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 + \
@@ -2901,16 +2903,11 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
             goto cleanup;
         }
 
-        xml = xmlReadDoc((const xmlChar *) cap_xml, "node.xml", NULL,
-                          XML_PARSE_NOENT | XML_PARSE_NONET |
-                          XML_PARSE_NOWARNING);
-
+        xml = virXMLParseStringCtxt(cap_xml, "node.xml", &ctxt);
         if (!xml) {
             vshError(ctl, "%s", _("unable to get node capabilities"));
             goto cleanup;
         }
-
-        ctxt = xmlXPathNewContext(xml);
         nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell",
                                     ctxt, &nodes);
 
@@ -8404,13 +8401,9 @@ makeCloneXML(const char *origxml, const char *newname) {
     xmlChar *newxml = NULL;
     int size;
 
-    doc = xmlReadDoc((const xmlChar *) origxml, "domain.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOWARNING);
+    doc = virXMLParseStringCtxt(origxml, "domain.xml", &ctxt);
     if (!doc)
         goto cleanup;
-    ctxt = xmlXPathNewContext(doc);
-    if (!ctxt)
-        goto cleanup;
 
     obj = xmlXPathEval(BAD_CAST "/volume/name", ctxt);
     if ((obj == NULL) || (obj->nodesetval == NULL) ||
@@ -10199,15 +10192,10 @@ cmdVNCDisplay(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = xmlReadDoc((const xmlChar *) doc, "domain.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOWARNING);
+    xml = virXMLParseStringCtxt(doc, "domain.xml", &ctxt);
     VIR_FREE(doc);
     if (!xml)
         goto cleanup;
-    ctxt = xmlXPathNewContext(xml);
-    if (!ctxt)
-        goto cleanup;
 
     obj = xmlXPathEval(BAD_CAST "string(/domain/devices/graphics[@type='vnc']/@port)", ctxt);
     if ((obj == NULL) || (obj->type != XPATH_STRING) ||
@@ -10272,15 +10260,10 @@ cmdTTYConsole(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = xmlReadDoc((const xmlChar *) doc, "domain.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOWARNING);
+    xml = virXMLParseStringCtxt(doc, "domain.xml", &ctxt);
     VIR_FREE(doc);
     if (!xml)
         goto cleanup;
-    ctxt = xmlXPathNewContext(xml);
-    if (!ctxt)
-        goto cleanup;
 
     obj = xmlXPathEval(BAD_CAST "string(/domain/devices/console/@tty)", ctxt);
     if ((obj == NULL) || (obj->type != XPATH_STRING) ||
@@ -10664,19 +10647,12 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = xmlReadDoc((const xmlChar *) doc, "domain.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOWARNING);
+    xml = virXMLParseStringCtxt(doc, "domain.xml", &ctxt);
     VIR_FREE(doc);
     if (!xml) {
         vshError(ctl, "%s", _("Failed to get interface information"));
         goto cleanup;
     }
-    ctxt = xmlXPathNewContext(xml);
-    if (!ctxt) {
-        vshError(ctl, "%s", _("Failed to get interface information"));
-        goto cleanup;
-    }
 
     snprintf(buf, sizeof(buf), "/domain/devices/interface[@type='%s']", type);
     obj = xmlXPathEval(BAD_CAST buf, ctxt);
@@ -11138,19 +11114,12 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = xmlReadDoc((const xmlChar *) doc, "domain.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOWARNING);
+    xml = virXMLParseStringCtxt(doc, "domain.xml", &ctxt);
     VIR_FREE(doc);
     if (!xml) {
         vshError(ctl, "%s", _("Failed to get disk information"));
         goto cleanup;
     }
-    ctxt = xmlXPathNewContext(xml);
-    if (!ctxt) {
-        vshError(ctl, "%s", _("Failed to get disk information"));
-        goto cleanup;
-    }
 
     obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
     if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
@@ -11865,14 +11834,9 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = xmlReadDoc((const xmlChar *) doc, "domainsnapshot.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOWARNING);
+    xml = virXMLParseStringCtxt(doc, "domainsnapshot.xml", &ctxt);
     if (!xml)
         goto cleanup;
-    ctxt = xmlXPathNewContext(xml);
-    if (!ctxt)
-        goto cleanup;
 
     name = virXPathString("string(/domainsnapshot/name)", ctxt);
     if (!name) {
@@ -11974,14 +11938,9 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = xmlReadDoc((const xmlChar *) doc, "domainsnapshot.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOWARNING);
+    xml = virXMLParseStringCtxt(doc, "domainsnapshot.xml", &ctxt);
     if (!xml)
         goto cleanup;
-    ctxt = xmlXPathNewContext(xml);
-    if (!ctxt)
-        goto cleanup;
 
     parsed_name = virXPathString("string(/domainsnapshot/name)", ctxt);
     if (!parsed_name) {
@@ -12056,16 +12015,9 @@ cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd)
             xmlDocPtr xmldoc = NULL;
             xmlXPathContextPtr ctxt = NULL;
 
-            xmldoc = xmlReadDoc((const xmlChar *) xml, "domainsnapshot.xml",
-                                NULL, XML_PARSE_NOENT | XML_PARSE_NONET |
-                                XML_PARSE_NOWARNING);
+            xmldoc = virXMLParseStringCtxt(xml, "domainsnapshot.xml", &ctxt);
             if (!xmldoc)
                 goto cleanup;
-            ctxt = xmlXPathNewContext(xmldoc);
-            if (!ctxt) {
-                xmlFreeDoc(xmldoc);
-                goto cleanup;
-            }
 
             name = virXPathString("string(/domainsnapshot/name)", ctxt);
             xmlXPathFreeContext(ctxt);
@@ -12165,14 +12117,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
             if (!doc)
                 continue;
 
-            xml = xmlReadDoc((const xmlChar *) doc, "domainsnapshot.xml", NULL,
-                             XML_PARSE_NOENT | XML_PARSE_NONET |
-                             XML_PARSE_NOWARNING);
+            xml = virXMLParseStringCtxt(doc, "domainsnapshot.xml", &ctxt);
             if (!xml)
                 continue;
-            ctxt = xmlXPathNewContext(xml);
-            if (!ctxt)
-                continue;
 
             state = virXPathString("string(/domainsnapshot/state)", ctxt);
             if (state == NULL)
@@ -12312,14 +12259,9 @@ cmdSnapshotParent(vshControl *ctl, const vshCmd *cmd)
     if (!xml)
         goto cleanup;
 
-    xmldoc = xmlReadDoc((const xmlChar *) xml, "domainsnapshot.xml", NULL,
-                        XML_PARSE_NOENT | XML_PARSE_NONET |
-                        XML_PARSE_NOWARNING);
+    xmldoc = virXMLParseStringCtxt(xml, "domainsnapshot.xml", &ctxt);
     if (!xmldoc)
         goto cleanup;
-    ctxt = xmlXPathNewContext(xmldoc);
-    if (!ctxt)
-        goto cleanup;
 
     parent = virXPathString("string(/domainsnapshot/parent/name)", ctxt);
     if (!parent)