virNetDevIPRouteParseXML: Refactor to use 'virXMLProp*' instead of XPath

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

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-10-05 14:33:57 +02:00
parent a3c7426839
commit fa6ba9b8c7
4 changed files with 19 additions and 51 deletions

View File

@ -6263,7 +6263,7 @@ virDomainNetIPInfoParseXML(const char *source,
for (i = 0; i < nrouteNodes; i++) { for (i = 0; i < nrouteNodes; i++) {
virNetDevIPRoute *route = NULL; virNetDevIPRoute *route = NULL;
if (!(route = virNetDevIPRouteParseXML(source, routeNodes[i], ctxt))) if (!(route = virNetDevIPRouteParseXML(source, routeNodes[i])))
goto error; goto error;
VIR_APPEND_ELEMENT(def->routes, def->nroutes, route); VIR_APPEND_ELEMENT(def->routes, def->nroutes, route);

View File

@ -1824,9 +1824,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
for (i = 0; i < nRoutes; i++) { for (i = 0; i < nRoutes; i++) {
virNetDevIPRoute *route = NULL; virNetDevIPRoute *route = NULL;
if (!(route = virNetDevIPRouteParseXML(def->name, if (!(route = virNetDevIPRouteParseXML(def->name, routeNodes[i])))
routeNodes[i],
ctxt)))
return NULL; return NULL;
def->routes[i] = route; def->routes[i] = route;
def->nroutes++; def->nroutes++;

View File

@ -211,58 +211,29 @@ virNetDevIPRouteCreate(const char *errorDetail,
virNetDevIPRoute * virNetDevIPRoute *
virNetDevIPRouteParseXML(const char *errorDetail, virNetDevIPRouteParseXML(const char *errorDetail,
xmlNodePtr node, xmlNodePtr node)
xmlXPathContextPtr ctxt)
{ {
/* g_autofree char *family = virXMLPropString(node, "family");
* virNetDevIPRoute object is already allocated as part g_autofree char *address = virXMLPropString(node, "address");
* of an array. On failure clear: it out, but don't free it. g_autofree char *netmask = virXMLPropString(node, "netmask");
*/ g_autofree char *gateway = virXMLPropString(node, "gateway");
unsigned int prefix = 0;
VIR_XPATH_NODE_AUTORESTORE(ctxt) unsigned int metric = 0;
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;
bool hasPrefix = false; bool hasPrefix = false;
bool hasMetric = false; bool hasMetric = false;
int rc;
ctxt->node = node; if ((rc = virXMLPropUInt(node, "prefix", 10, VIR_XML_PROP_NONE, &prefix)) < 0)
return NULL;
/* grab raw data from XML */ if (rc == 1)
family = virXPathString("string(./@family)", ctxt); hasPrefix = true;
address = virXPathString("string(./@address)", ctxt);
netmask = virXPathString("string(./@netmask)", ctxt); if ((rc = virXMLPropUInt(node, "metric", 10, VIR_XML_PROP_NONZERO, &metric)) < 0)
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);
return NULL; return NULL;
}
hasPrefix = (prefixRc == 0); if (rc == 1)
metricRc = virXPathULong("string(./@metric)", ctxt, &metric);
if (metricRc == -2) {
virReportError(VIR_ERR_XML_ERROR,
_("%s: Invalid metric specified "
"in route definition"),
errorDetail);
return NULL;
}
if (metricRc == 0) {
hasMetric = true; 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, return virNetDevIPRouteCreate(errorDetail, family, address, netmask,
gateway, prefix, hasPrefix, metric, gateway, prefix, hasPrefix, metric,

View File

@ -42,8 +42,7 @@ virNetDevIPRouteCreate(const char *networkName,
virNetDevIPRoute * virNetDevIPRoute *
virNetDevIPRouteParseXML(const char *networkName, virNetDevIPRouteParseXML(const char *networkName,
xmlNodePtr node, xmlNodePtr node);
xmlXPathContextPtr ctxt);
int int
virNetDevIPRouteFormat(virBuffer *buf, virNetDevIPRouteFormat(virBuffer *buf,
const virNetDevIPRoute *def); const virNetDevIPRoute *def);