]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add virXMLPropUIntDefault() function
authorJonathon Jongsma <jjongsma@redhat.com>
Tue, 8 Nov 2022 20:12:32 +0000 (14:12 -0600)
committerJonathon Jongsma <jjongsma@redhat.com>
Wed, 8 Mar 2023 19:05:05 +0000 (13:05 -0600)
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>
src/conf/cpu_conf.c
src/conf/domain_conf.c
src/conf/numa_conf.c
src/util/virxml.c
src/util/virxml.h

index 9f5e8cb8485da583b45d27ead410906f77d4abdb..ba7428bc930b32058f3b01bae9db1e04f7fe2295 100644 (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,
index 694e1195256adbbffba44f7fd318533699d26a43..26ef31ee6ec9aa368f70028837450ddd0edcb4c6 100644 (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,
index b55bb3ffcb8142308af70199eabb3530373642e6..7ef7aa51388013bcc9c34fe3065782b2f0bda557 100644 (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",
index 9b6ccfd6c9639700b1710ff8e1baa5e9ff335786..af4be4e4433801a7c218d33b3b4d81b4c6b26301 100644 (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))
index d5b998263c47edc4ef257580c3a9f7e6953b7c7f..cca9f222ab9786f1e6f38e78a27625e4debc4e0c 100644 (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,