]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: storage: Remove virStoragePoolDefParseNode
authorPeter Krempa <pkrempa@redhat.com>
Thu, 22 Sep 2022 15:00:25 +0000 (17:00 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 6 Oct 2022 08:54:24 +0000 (10:54 +0200)
Replace it by proper use of virXMLParse to validate the root node and
allocate the context. The use in the test driver can be directly
replaced by virStoragePoolDefParseXML as both are validated.

The change to the storage driver isn't trivial though as it requires
careful xpath context juggling to parse the nested volumes properly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/storage_conf.c
src/conf/storage_conf.h
src/libvirt_private.syms
src/test/test_driver.c

index d7375a51603aefa01a10a01400671ecb6003b1a7..c11f40488d664830e050b4a53a3b48ea6ebab8cc 100644 (file)
@@ -972,43 +972,21 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
 }
 
 
-virStoragePoolDef *
-virStoragePoolDefParseNode(xmlDocPtr xml,
-                           xmlNodePtr root)
-{
-    g_autoptr(xmlXPathContext) ctxt = NULL;
-
-    if (!virXMLNodeNameEqual(root, "pool")) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("unexpected root element <%s>, "
-                         "expecting <pool>"),
-                       root->name);
-        return NULL;
-    }
-
-    if (!(ctxt = virXMLXPathContextNew(xml)))
-        return NULL;
-
-    ctxt->node = root;
-    return virStoragePoolDefParseXML(ctxt);
-}
-
-
 static virStoragePoolDef *
 virStoragePoolDefParse(const char *xmlStr,
                        const char *filename,
                        unsigned int flags)
 {
-    virStoragePoolDef *ret = NULL;
     g_autoptr(xmlDoc) xml = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     bool validate = flags & VIR_STORAGE_POOL_DEFINE_VALIDATE;
 
-    if ((xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)"),
-                           NULL, NULL, "storagepool.rng", validate))) {
-        ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
-    }
 
-    return ret;
+    if (!(xml = virXMLParse(filename, xmlStr, _("(storage_pool_definition)"),
+                            "pool", &ctxt, "storagepool.rng", validate)))
+        return NULL;
+
+    return virStoragePoolDefParseXML(ctxt);
 }
 
 
index a1bf243935c563ccb7072c800b6e048a0f6ed853..5eee5b68819921c301e63de8b02567acd3827936 100644 (file)
@@ -278,10 +278,6 @@ virStoragePoolDefParseString(const char *xml,
 virStoragePoolDef *
 virStoragePoolDefParseFile(const char *filename);
 
-virStoragePoolDef *
-virStoragePoolDefParseNode(xmlDocPtr xml,
-                           xmlNodePtr root);
-
 char *
 virStoragePoolDefFormat(virStoragePoolDef *def);
 
index 3ef32101845886f0b11062d14f4b4fba1b1e99cb..ddc2394fb9591e0e8c31758ceaf0d52c8d16dc8d 100644 (file)
@@ -1054,7 +1054,6 @@ virStoragePartedFsTypeToString;
 virStoragePoolDefFormat;
 virStoragePoolDefFree;
 virStoragePoolDefParseFile;
-virStoragePoolDefParseNode;
 virStoragePoolDefParseSourceString;
 virStoragePoolDefParseString;
 virStoragePoolFormatDiskTypeFromString;
index 5eae22f591897e752deab38ed72c187a01c029ad..27cf3ced43057dd3bbb4a080e3f76166dcfceeb9 100644 (file)
@@ -1145,20 +1145,15 @@ testParseInterfaces(testDriver *privconn,
 static int
 testOpenVolumesForPool(const char *file,
                        xmlXPathContextPtr ctxt,
-                       virStoragePoolObj *obj,
-                       int objidx)
+                       virStoragePoolObj *obj)
 {
     virStoragePoolDef *def = virStoragePoolObjGetDef(obj);
     size_t i;
     int num;
-    g_autofree char *vol_xpath = NULL;
     g_autofree xmlNodePtr *nodes = NULL;
     g_autoptr(virStorageVolDef) volDef = NULL;
 
-    /* Find storage volumes */
-    vol_xpath = g_strdup_printf("/node/pool[%d]/volume", objidx);
-
-    num = virXPathNodeSet(vol_xpath, ctxt, &nodes);
+    num = virXPathNodeSet("/pool/volume", ctxt, &nodes);
     if (num < 0)
         return -1;
 
@@ -1195,6 +1190,7 @@ testParseStorage(testDriver *privconn,
                  const char *file,
                  xmlXPathContextPtr ctxt)
 {
+    VIR_XPATH_NODE_AUTORESTORE(ctxt)
     int num;
     size_t i;
     virStoragePoolObj *obj;
@@ -1206,12 +1202,11 @@ testParseStorage(testDriver *privconn,
 
     for (i = 0; i < num; i++) {
         virStoragePoolDef *def;
-        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file);
-        if (!node)
+
+        if (!(ctxt->node = testParseXMLDocFromFile(nodes[i], file)))
             return -1;
 
-        def = virStoragePoolDefParseNode(ctxt->doc, node);
-        if (!def)
+        if (!(def = virStoragePoolDefParseXML(ctxt)))
             return -1;
 
         if (!(obj = virStoragePoolObjListAdd(privconn->pools, &def, 0))) {
@@ -1226,7 +1221,7 @@ testParseStorage(testDriver *privconn,
         virStoragePoolObjSetActive(obj, true);
 
         /* Find storage volumes */
-        if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
+        if (testOpenVolumesForPool(file, ctxt, obj) < 0) {
             virStoragePoolObjEndAPI(&obj);
             return -1;
         }