From fe54d2a94fbe0c8193a063906982c62fa257ac8c Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 16 Sep 2022 08:19:31 +0200 Subject: [PATCH] util: xml: Introduce virXMLPropUUID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The helper function extracts a UUID with semantics similar to other helpers we have. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- 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..f92cd32dfa 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 an XML property as a 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..b643f0b0c0 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,