[libvirt PATCH v6 3/8] virxml: Add virXMLPropInt

Tim Wiederhake twiederh at redhat.com
Fri Apr 16 09:41:47 UTC 2021


Convenience function to return the value of an integer XML attribute.

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
Reviewed-by: Peter Krempa <pkrempa at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 53 ++++++++++++++++++++++++++++++++++++++++
 src/util/virxml.h        |  8 ++++++
 3 files changed, 62 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ba70e8b4ca..11e7b0c741 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3548,6 +3548,7 @@ virXMLNodeSanitizeNamespaces;
 virXMLNodeToString;
 virXMLParseHelper;
 virXMLPickShellSafeComment;
+virXMLPropInt;
 virXMLPropString;
 virXMLPropStringLimit;
 virXMLPropTristateBool;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 1b1c3878e6..6da4a2b1f6 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -646,6 +646,59 @@ virXMLPropTristateSwitch(xmlNodePtr node,
 }
 
 
+/**
+ * virXMLPropInt:
+ * @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
+ *
+ * Convenience function to return value of an 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
+virXMLPropInt(xmlNodePtr node,
+              const char *name,
+              int base,
+              virXMLPropFlags flags,
+              int *result)
+{
+    g_autofree char *tmp = NULL;
+    int val;
+
+    if (!(tmp = virXMLPropString(node, name))) {
+        if (!(flags & VIR_XML_PROP_REQUIRED))
+            return 0;
+
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Missing required attribute '%s' in element '%s'"),
+                       name, node->name);
+        return -1;
+    }
+
+    if (virStrToLong_i(tmp, NULL, base, &val) < 0) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected integer value"),
+                       name, node->name, tmp);
+        return -1;
+    }
+
+    if ((flags & VIR_XML_PROP_NONZERO) && (val == 0)) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': Zero is not permitted"),
+                       name, node->name);
+        return -1;
+    }
+
+    *result = val;
+    return 1;
+}
+
+
 /**
  * virXPathBoolean:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 4c9497ba17..10c2404f5f 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -36,6 +36,7 @@ xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml)
 typedef enum {
     VIR_XML_PROP_OPTIONAL = 0, /* Attribute may be absent */
     VIR_XML_PROP_REQUIRED = 1 << 0, /* Attribute may not be absent */
+    VIR_XML_PROP_NONZERO = 1 << 1, /* Attribute may not be zero */
 } virXMLPropFlags;
 
 int              virXPathBoolean(const char *xpath,
@@ -96,6 +97,13 @@ int virXMLPropTristateSwitch(xmlNodePtr node,
                              virTristateSwitch *result)
     ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 
+int virXMLPropInt(xmlNodePtr node,
+                  const char *name,
+                  int base,
+                  virXMLPropFlags flags,
+                  int *result)
+    ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
+
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
                                  const char *filename,
-- 
2.26.2




More information about the libvir-list mailing list