[PATCH 2/5] util: add xml validation against schema in virXMLParseHelper()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



We need this in order to validate XML against schema at one
place, rather than have the same code for validation in different
functions.
I will add '--validate' option to more virsh commands soon and
this makes it easier as virXMLParse() is called in every one I
plan to change.

Signed-off-by: Kristina Hanicova <khanicov@xxxxxxxxxx>
---
 src/util/virxml.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 0220c5906f..b896a80460 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 "configmake.h"
 
 #define VIR_FROM_THIS VIR_FROM_XML
 
@@ -1096,8 +1097,8 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
  * @url: URL of XML document for string parser
  * @rootelement: Optional name of the expected root element
  * @ctxt: optional pointer to populate with new context pointer
- * @schemafile: unused
- * @validate: unused
+ * @schemafile: optional name of the file the parsed XML will be validated against
+ * @validate: if true, the XML will be validated against schema in @schemafile
  *
  * Parse XML document provided either as a file or a string. The function
  * guarantees that the XML document contains a root element.
@@ -1114,8 +1115,8 @@ virXMLParseHelper(int domcode,
                   const char *url,
                   const char *rootelement,
                   xmlXPathContextPtr *ctxt,
-                  const char *schemafile G_GNUC_UNUSED,
-                  bool validate G_GNUC_UNUSED)
+                  const char *schemafile,
+                  bool validate)
 {
     struct virParserData private;
     g_autoptr(xmlParserCtxt) pctxt = NULL;
@@ -1181,6 +1182,15 @@ virXMLParseHelper(int domcode,
         (*ctxt)->node = rootnode;
     }
 
+    if (validate && schemafile != NULL) {
+        g_autofree char *schema = virFileFindResource(schemafile,
+                                                      abs_top_srcdir "/docs/schemas",
+                                                      PKGDATADIR "/schemas");
+        if (!schema ||
+            (virXMLValidateAgainstSchema(schema, xml) < 0))
+            return NULL;
+    }
+
     return g_steal_pointer(&xml);
 }
 
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux