From a664916fd2d121a7aa733fac05bbdbf1d4ac2248 Mon Sep 17 00:00:00 2001
From: Tim Wiederhake <twiederh@redhat.com>
Date: Fri, 16 Apr 2021 11:41:45 +0200
Subject: [PATCH] virxml: Add virXMLPropTristateBool

Convenience function to return the value of a yes / no XML attribute.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Shaokun Wei <weishaokun@kylinos.cn>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 44 ++++++++++++++++++++++++++++++++++++++++
 src/util/virxml.h        | 15 ++++++++++++++
 3 files changed, 60 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a00e354859..42b3700d57 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3482,6 +3482,7 @@ virXMLParseHelper;
 virXMLPickShellSafeComment;
 virXMLPropString;
 virXMLPropStringLimit;
+virXMLPropTristateBool;
 virXMLSaveFile;
 virXMLValidateAgainstSchema;
 virXMLValidatorFree;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 9ea7b99dba..a1fe319e08 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -559,6 +559,50 @@ virXMLNodeContentString(xmlNodePtr node)
 }
 
 
+/**
+ * virXMLPropTristateBool:
+ * @node: XML dom node pointer
+ * @name: Name of the property (attribute) to get
+ * @flags: Bitwise or of virXMLPropFlags
+ * @result: The returned value
+ *
+ * Convenience function to return value of a yes / no attribute.
+ *
+ * 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
+virXMLPropTristateBool(xmlNodePtr node,
+                       const char* name,
+                       virXMLPropFlags flags,
+                       virTristateBool *result)
+{
+    g_autofree char *tmp = NULL;
+    int val;
+
+    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 ((val = virTristateBoolTypeFromString(tmp)) <= 0) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'yes' or 'no'"),
+                       name, node->name, tmp);
+        return -1;
+    }
+
+    *result = val;
+    return 1;
+}
+
+
 /**
  * virXPathBoolean:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 26ab9f9c2d..1d7070b98f 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -28,10 +28,18 @@
 #include <libxml/relaxng.h>
 
 #include "virbuffer.h"
+#include "virenum.h"
 
 xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml)
     G_GNUC_WARN_UNUSED_RESULT;
 
+
+typedef enum {
+    VIR_XML_PROP_OPTIONAL = 0, /* Attribute may be absent */
+    VIR_XML_PROP_REQUIRED = 1 << 0, /* Attribute may not be absent */
+} virXMLPropFlags;
+
+
 int              virXPathBoolean(const char *xpath,
                                  xmlXPathContextPtr ctxt);
 char *            virXPathString(const char *xpath,
@@ -79,6 +87,13 @@ char *     virXMLPropStringLimit(xmlNodePtr node,
 char *   virXMLNodeContentString(xmlNodePtr node);
 long     virXMLChildElementCount(xmlNodePtr node);
 
+int
+virXMLPropTristateBool(xmlNodePtr node,
+                       const char *name,
+                       virXMLPropFlags flags,
+                       virTristateBool *result)
+    ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
+
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
                                  const char *filename,
-- 
2.27.0

