util: add virXMLPropUIntDefault() function

This function allows you to specify a default value to return if the
property is not found rather than always setting *result to 0.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Jonathon Jongsma 2022-11-08 14:12:32 -06:00
parent 8a3cd801f2
commit c650e460bd
5 changed files with 42 additions and 18 deletions

View File

@ -561,7 +561,6 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
}
if ((topology = virXPathNode("./topology[1]", ctxt))) {
int rc;
if (virXMLPropUInt(topology, "sockets", 10,
VIR_XML_PROP_REQUIRED | VIR_XML_PROP_NONZERO,
@ -569,12 +568,10 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
return -1;
}
if ((rc = virXMLPropUInt(topology, "dies", 10,
VIR_XML_PROP_NONZERO,
&def->dies)) < 0) {
if (virXMLPropUIntDefault(topology, "dies", 10,
VIR_XML_PROP_NONZERO,
&def->dies, 1) < 0) {
return -1;
} else if (rc == 0) {
def->dies = 1;
}
if (virXMLPropUInt(topology, "cores", 10,

View File

@ -17085,7 +17085,6 @@ virDomainVcpuParse(virDomainDef *def,
unsigned int vcpus;
g_autofree char *tmp = NULL;
g_autofree xmlNodePtr *nodes = NULL;
int rc;
vcpus = maxvcpus = 1;
@ -17100,11 +17099,8 @@ virDomainVcpuParse(virDomainDef *def,
}
VIR_FREE(tmp);
if ((rc = virXMLPropUInt(vcpuNode, "current", 10, VIR_XML_PROP_NONE, &vcpus)) < 0) {
if (virXMLPropUIntDefault(vcpuNode, "current", 10, VIR_XML_PROP_NONE, &vcpus, maxvcpus) < 0)
return -1;
} else if (rc == 0) {
vcpus = maxvcpus;
}
if (virXMLPropEnumDefault(vcpuNode, "placement",
virDomainCpuPlacementModeTypeFromString,

View File

@ -916,16 +916,11 @@ virDomainNumaDefParseXML(virDomainNuma *def,
for (i = 0; i < n; i++) {
VIR_XPATH_NODE_AUTORESTORE(ctxt)
g_autofree char *tmp = NULL;
int rc;
unsigned int cur_cell;
if ((rc = virXMLPropUInt(cell[i], "id", 10, VIR_XML_PROP_NONE,
&cur_cell)) < 0)
if (virXMLPropUIntDefault(cell[i], "id", 10, VIR_XML_PROP_NONE, &cur_cell, i) < 0)
return -1;
if (rc == 0)
cur_cell = i;
/* cells are in order of parsing or explicitly numbered */
if (cur_cell >= n) {
virReportError(VIR_ERR_XML_ERROR, "%s",

View File

@ -560,12 +560,39 @@ virXMLPropUInt(xmlNodePtr node,
int base,
virXMLPropFlags flags,
unsigned int *result)
{
return virXMLPropUIntDefault(node, name, base, flags, result, 0);
}
/**
* virXMLPropUIntDefault:
* @node: XML dom node pointer
* @name: Name of the property (attribute) to get
* @base: Number base, see strtol
* @flags: Bitwise-OR of virXMLPropFlags
* @result: The returned value
* @defaultResult: Default value of @result in case the property is not found
*
* Convenience function to return value of an unsigned integer attribute.
*
* Returns 1 in case of success in which case @result is set,
* or 0 if the attribute is not present,
* or -1 and reports an error on failure.
*/
int
virXMLPropUIntDefault(xmlNodePtr node,
const char *name,
int base,
virXMLPropFlags flags,
unsigned int *result,
unsigned int defaultResult)
{
g_autofree char *tmp = NULL;
int ret;
unsigned int val;
*result = 0;
*result = defaultResult;
if (!(tmp = virXMLPropString(node, name))) {
if (!(flags & VIR_XML_PROP_REQUIRED))

View File

@ -132,6 +132,15 @@ virXMLPropUInt(xmlNodePtr node,
unsigned int *result)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5);
int
virXMLPropUIntDefault(xmlNodePtr node,
const char *name,
int base,
virXMLPropFlags flags,
unsigned int *result,
unsigned int defaultResult)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5);
int
virXMLPropLongLong(xmlNodePtr node,
const char *name,