[PATCH 12/37] util: xml: Introduce virXMLPropUUID

Peter Krempa pkrempa at redhat.com
Mon Sep 19 08:54:57 UTC 2022


The helper function extracts an UUID with semantics similar to other
helpers we have.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 25794bc2f4..1e852902ab 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3688,6 +3688,7 @@ virXMLPropTristateBoolAllowDefault;
 virXMLPropTristateSwitch;
 virXMLPropUInt;
 virXMLPropULongLong;
+virXMLPropUUID;
 virXMLSaveFile;
 virXMLValidateAgainstSchema;
 virXMLValidatorFree;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index d6e2e5dd91..04a8b29ba3 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -33,6 +33,7 @@
 #include "virfile.h"
 #include "virstring.h"
 #include "virutil.h"
+#include "viruuid.h"
 #include "configmake.h"

 #define VIR_FROM_THIS VIR_FROM_XML
@@ -808,6 +809,50 @@ virXMLPropEnumDefault(xmlNodePtr node,
 }


+/**
+ * virXMLPropUUID:
+ * @node: XML dom node pointer
+ * @name: Name of the property (attribute) to get
+ * @flags: Bitwise or of virXMLPropFlags
+ * @result: array of VIR_UUID_BUFLEN bytes to store the raw UUID
+ *
+ * Convenience function to fetch a XML property as UUID.
+ *
+ * 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
+virXMLPropUUID(xmlNodePtr node,
+               const char* name,
+               virXMLPropFlags flags,
+               unsigned char *result)
+{
+    g_autofree char *tmp = NULL;
+    unsigned char val[VIR_UUID_BUFLEN];
+
+    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 (virUUIDParse(tmp, val) < 0) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected UUID"),
+                       name, node->name, tmp);
+        return -1;
+    }
+
+    memcpy(result, val, VIR_UUID_BUFLEN);
+    return 1;
+}
+
+
 /**
  * virXMLPropEnum:
  * @node: XML dom node pointer
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 539228a9ba..dfb58ff276 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -154,6 +154,13 @@ virXMLPropEnum(xmlNodePtr node,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
     ATTRIBUTE_NONNULL(5);

+int
+virXMLPropUUID(xmlNodePtr node,
+               const char* name,
+               virXMLPropFlags flags,
+               unsigned char *result)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
+
 int
 virXMLPropEnumDefault(xmlNodePtr node,
                       const char* name,
-- 
2.37.1



More information about the libvir-list mailing list