]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: xml: Introduce virXMLPropStringRequired
authorPeter Krempa <pkrempa@redhat.com>
Fri, 16 Sep 2022 07:32:48 +0000 (09:32 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 3 Oct 2022 12:43:17 +0000 (14:43 +0200)
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 <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libvirt_private.syms
src/util/virxml.c
src/util/virxml.h

index 1e852902ab10fd693efcd089938212b435a52203..01bb349e058fc84c36f3cf88b5bb51d6e035bb4b 100644 (file)
@@ -3683,6 +3683,7 @@ virXMLPropEnum;
 virXMLPropEnumDefault;
 virXMLPropInt;
 virXMLPropString;
+virXMLPropStringRequired;
 virXMLPropTristateBool;
 virXMLPropTristateBoolAllowDefault;
 virXMLPropTristateSwitch;
index 7122d42ef1962fcfc63038e1b2f7ba6b8a77a8d6..7d5f4478c34c2c2cef100af0a954d8261f8faeb6 100644 (file)
@@ -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
index b643f0b0c070ebebbda16bb2911f6fcd58922c69..59342105d5c2c4cbe9b9a6f3352100bfc1e4cfe3 100644 (file)
@@ -97,6 +97,10 @@ char *
 virXMLPropString(xmlNodePtr node,
                  const char *name);
 char *
+virXMLPropStringRequired(xmlNodePtr node,
+                         const char *name);
+
+char *
 virXMLNodeContentString(xmlNodePtr node);
 
 int