util: xml: Add wrapper for xmlXPathNewContext

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@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Peter Krempa 2019-09-09 08:33:58 +02:00
parent 73aaa8f8c8
commit bfedd14955
18 changed files with 46 additions and 75 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -3365,6 +3365,7 @@ virXMLValidateAgainstSchema;
virXMLValidatorFree;
virXMLValidatorInit;
virXMLValidatorValidate;
virXMLXPathContextNew;
virXPathBoolean;
virXPathContextNodeRestore;
virXPathInt;

View File

@ -3611,10 +3611,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);

View File

@ -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);
}

View File

@ -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,

View File

@ -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,

View File

@ -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) {