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,