[PATCH 26/43] virNetDevIPRouteParseXML: Refactor to use 'virXMLProp*' instead of XPath

Peter Krempa pkrempa at redhat.com
Mon Oct 24 14:14:31 UTC 2022


The function extracts multiple attributes form a single element. Modify
the function to stop using multiple XPath lookups.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/conf/domain_conf.c        |  2 +-
 src/conf/network_conf.c       |  4 +--
 src/conf/networkcommon_conf.c | 61 +++++++++--------------------------
 src/conf/networkcommon_conf.h |  3 +-
 4 files changed, 19 insertions(+), 51 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 8be37a4040..cdf05e8fae 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6263,7 +6263,7 @@ virDomainNetIPInfoParseXML(const char *source,
     for (i = 0; i < nrouteNodes; i++) {
         virNetDevIPRoute *route = NULL;

-        if (!(route = virNetDevIPRouteParseXML(source, routeNodes[i], ctxt)))
+        if (!(route = virNetDevIPRouteParseXML(source, routeNodes[i])))
             goto error;

         VIR_APPEND_ELEMENT(def->routes, def->nroutes, route);
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index be43894050..44ac5408d1 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1824,9 +1824,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
         for (i = 0; i < nRoutes; i++) {
             virNetDevIPRoute *route = NULL;

-            if (!(route = virNetDevIPRouteParseXML(def->name,
-                                                   routeNodes[i],
-                                                   ctxt)))
+            if (!(route = virNetDevIPRouteParseXML(def->name, routeNodes[i])))
                 return NULL;
             def->routes[i] = route;
             def->nroutes++;
diff --git a/src/conf/networkcommon_conf.c b/src/conf/networkcommon_conf.c
index 7bfcd2e9b4..275847853d 100644
--- a/src/conf/networkcommon_conf.c
+++ b/src/conf/networkcommon_conf.c
@@ -211,58 +211,29 @@ virNetDevIPRouteCreate(const char *errorDetail,

 virNetDevIPRoute *
 virNetDevIPRouteParseXML(const char *errorDetail,
-                         xmlNodePtr node,
-                         xmlXPathContextPtr ctxt)
+                         xmlNodePtr node)
 {
-    /*
-     * virNetDevIPRoute object is already allocated as part
-     * of an array.  On failure clear: it out, but don't free it.
-     */
-
-    VIR_XPATH_NODE_AUTORESTORE(ctxt)
-    g_autofree char *family = NULL;
-    g_autofree char *address = NULL;
-    g_autofree char *netmask = NULL;
-    g_autofree char *gateway = NULL;
-    unsigned long prefix = 0, metric = 0;
-    int prefixRc, metricRc;
+    g_autofree char *family = virXMLPropString(node, "family");
+    g_autofree char *address = virXMLPropString(node, "address");
+    g_autofree char *netmask = virXMLPropString(node, "netmask");
+    g_autofree char *gateway = virXMLPropString(node, "gateway");
+    unsigned int prefix = 0;
+    unsigned int metric = 0;
     bool hasPrefix = false;
     bool hasMetric = false;
+    int rc;

-    ctxt->node = node;
-
-    /* grab raw data from XML */
-    family = virXPathString("string(./@family)", ctxt);
-    address = virXPathString("string(./@address)", ctxt);
-    netmask = virXPathString("string(./@netmask)", ctxt);
-    gateway = virXPathString("string(./@gateway)", ctxt);
-    prefixRc = virXPathULong("string(./@prefix)", ctxt, &prefix);
-    if (prefixRc == -2) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("%s: Invalid prefix specified "
-                         "in route definition"),
-                       errorDetail);
+    if ((rc = virXMLPropUInt(node, "prefix", 10, VIR_XML_PROP_NONE, &prefix)) < 0)
         return NULL;
-    }
-    hasPrefix = (prefixRc == 0);
-    metricRc = virXPathULong("string(./@metric)", ctxt, &metric);
-    if (metricRc == -2) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("%s: Invalid metric specified "
-                         "in route definition"),
-                       errorDetail);
+
+    if (rc == 1)
+        hasPrefix = true;
+
+    if ((rc = virXMLPropUInt(node, "metric", 10, VIR_XML_PROP_NONZERO, &metric)) < 0)
         return NULL;
-    }
-    if (metricRc == 0) {
+
+    if (rc == 1)
         hasMetric = true;
-        if (metric == 0) {
-            virReportError(VIR_ERR_XML_ERROR,
-                           _("%s: Invalid metric value, must be > 0 "
-                             "in route definition"),
-                           errorDetail);
-            return NULL;
-        }
-    }

     return virNetDevIPRouteCreate(errorDetail, family, address, netmask,
                                   gateway, prefix, hasPrefix, metric,
diff --git a/src/conf/networkcommon_conf.h b/src/conf/networkcommon_conf.h
index b5955d9f99..9ac981d0d7 100644
--- a/src/conf/networkcommon_conf.h
+++ b/src/conf/networkcommon_conf.h
@@ -42,8 +42,7 @@ virNetDevIPRouteCreate(const char *networkName,

 virNetDevIPRoute *
 virNetDevIPRouteParseXML(const char *networkName,
-                         xmlNodePtr node,
-                         xmlXPathContextPtr ctxt);
+                         xmlNodePtr node);
 int
 virNetDevIPRouteFormat(virBuffer *buf,
                        const virNetDevIPRoute *def);
-- 
2.37.3



More information about the libvir-list mailing list