[PATCH 17/37] util: xml: Introduce virXMLPropLongLong

Peter Krempa posted 37 patches 3 years, 4 months ago
[PATCH 17/37] util: xml: Introduce virXMLPropLongLong
Posted by Peter Krempa 3 years, 4 months ago
Add a helper for parsing long long values from XML properties with
semantics like virXMLPropInt.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 64 ++++++++++++++++++++++++++++++++++++++++
 src/util/virxml.h        |  9 ++++++
 3 files changed, 74 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 01bb349e05..5be40dbefe 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3682,6 +3682,7 @@ virXMLPickShellSafeComment;
 virXMLPropEnum;
 virXMLPropEnumDefault;
 virXMLPropInt;
+virXMLPropLongLong;
 virXMLPropString;
 virXMLPropStringRequired;
 virXMLPropTristateBool;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index b94af9c7a3..0a91fd237e 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -750,6 +750,70 @@ virXMLPropUInt(xmlNodePtr node,
 }


+/**
+ * virXMLPropLongLong:
+ * @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 a long long 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
+virXMLPropLongLong(xmlNodePtr node,
+                   const char* name,
+                   int base,
+                   virXMLPropFlags flags,
+                   long long *result,
+                   long long defaultResult)
+{
+    g_autofree char *tmp = NULL;
+    long long val;
+
+    *result = defaultResult;
+
+    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_ll(tmp, NULL, base, &val) < 0) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected long long integer value"),
+                       name, node->name, tmp);
+        return -1;
+    }
+
+    if ((flags & VIR_XML_PROP_NONNEGATIVE) && (val < 0)) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected non-negative 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;
+}
+
+
 /**
  * virXMLPropULongLong:
  * @node: XML dom node pointer
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 42d3740c58..3b00d20ea9 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -141,6 +141,15 @@ virXMLPropUInt(xmlNodePtr node,
                unsigned int *result)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5);

+int
+virXMLPropLongLong(xmlNodePtr node,
+                   const char* name,
+                   int base,
+                   virXMLPropFlags flags,
+                   long long *result,
+                   long long defaultResult)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5);
+
 int
 virXMLPropULongLong(xmlNodePtr node,
                     const char* name,
-- 
2.37.1
Re: [PATCH 17/37] util: xml: Introduce virXMLPropLongLong
Posted by Ján Tomko 3 years, 4 months ago
On a Monday in 2022, Peter Krempa wrote:
>Add a helper for parsing long long values from XML properties with
>semantics like virXMLPropInt.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> src/libvirt_private.syms |  1 +
> src/util/virxml.c        | 64 ++++++++++++++++++++++++++++++++++++++++
> src/util/virxml.h        |  9 ++++++
> 3 files changed, 74 insertions(+)
>
>diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
>index 01bb349e05..5be40dbefe 100644
>--- a/src/libvirt_private.syms
>+++ b/src/libvirt_private.syms
>@@ -3682,6 +3682,7 @@ virXMLPickShellSafeComment;
> virXMLPropEnum;
> virXMLPropEnumDefault;
> virXMLPropInt;
>+virXMLPropLongLong;
> virXMLPropString;
> virXMLPropStringRequired;
> virXMLPropTristateBool;
>diff --git a/src/util/virxml.c b/src/util/virxml.c
>index b94af9c7a3..0a91fd237e 100644
>--- a/src/util/virxml.c
>+++ b/src/util/virxml.c
>@@ -750,6 +750,70 @@ virXMLPropUInt(xmlNodePtr node,
> }
>
>
>+/**
>+ * virXMLPropLongLong:
>+ * @node: XML dom node pointer
>+ * @name: Name of the property (attribute) to get
>+ * @base: Number base, see strtol
>+ * @flags: Bitwise or of virXMLPropFlags

*Bitwise-OR

>+ * @result: The returned value
>+ * @defaultResult: default value of @result in case the property is not found

*Default value

>+ *
>+ * Convenience function to return value of a long long 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.
>+ */

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano