[PATCH 1/9] util: xml: Add wrapper for xmlXPathNewContext

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

 



The wrapper reports libvirt errors for the libxml2 function so that
the same does not have to be repeated over and over.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/conf/checkpoint_conf.c       |  5 +----
 src/conf/domain_conf.c           |  9 ++-------
 src/conf/interface_conf.c        |  5 +----
 src/conf/network_conf.c          |  5 +----
 src/conf/node_device_conf.c      |  5 +----
 src/conf/nwfilter_conf.c         |  5 +----
 src/conf/secret_conf.c           |  6 ++----
 src/conf/snapshot_conf.c         |  5 +----
 src/conf/storage_conf.c          | 10 ++--------
 src/conf/virnetworkportdef.c     |  5 +----
 src/conf/virnwfilterbindingdef.c |  5 +----
 src/conf/virnwfilterbindingobj.c |  5 +----
 src/libvirt_private.syms         |  1 +
 src/qemu/qemu_capabilities.c     |  4 +---
 src/util/virxml.c                | 20 ++++++++++++++++----
 src/util/virxml.h                |  3 +++
 src/vbox/vbox_snapshot_conf.c    | 17 ++++++++---------
 src/vz/vz_sdk.c                  |  6 ++----
 18 files changed, 46 insertions(+), 75 deletions(-)

diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c
index a3513aaa65..113d85cc14 100644
--- a/src/conf/checkpoint_conf.c
+++ b/src/conf/checkpoint_conf.c
@@ -227,11 +227,8 @@ virDomainCheckpointDefParseNode(xmlDocPtr xml,
     if (virXMLValidateAgainstSchema(schema, xml) < 0)
         return NULL;

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virDomainCheckpointDefParse(ctxt, caps, xmlopt, parseOpaque, flags);
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 848c831330..e5e3f31f76 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -21575,11 +21575,8 @@ virDomainDefParseNode(xmlDocPtr xml,
     virDomainDefPtr def = NULL;
     virDomainDefPtr ret = NULL;

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;

@@ -21621,10 +21618,8 @@ virDomainObjParseNode(xmlDocPtr xml,
         goto cleanup;
     }

-    if (!(ctxt = xmlXPathNewContext(xml))) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     obj = virDomainObjParseXML(xml, ctxt, caps, xmlopt, flags);
diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c
index b134ff4adc..64729aea43 100644
--- a/src/conf/interface_conf.c
+++ b/src/conf/interface_conf.c
@@ -830,11 +830,8 @@ virInterfaceDefParseNode(xmlDocPtr xml,
         return NULL;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_LAST);
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index aa0d48af8f..585c87a9f4 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -2125,11 +2125,8 @@ virNetworkDefParseNode(xmlDocPtr xml,
         return NULL;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virNetworkDefParseXML(ctxt, xmlopt);
diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c
index 618ce8e00e..8f8830aef1 100644
--- a/src/conf/node_device_conf.c
+++ b/src/conf/node_device_conf.c
@@ -2063,11 +2063,8 @@ virNodeDeviceDefParseNode(xmlDocPtr xml,
         return NULL;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virNodeDeviceDefParseXML(ctxt, create, virt_type);
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index cecee51d87..21885eb7ae 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2756,11 +2756,8 @@ virNWFilterDefParseNode(xmlDocPtr xml,
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virNWFilterDefParseXML(ctxt);
diff --git a/src/conf/secret_conf.c b/src/conf/secret_conf.c
index b291339e77..a7691da7b5 100644
--- a/src/conf/secret_conf.c
+++ b/src/conf/secret_conf.c
@@ -144,11 +144,9 @@ secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root)
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }
+
     ctxt->node = root;

     if (VIR_ALLOC(def) < 0)
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index cce9a7999c..8cf80aed83 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -450,11 +450,8 @@ virDomainSnapshotDefParseNode(xmlDocPtr xml,
             goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virDomainSnapshotDefParse(ctxt, caps, xmlopt, parseOpaque, current, flags);
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 024f047fab..0560994281 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1024,11 +1024,8 @@ virStoragePoolDefParseNode(xmlDocPtr xml,
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virStoragePoolDefParseXML(ctxt);
@@ -1468,11 +1465,8 @@ virStorageVolDefParseNode(virStoragePoolDefPtr pool,
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virStorageVolDefParseXML(pool, ctxt, flags);
diff --git a/src/conf/virnetworkportdef.c b/src/conf/virnetworkportdef.c
index 29ecf5b178..2e20bff66e 100644
--- a/src/conf/virnetworkportdef.c
+++ b/src/conf/virnetworkportdef.c
@@ -277,11 +277,8 @@ virNetworkPortDefParseNode(xmlDocPtr xml,
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virNetworkPortDefParseXML(ctxt);
diff --git a/src/conf/virnwfilterbindingdef.c b/src/conf/virnwfilterbindingdef.c
index facca61833..6f13bc581a 100644
--- a/src/conf/virnwfilterbindingdef.c
+++ b/src/conf/virnwfilterbindingdef.c
@@ -182,11 +182,8 @@ virNWFilterBindingDefParseNode(xmlDocPtr xml,
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }

     ctxt->node = root;
     def = virNWFilterBindingDefParseXML(ctxt);
diff --git a/src/conf/virnwfilterbindingobj.c b/src/conf/virnwfilterbindingobj.c
index 68afb9c434..06dd66a5d1 100644
--- a/src/conf/virnwfilterbindingobj.c
+++ b/src/conf/virnwfilterbindingobj.c
@@ -248,11 +248,8 @@ virNWFilterBindingObjParseNode(xmlDocPtr doc,
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(doc);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(doc)))
         goto cleanup;
-    }

     ctxt->node = root;
     obj = virNWFilterBindingObjParseXML(doc, ctxt);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4865edaf59..cd880d2c43 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3366,6 +3366,7 @@ virXMLValidateAgainstSchema;
 virXMLValidatorFree;
 virXMLValidatorInit;
 virXMLValidatorValidate;
+virXMLXPathContextNew;
 virXPathBoolean;
 virXPathContextNodeRestore;
 virXPathInt;
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 9b19930964..01d3b008f0 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3612,10 +3612,8 @@ virQEMUCapsLoadCache(virArch hostArch,
     if (!(doc = virXMLParseFile(filename)))
         goto cleanup;

-    if (!(ctxt = xmlXPathNewContext(doc))) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(doc)))
         goto cleanup;
-    }

     ctxt->node = xmlDocGetRootElement(doc);

diff --git a/src/util/virxml.c b/src/util/virxml.c
index f9c117dd58..245ca0a752 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -46,6 +46,20 @@ struct virParserData {
 };


+xmlXPathContextPtr
+virXMLXPathContextNew(xmlDocPtr xml)
+{
+    xmlXPathContextPtr ctxt;
+
+    if (!(ctxt = xmlXPathNewContext(xml))) {
+        virReportOOMError();
+        return NULL;
+    }
+
+    return ctxt;
+}
+
+
 /**
  * virXPathString:
  * @xpath: the XPath string to evaluate
@@ -824,11 +838,9 @@ virXMLParseHelper(int domcode,
     }

     if (ctxt) {
-        *ctxt = xmlXPathNewContext(xml);
-        if (!*ctxt) {
-            virReportOOMError();
+        if (!(*ctxt = virXMLXPathContextNew(xml)))
             goto error;
-        }
+
         (*ctxt)->node = xmlDocGetRootElement(xml);
     }

diff --git a/src/util/virxml.h b/src/util/virxml.h
index 30cc895436..39e5a988a2 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -30,6 +30,9 @@
 #include "virbuffer.h"
 #include "virautoclean.h"

+xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml)
+    ATTRIBUTE_RETURN_CHECK;
+
 int              virXPathBoolean(const char *xpath,
                                  xmlXPathContextPtr ctxt);
 char *            virXPathString(const char *xpath,
diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c
index a36da30a4f..a6daf0ffcf 100644
--- a/src/vbox/vbox_snapshot_conf.c
+++ b/src/vbox/vbox_snapshot_conf.c
@@ -615,10 +615,9 @@ virVBoxSnapshotConfLoadVboxFile(const char *filePath,
                        _("Unable to parse the xml"));
         goto cleanup;
     }
-    if (!(xPathContext = xmlXPathNewContext(xml))) {
-        virReportOOMError();
+    if (!(xPathContext = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }
+
     if (xmlXPathRegisterNs(xPathContext,
                            BAD_CAST "vbox",
                            BAD_CAST "http://www.innotek.de/VirtualBox-settings";) < 0) {
@@ -1299,10 +1298,10 @@ virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath,
                        _("Unable to parse the xml"));
         goto cleanup;
     }
-    if (!(xPathContext = xmlXPathNewContext(xml))) {
-        virReportOOMError();
+
+    if (!(xPathContext = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }
+
     xPathContext->node = xmlDocGetRootElement(xml);
     if ((nodeSize = virXPathNodeSet("/domainsnapshot/disks/disk",
                                     xPathContext, &nodes)) < 0)
@@ -1360,10 +1359,10 @@ virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath,
                        _("Unable to parse the xml"));
         goto cleanup;
     }
-    if (!(xPathContext = xmlXPathNewContext(xml))) {
-        virReportOOMError();
+
+    if (!(xPathContext = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }
+
     xPathContext->node = xmlDocGetRootElement(xml);
     if ((nodeSize = virXPathNodeSet("/domainsnapshot/domain/devices/disk",
                                     xPathContext,
diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c
index 478443298f..c6e3398620 100644
--- a/src/vz/vz_sdk.c
+++ b/src/vz/vz_sdk.c
@@ -4674,11 +4674,9 @@ prlsdkParseSnapshotTree(const char *treexml)
         goto cleanup;
     }

-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
+    if (!(ctxt = virXMLXPathContextNew(xml)))
         goto cleanup;
-    }
+
     ctxt->node = root;

     if ((n = virXPathNodeSet("//SavedStateItem", ctxt, &nodes)) < 0) {
-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[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