util: xml: Introduce virXMLPropUUID

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

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-09-16 08:19:31 +02:00
parent a38c47bfb9
commit fe54d2a94f
3 changed files with 53 additions and 0 deletions

View File

@ -3688,6 +3688,7 @@ virXMLPropTristateBoolAllowDefault;
virXMLPropTristateSwitch;
virXMLPropUInt;
virXMLPropULongLong;
virXMLPropUUID;
virXMLSaveFile;
virXMLValidateAgainstSchema;
virXMLValidatorFree;

View File

@ -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

View File

@ -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,