130 lines
4.0 KiB
Diff
130 lines
4.0 KiB
Diff
|
|
From 122ef42d1d0e1d2d66968ab1e038926261e28e22 Mon Sep 17 00:00:00 2001
|
||
|
|
From: Tim Wiederhake <twiederh@redhat.com>
|
||
|
|
Date: Fri, 16 Apr 2021 11:41:48 +0200
|
||
|
|
Subject: [PATCH] virxml: Add virXMLPropUInt
|
||
|
|
|
||
|
|
Convenience function to return the value of an unsigned integer XML attribute.
|
||
|
|
|
||
|
|
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
|
||
|
|
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
|
||
|
|
Reviewed-by: Shaokun Wei <weishaokun@kylinos.cn>
|
||
|
|
---
|
||
|
|
src/libvirt_private.syms | 1 +
|
||
|
|
src/util/virxml.c | 60 ++++++++++++++++++++++++++++++++++++++++
|
||
|
|
src/util/virxml.h | 9 ++++++
|
||
|
|
3 files changed, 70 insertions(+)
|
||
|
|
|
||
|
|
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
|
||
|
|
index 4720bb0ab0..82d720c7c1 100644
|
||
|
|
--- a/src/libvirt_private.syms
|
||
|
|
+++ b/src/libvirt_private.syms
|
||
|
|
@@ -3485,6 +3485,7 @@ virXMLPropString;
|
||
|
|
virXMLPropStringLimit;
|
||
|
|
virXMLPropTristateBool;
|
||
|
|
virXMLPropTristateSwitch;
|
||
|
|
+virXMLPropUInt;
|
||
|
|
virXMLSaveFile;
|
||
|
|
virXMLValidateAgainstSchema;
|
||
|
|
virXMLValidatorFree;
|
||
|
|
diff --git a/src/util/virxml.c b/src/util/virxml.c
|
||
|
|
index f6f028896e..de86ce3d2b 100644
|
||
|
|
--- a/src/util/virxml.c
|
||
|
|
+++ b/src/util/virxml.c
|
||
|
|
@@ -700,6 +700,66 @@ virXMLPropInt(xmlNodePtr node,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
+/**
|
||
|
|
+ * virXMLPropUInt:
|
||
|
|
+ * @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 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
|
||
|
|
+virXMLPropUInt(xmlNodePtr node,
|
||
|
|
+ const char* name,
|
||
|
|
+ int base,
|
||
|
|
+ virXMLPropFlags flags,
|
||
|
|
+ unsigned int *result)
|
||
|
|
+{
|
||
|
|
+ g_autofree char *tmp = NULL;
|
||
|
|
+ int ret;
|
||
|
|
+ unsigned 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 (flags & VIR_XML_PROP_WRAPNEGATIVE) {
|
||
|
|
+ ret = virStrToLong_ui(tmp, NULL, base, &val);
|
||
|
|
+ } else {
|
||
|
|
+ ret = virStrToLong_uip(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;
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
+
|
||
|
|
/**
|
||
|
|
* virXPathBoolean:
|
||
|
|
* @xpath: the XPath string to evaluate
|
||
|
|
diff --git a/src/util/virxml.h b/src/util/virxml.h
|
||
|
|
index 28538fe035..d953743ab1 100644
|
||
|
|
--- a/src/util/virxml.h
|
||
|
|
+++ b/src/util/virxml.h
|
||
|
|
@@ -38,6 +38,7 @@ 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 */
|
||
|
|
+ VIR_XML_PROP_WRAPNEGATIVE = 1 << 2, /* Wrap around negative values */
|
||
|
|
} virXMLPropFlags;
|
||
|
|
|
||
|
|
|
||
|
|
@@ -110,6 +111,14 @@ virXMLPropInt(xmlNodePtr node,
|
||
|
|
int *result)
|
||
|
|
ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
|
||
|
|
|
||
|
|
+int
|
||
|
|
+virXMLPropUInt(xmlNodePtr node,
|
||
|
|
+ const char* name,
|
||
|
|
+ int base,
|
||
|
|
+ virXMLPropFlags flags,
|
||
|
|
+ unsigned 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.27.0
|
||
|
|
|