xml: Use virXMLParse* helpers everywhere

virt-aa-helper isn't even compile tested since I don't have the setup for
it.

v2:
    virt-aa-helper fixes from Eric
This commit is contained in:
Cole Robinson 2011-05-12 17:20:47 -04:00
parent 34639a3f6b
commit b9e51e5677
6 changed files with 23 additions and 171 deletions

View File

@ -4877,26 +4877,17 @@ virDomainDeviceDefPtr virDomainDeviceDefParse(virCapsPtr caps,
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
virDomainDeviceDefPtr dev = NULL; virDomainDeviceDefPtr dev = NULL;
if (!(xml = xmlReadDoc(BAD_CAST xmlStr, "device.xml", NULL, if (!(xml = virXMLParseString(xmlStr, "device.xml"))) {
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
virDomainReportError(VIR_ERR_XML_ERROR, NULL);
goto error; goto error;
} }
node = xmlDocGetRootElement(xml); node = xmlDocGetRootElement(xml);
if (node == NULL) {
virDomainReportError(VIR_ERR_XML_ERROR,
"%s", _("missing root element"));
goto error;
}
ctxt = xmlXPathNewContext(xml); ctxt = xmlXPathNewContext(xml);
if (ctxt == NULL) { if (ctxt == NULL) {
virReportOOMError(); virReportOOMError();
goto error; goto error;
} }
ctxt->node = node; ctxt->node = xmlDocGetRootElement(xml);
if (VIR_ALLOC(dev) < 0) { if (VIR_ALLOC(dev) < 0) {
virReportOOMError(); virReportOOMError();
@ -9048,7 +9039,6 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
{ {
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlNodePtr root;
virDomainSnapshotDefPtr def = NULL; virDomainSnapshotDefPtr def = NULL;
virDomainSnapshotDefPtr ret = NULL; virDomainSnapshotDefPtr ret = NULL;
char *creation = NULL, *state = NULL; char *creation = NULL, *state = NULL;
@ -9056,22 +9046,9 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
xml = virXMLParse(NULL, xmlStr, "domainsnapshot.xml"); xml = virXMLParse(NULL, xmlStr, "domainsnapshot.xml");
if (!xml) { if (!xml) {
virDomainReportError(VIR_ERR_XML_ERROR,
"%s",_("failed to parse snapshot xml document"));
return NULL; return NULL;
} }
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virDomainReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
if (!xmlStrEqual(root->name, BAD_CAST "domainsnapshot")) {
virDomainReportError(VIR_ERR_XML_ERROR, "%s", _("domainsnapshot"));
goto cleanup;
}
ctxt = xmlXPathNewContext(xml); ctxt = xmlXPathNewContext(xml);
if (ctxt == NULL) { if (ctxt == NULL) {
virReportOOMError(); virReportOOMError();
@ -9083,7 +9060,11 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseString(const char *xmlStr,
goto cleanup; goto cleanup;
} }
ctxt->node = root; ctxt->node = xmlDocGetRootElement(xml);
if (!xmlStrEqual(ctxt->node->name, BAD_CAST "domainsnapshot")) {
virDomainReportError(VIR_ERR_XML_ERROR, "%s", _("domainsnapshot"));
goto cleanup;
}
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);

View File

@ -2066,28 +2066,6 @@ virNWFilterDefParseXML(xmlXPathContextPtr ctxt) {
} }
/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt) {
virConnectPtr conn = ctxt->_private;
if (conn &&
conn->err.code == VIR_ERR_NONE &&
ctxt->lastError.level == XML_ERR_FATAL &&
ctxt->lastError.message != NULL) {
virNWFilterReportError(VIR_ERR_XML_DETAIL,
_("at line %d: %s"),
ctxt->lastError.line,
ctxt->lastError.message);
}
}
}
virNWFilterDefPtr virNWFilterDefPtr
virNWFilterDefParseNode(xmlDocPtr xml, virNWFilterDefParseNode(xmlDocPtr xml,
xmlNodePtr root) { xmlNodePtr root) {
@ -2117,58 +2095,18 @@ cleanup:
static virNWFilterDefPtr static virNWFilterDefPtr
virNWFilterDefParse(virConnectPtr conn, virNWFilterDefParse(virConnectPtr conn ATTRIBUTE_UNUSED,
const char *xmlStr, const char *xmlStr,
const char *filename) { const char *filename) {
virNWFilterDefPtr ret = NULL; virNWFilterDefPtr def = NULL;
xmlParserCtxtPtr pctxt; xmlDocPtr xml;
xmlDocPtr xml = NULL;
xmlNodePtr node = NULL;
/* Set up a parser context so we can catch the details of XML errors. */ if ((xml = virXMLParse(filename, xmlStr, "nwfilter.xml"))) {
pctxt = xmlNewParserCtxt (); def = virNWFilterDefParseNode(xml, xmlDocGetRootElement(xml));
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
pctxt->_private = conn;
if (conn) virResetError (&conn->err);
if (filename) {
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
} else {
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr,
"nwfilter.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
}
if (!xml) {
if (conn && conn->err.code == VIR_ERR_NONE)
virNWFilterReportError(VIR_ERR_XML_ERROR,
"%s",_("failed to parse xml document"));
goto cleanup;
}
node = xmlDocGetRootElement(xml);
if (node == NULL) {
virNWFilterReportError(VIR_ERR_XML_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
ret = virNWFilterDefParseNode(xml, node);
xmlFreeParserCtxt (pctxt);
xmlFreeDoc(xml); xmlFreeDoc(xml);
}
return ret; return def;
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc(xml);
return NULL;
} }

View File

@ -505,13 +505,7 @@ virStoragePoolDefParseSourceString(const char *srcSpec,
xmlXPathContextPtr xpath_ctxt = NULL; xmlXPathContextPtr xpath_ctxt = NULL;
virStoragePoolSourcePtr def = NULL, ret = NULL; virStoragePoolSourcePtr def = NULL, ret = NULL;
doc = xmlReadDoc((const xmlChar *)srcSpec, "srcSpec.xml", NULL, if (!(doc = virXMLParseString(srcSpec, "srcSpec.xml"))) {
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (doc == NULL) {
virStorageReportError(VIR_ERR_XML_ERROR,
"%s", _("bad <source> spec"));
goto cleanup; goto cleanup;
} }

View File

@ -894,20 +894,10 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
(*response)->content = virBufferContentAndReset(&buffer); (*response)->content = virBufferContentAndReset(&buffer);
if ((*response)->responseCode == 500 || (*response)->responseCode == 200) { if ((*response)->responseCode == 500 || (*response)->responseCode == 200) {
(*response)->document = xmlReadDoc(BAD_CAST (*response)->content, "", (*response)->document = virXMLParseString((*response)->content,
NULL, XML_PARSE_NONET); "esx.xml");
if ((*response)->document == NULL) { if ((*response)->document == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Response for call to '%s' could not be parsed"),
methodName);
goto cleanup;
}
if (xmlDocGetRootElement((*response)->document) == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Response for call to '%s' is an empty XML document"),
methodName);
goto cleanup; goto cleanup;
} }

View File

@ -42,6 +42,8 @@
#include "files.h" #include "files.h"
#include "configmake.h" #include "configmake.h"
#define VIR_FROM_THIS VIR_FROM_SECURITY
static char *progname; static char *progname;
typedef struct { typedef struct {
@ -592,29 +594,6 @@ valid_path(const char *path, const bool readonly)
return 0; return 0;
} }
/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt) {
if (virGetLastError() == NULL &&
ctxt->lastError.level == XML_ERR_FATAL &&
ctxt->lastError.message != NULL) {
char *err_str = NULL;
if (virAsprintf(&err_str, "XML error at line %d: %s",
ctxt->lastError.line,
ctxt->lastError.message) == -1)
vah_error(NULL, 0, _("could not get XML error"));
else {
vah_error(NULL, 0, err_str);
VIR_FREE(err_str);
}
}
}
}
static int static int
verify_xpath_context(xmlXPathContextPtr ctxt) verify_xpath_context(xmlXPathContextPtr ctxt)
{ {
@ -658,31 +637,15 @@ static int
caps_mockup(vahControl * ctl, const char *xmlStr) caps_mockup(vahControl * ctl, const char *xmlStr)
{ {
int rc = -1; int rc = -1;
xmlParserCtxtPtr pctxt = NULL;
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
xmlNodePtr root; xmlNodePtr root;
/* Set up a parser context so we can catch the details of XML errors. */ if (!(xml = virXMLParseString(xmlStr, "domain.xml"))) {
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "domain.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
vah_error(NULL, 0, _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
vah_error(NULL, 0, _("missing root element"));
goto cleanup; goto cleanup;
} }
root = xmlDocGetRootElement(xml);
if (!xmlStrEqual(root->name, BAD_CAST "domain")) { if (!xmlStrEqual(root->name, BAD_CAST "domain")) {
vah_error(NULL, 0, _("incorrect root element")); vah_error(NULL, 0, _("incorrect root element"));
goto cleanup; goto cleanup;
@ -725,7 +688,6 @@ caps_mockup(vahControl * ctl, const char *xmlStr)
rc = 0; rc = 0;
cleanup: cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml); xmlFreeDoc (xml);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);

View File

@ -746,7 +746,7 @@ error:
static int testOpenFromFile(virConnectPtr conn, static int testOpenFromFile(virConnectPtr conn,
const char *file) { const char *file) {
int fd = -1, i, ret; int i, ret;
long l; long l;
char *str; char *str;
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
@ -779,22 +779,10 @@ static int testOpenFromFile(virConnectPtr conn,
if (!(privconn->caps = testBuildCapabilities(conn))) if (!(privconn->caps = testBuildCapabilities(conn)))
goto error; goto error;
if ((fd = open(file, O_RDONLY)) < 0) { if (!(xml = virXMLParseFile(file))) {
virReportSystemError(errno,
_("loading host definition file '%s'"),
file);
goto error; goto error;
} }
if (!(xml = xmlReadFd(fd, file, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
testError(VIR_ERR_INTERNAL_ERROR,
_("Invalid XML in file '%s'"), file);
goto error;
}
VIR_FORCE_CLOSE(fd);
root = xmlDocGetRootElement(xml); root = xmlDocGetRootElement(xml);
if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) { if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
testError(VIR_ERR_XML_ERROR, "%s", testError(VIR_ERR_XML_ERROR, "%s",
@ -1100,7 +1088,6 @@ static int testOpenFromFile(virConnectPtr conn,
VIR_FREE(networks); VIR_FREE(networks);
VIR_FREE(ifaces); VIR_FREE(ifaces);
VIR_FREE(pools); VIR_FREE(pools);
VIR_FORCE_CLOSE(fd);
virDomainObjListDeinit(&privconn->domains); virDomainObjListDeinit(&privconn->domains);
virNetworkObjListFree(&privconn->networks); virNetworkObjListFree(&privconn->networks);
virInterfaceObjListFree(&privconn->ifaces); virInterfaceObjListFree(&privconn->ifaces);