[PATCH v2 08/19] Refactoring virDomainLeaseDefParseXML() to use XPath

Kristina Hanicova khanicov at redhat.com
Tue May 4 11:40:02 UTC 2021


Signed-off-by: Kristina Hanicova <khanicov at redhat.com>
---
 src/conf/domain_conf.c | 189 ++++++++++++++++++-----------------------
 1 file changed, 82 insertions(+), 107 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 1142b1214a..365879ea98 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8085,48 +8085,34 @@ virSecurityDeviceLabelDefParseXML(virSecurityDeviceLabelDef ***seclabels_rtn,
  */
 static virDomainLeaseDef *
 virDomainLeaseDefParseXML(xmlNodePtr node,
-                          xmlXPathContextPtr ctxt G_GNUC_UNUSED)
+                          xmlXPathContextPtr ctxt)
 {
     virDomainLeaseDef *def;
-    xmlNodePtr cur;
     g_autofree char *lockspace = NULL;
     g_autofree char *key = NULL;
     g_autofree char *path = NULL;
     g_autofree char *offset = NULL;
+    VIR_XPATH_NODE_AUTORESTORE(ctxt)
 
+    ctxt->node = node;
     def = g_new0(virDomainLeaseDef, 1);
 
-    cur = node->children;
-    while (cur != NULL) {
-        if (cur->type == XML_ELEMENT_NODE) {
-            if (!key && virXMLNodeNameEqual(cur, "key")) {
-                if (!(key = virXMLNodeContentString(cur)))
-                    goto error;
-            } else if (!lockspace &&
-                       virXMLNodeNameEqual(cur, "lockspace")) {
-                if (!(lockspace = virXMLNodeContentString(cur)))
-                    goto error;
-            } else if (!path &&
-                       virXMLNodeNameEqual(cur, "target")) {
-                path = virXMLPropString(cur, "path");
-                offset = virXMLPropString(cur, "offset");
-            }
-        }
-        cur = cur->next;
-    }
-
-    if (!key) {
+    if (!(key = virXPathString("string(./key)", ctxt))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing 'key' element for lease"));
         goto error;
     }
-    if (!path) {
+
+    if (!(lockspace = virXPathString("string(./lockspace)", ctxt)))
+        goto error;
+
+    if (!(path = virXPathString("string(./target/@path)", ctxt))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing 'target' element for lease"));
         goto error;
     }
 
-    if (offset &&
+    if ((offset = virXPathString("string(./target/@offset)", ctxt)) &&
         virStrToLong_ull(offset, NULL, 10, &def->offset) < 0) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Malformed lease target offset %s"), offset);
@@ -9464,9 +9450,8 @@ virDomainControllerDefParseXML(virDomainXMLOption *xmlopt,
 {
     g_autoptr(virDomainControllerDef) def = NULL;
     virDomainControllerType type = 0;
-    xmlNodePtr cur = NULL;
-    bool processedModel = false;
-    bool processedTarget = false;
+    xmlNodePtr driver = NULL;
+    xmlNodePtr target = NULL;
     int numaNode = -1;
     int ports = -1;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
@@ -9502,94 +9487,84 @@ virDomainControllerDefParseXML(virDomainXMLOption *xmlopt,
         def->idx = idxVal;
     }
 
-    cur = node->children;
-    while (cur != NULL) {
-        if (cur->type == XML_ELEMENT_NODE) {
-            if (virXMLNodeNameEqual(cur, "driver")) {
-                if (virXMLPropUInt(cur, "queues", 10, VIR_XML_PROP_NONE,
-                                   &def->queues) < 0)
-                    return NULL;
+    if ((driver = virXPathNode("./driver", ctxt)) &&
+        (virDomainVirtioOptionsParseXML(driver, &def->virtio) < 0))
+        return NULL;
 
-                if (virXMLPropUInt(cur, "cmd_per_lun", 10, VIR_XML_PROP_NONE,
-                                   &def->cmd_per_lun) < 0)
-                    return NULL;
+    if (virXMLPropUInt(driver, "queues", 10, VIR_XML_PROP_NONE,
+                       &def->queues) < 0)
+        return NULL;
 
-                if (virXMLPropUInt(cur, "max_sectors", 10, VIR_XML_PROP_NONE,
-                                   &def->max_sectors) < 0)
-                    return NULL;
+    if (virXMLPropUInt(driver, "cmd_per_lun", 10, VIR_XML_PROP_NONE,
+                       &def->cmd_per_lun) < 0)
+        return NULL;
 
-                if (virXMLPropTristateSwitch(cur, "ioeventfd",
-                                             VIR_XML_PROP_NONE,
-                                             &def->ioeventfd) < 0)
-                    return NULL;
+    if (virXMLPropUInt(driver, "max_sectors", 10, VIR_XML_PROP_NONE,
+                       &def->max_sectors) < 0)
+        return NULL;
 
-                if (virXMLPropUInt(cur, "iothread", 10, VIR_XML_PROP_NONE,
-                                   &def->iothread) < 0)
-                    return NULL;
+    if (virXMLPropTristateSwitch(driver, "ioeventfd",
+                                 VIR_XML_PROP_NONE,
+                                 &def->ioeventfd) < 0)
+        return NULL;
 
-                if (virDomainVirtioOptionsParseXML(cur, &def->virtio) < 0)
-                    return NULL;
-            } else if (virXMLNodeNameEqual(cur, "model")) {
-                if (processedModel) {
-                    virReportError(VIR_ERR_XML_ERROR, "%s",
-                                   _("Multiple <model> elements in "
-                                     "controller definition not allowed"));
-                    return NULL;
-                }
+    if (virXMLPropUInt(driver, "iothread", 10, VIR_XML_PROP_NONE,
+                       &def->iothread) < 0)
+        return NULL;
 
-                if (def->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
-                    if (virXMLPropEnum(cur, "name",
-                                       virDomainControllerPCIModelNameTypeFromString,
-                                       VIR_XML_PROP_NONE,
-                                       &def->opts.pciopts.modelName) < 0)
-                        return NULL;
-                }
+    if (virXPathBoolean("boolean(count(./model) > 1)", ctxt)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("Multiple <model> elements in "
+                         "controller definition not allowed"));
+        return NULL;
+    }
 
-                processedModel = true;
-            } else if (virXMLNodeNameEqual(cur, "target")) {
-                if (processedTarget) {
-                    virReportError(VIR_ERR_XML_ERROR, "%s",
-                                   _("Multiple <target> elements in "
-                                     "controller definition not allowed"));
-                    return NULL;
-                }
-                if (def->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
-                    if (virXMLPropInt(cur, "chassisNr", 0, VIR_XML_PROP_NONE,
-                                      &def->opts.pciopts.chassisNr) < 0)
-                        return NULL;
-
-                    if (virXMLPropInt(cur, "chassis", 0, VIR_XML_PROP_NONE,
-                                      &def->opts.pciopts.chassis) < 0)
-                        return NULL;
-
-                    if (virXMLPropInt(cur, "port", 0, VIR_XML_PROP_NONE,
-                                      &def->opts.pciopts.port) < 0)
-                        return NULL;
-
-                    if (virXMLPropInt(cur, "busNr", 0, VIR_XML_PROP_NONE,
-                                      &def->opts.pciopts.busNr) < 0)
-                        return NULL;
-
-                    if (virXMLPropTristateSwitch(cur, "hotplug",
-                                                 VIR_XML_PROP_NONE,
-                                                 &def->opts.pciopts.hotplug) < 0)
-                        return NULL;
-
-                    if ((rc = virXMLPropInt(cur, "index", 0, VIR_XML_PROP_NONE,
-                                      &def->opts.pciopts.targetIndex)) < 0)
-                        return NULL;
-
-                    if ((rc == 1) && def->opts.pciopts.targetIndex == -1) {
-                        virReportError(VIR_ERR_XML_ERROR,
-                                       _("Invalid target index '%i' in PCI controller"),
-                                       def->opts.pciopts.targetIndex);
-                    }
-                }
+    if (def->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
+        if (virXMLPropEnum(virXPathNode("./model", ctxt), "name",
+                           virDomainControllerPCIModelNameTypeFromString,
+                           VIR_XML_PROP_NONE,
+                           &def->opts.pciopts.modelName) < 0)
+            return NULL;
+    }
 
-                processedTarget = true;
-            }
-        }
-        cur = cur->next;
+    if (virXPathBoolean("boolean(count(./target) > 1)", ctxt)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("Multiple <target> elements in "
+                         "controller definition not allowed"));
+        return NULL;
+    }
+
+    if ((target = virXPathNode("./target", ctxt)) &&
+        def->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
+        if (virXMLPropInt(target, "chassisNr", 0, VIR_XML_PROP_NONE,
+                      &def->opts.pciopts.chassisNr) < 0)
+            return NULL;
+
+        if (virXMLPropInt(target, "chassis", 0, VIR_XML_PROP_NONE,
+                          &def->opts.pciopts.chassis) < 0)
+            return NULL;
+
+        if (virXMLPropInt(target, "port", 0, VIR_XML_PROP_NONE,
+                          &def->opts.pciopts.port) < 0)
+            return NULL;
+
+        if (virXMLPropInt(target, "busNr", 0, VIR_XML_PROP_NONE,
+                          &def->opts.pciopts.busNr) < 0)
+            return NULL;
+
+        if (virXMLPropTristateSwitch(target, "hotplug",
+                                     VIR_XML_PROP_NONE,
+                                     &def->opts.pciopts.hotplug) < 0)
+            return NULL;
+
+        if ((rc = virXMLPropInt(target, "index", 0, VIR_XML_PROP_NONE,
+                          &def->opts.pciopts.targetIndex)) < 0)
+            return NULL;
+
+        if ((rc == 1) && def->opts.pciopts.targetIndex == -1)
+            virReportError(VIR_ERR_XML_ERROR,
+                           _("Invalid target index '%i' in PCI controller"),
+                           def->opts.pciopts.targetIndex);
     }
 
     /* node is parsed differently from target attributes because
-- 
2.30.2




More information about the libvir-list mailing list