From 65eaf58335f0eca789c74726b84910fb48c4cece Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 16 Sep 2022 09:32:48 +0200 Subject: [PATCH] util: xml: Introduce virXMLPropStringRequired MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Similarly to virXMLPropString it extracts a string but reports an error similar to the newer virXMLProp helpers if the attribute is not present. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/libvirt_private.syms | 1 + src/util/virxml.c | 27 +++++++++++++++++++++++++++ src/util/virxml.h | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1e852902ab..01bb349e05 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3683,6 +3683,7 @@ virXMLPropEnum; virXMLPropEnumDefault; virXMLPropInt; virXMLPropString; +virXMLPropStringRequired; virXMLPropTristateBool; virXMLPropTristateBoolAllowDefault; virXMLPropTristateSwitch; diff --git a/src/util/virxml.c b/src/util/virxml.c index 7122d42ef1..7d5f4478c3 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -455,6 +455,33 @@ virXMLPropString(xmlNodePtr node, } +/** + * virXMLPropStringRequired: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * + * Convenience function to return copy of an mandatoryu attribute value of an + * XML node. + * + * Returns the property (attribute) value as string or NULL and if the attribute + * is not present (libvirt error reported). + * The caller is responsible for freeing the returned buffer. + */ +char * +virXMLPropStringRequired(xmlNodePtr node, + const char *name) +{ + char *ret = virXMLPropString(node, name); + + if (!(*ret)) + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"), + name, node->name); + + return ret; +} + + /** * virXMLNodeContentString: * @node: XML dom node pointer diff --git a/src/util/virxml.h b/src/util/virxml.h index b643f0b0c0..59342105d5 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -97,6 +97,10 @@ char * virXMLPropString(xmlNodePtr node, const char *name); char * +virXMLPropStringRequired(xmlNodePtr node, + const char *name); + +char * virXMLNodeContentString(xmlNodePtr node); int