]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virxml: Add virXMLPropULongLong
authorTim Wiederhake <twiederh@redhat.com>
Wed, 21 Apr 2021 15:51:27 +0000 (17:51 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 22 Apr 2021 09:24:53 +0000 (11:24 +0200)
Convenience function to return the value of an unsigned long long XML
attribute.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virxml.c
src/util/virxml.h

index e9bb23913c25e97b827640db9c71f959bdabffd4..8f8d399d88723aa2e717aae0a9d46c586318b428 100644 (file)
@@ -3555,6 +3555,7 @@ virXMLPropStringLimit;
 virXMLPropTristateBool;
 virXMLPropTristateSwitch;
 virXMLPropUInt;
+virXMLPropULongLong;
 virXMLSaveFile;
 virXMLValidateAgainstSchema;
 virXMLValidatorFree;
index 5ceef7381475c51e7a413eafa99b062991020a00..b79050db356086e730b03590da118a6e94cc2253 100644 (file)
@@ -755,6 +755,62 @@ virXMLPropUInt(xmlNodePtr node,
 }
 
 
+/**
+ * virXMLPropULongLong:
+ * @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 unsigned 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
+virXMLPropULongLong(xmlNodePtr node,
+                    const char* name,
+                    int base,
+                    virXMLPropFlags flags,
+                    unsigned long long *result)
+{
+    g_autofree char *tmp = NULL;
+    int ret;
+    unsigned long long 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;
+    }
+
+    ret = virStrToLong_ullp(tmp, NULL, base, &val);
+
+    if (ret < 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;
+}
+
+
 /**
  * virXMLPropEnum:
  * @node: XML dom node pointer
index c83d16a14ae72a1061605a91fcae99ad8ea1d12e..13c543fbb6b2673dc95d52c6954c9e1ca0b79789 100644 (file)
@@ -134,6 +134,14 @@ virXMLPropUInt(xmlNodePtr node,
                unsigned int *result)
     ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
 
+int
+virXMLPropULongLong(xmlNodePtr node,
+                    const char* name,
+                    int base,
+                    virXMLPropFlags flags,
+                    unsigned long long *result)
+    ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
+
 int
 virXMLPropEnum(xmlNodePtr node,
                const char* name,