Check the root XML node name and fetch XPath context by properly configuring virXMLParse. Callers can use virDomainSnapshotDefParse instead. Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx> --- src/conf/snapshot_conf.c | 40 +++++++++------------------------------- src/conf/snapshot_conf.h | 13 +++++++------ src/test/test_driver.c | 18 ++++++++---------- 3 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index a5974053f4..afdc11876d 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -197,7 +197,7 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node, * If flags does not include * VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL, then current is ignored. */ -static virDomainSnapshotDef * +virDomainSnapshotDef * virDomainSnapshotDefParse(xmlXPathContextPtr ctxt, virDomainXMLOption *xmlopt, void *parseOpaque, @@ -389,27 +389,6 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt, return g_steal_pointer(&def); } -virDomainSnapshotDef * -virDomainSnapshotDefParseNode(xmlDocPtr xml, - xmlNodePtr root, - virDomainXMLOption *xmlopt, - void *parseOpaque, - bool *current, - unsigned int flags) -{ - g_autoptr(xmlXPathContext) ctxt = NULL; - - if (!virXMLNodeNameEqual(root, "domainsnapshot")) { - virReportError(VIR_ERR_XML_ERROR, "%s", _("domainsnapshot")); - return NULL; - } - - if (!(ctxt = virXMLXPathContextNew(xml))) - return NULL; - - ctxt->node = root; - return virDomainSnapshotDefParse(ctxt, xmlopt, parseOpaque, current, flags); -} virDomainSnapshotDef * virDomainSnapshotDefParseString(const char *xmlStr, @@ -418,21 +397,20 @@ virDomainSnapshotDefParseString(const char *xmlStr, bool *current, unsigned int flags) { - virDomainSnapshotDef *ret = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; g_autoptr(xmlDoc) xml = NULL; int keepBlanksDefault = xmlKeepBlanksDefault(0); bool validate = flags & VIR_DOMAIN_SNAPSHOT_PARSE_VALIDATE; - if ((xml = virXMLParse(NULL, xmlStr, _("(domain_snapshot)"), - NULL, NULL, "domainsnapshot.rng", validate))) { - xmlKeepBlanksDefault(keepBlanksDefault); - ret = virDomainSnapshotDefParseNode(xml, xmlDocGetRootElement(xml), - xmlopt, parseOpaque, - current, flags); - } + xml = virXMLParse(NULL, xmlStr, _("(domain_snapshot)"), + "domainsnapshot", &ctxt, "domainsnapshot.rng", validate); + xmlKeepBlanksDefault(keepBlanksDefault); - return ret; + if (!xml) + return NULL; + + return virDomainSnapshotDefParse(ctxt, xmlopt, parseOpaque, current, flags); } diff --git a/src/conf/snapshot_conf.h b/src/conf/snapshot_conf.h index 1f787f1a94..fec4a5a912 100644 --- a/src/conf/snapshot_conf.h +++ b/src/conf/snapshot_conf.h @@ -102,12 +102,13 @@ virDomainSnapshotDef *virDomainSnapshotDefParseString(const char *xmlStr, void *parseOpaque, bool *current, unsigned int flags); -virDomainSnapshotDef *virDomainSnapshotDefParseNode(xmlDocPtr xml, - xmlNodePtr root, - virDomainXMLOption *xmlopt, - void *parseOpaque, - bool *current, - unsigned int flags); +virDomainSnapshotDef * +virDomainSnapshotDefParse(xmlXPathContextPtr ctxt, + virDomainXMLOption *xmlopt, + void *parseOpaque, + bool *current, + unsigned int flags); + virDomainSnapshotDef *virDomainSnapshotDefNew(void); char *virDomainSnapshotDefFormat(const char *uuidstr, virDomainSnapshotDef *def, diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 5a4e240d35..50c8a7e2be 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -961,25 +961,23 @@ testParseDomainSnapshots(testDriver *privconn, const char *file, xmlXPathContextPtr ctxt) { + VIR_XPATH_NODE_AUTORESTORE(ctxt) size_t i; testDomainNamespaceDef *nsdata = domobj->def->namespaceData; xmlNodePtr *nodes = nsdata->snap_nodes; - bool cur; for (i = 0; i < nsdata->num_snap_nodes; i++) { virDomainMomentObj *snap; g_autoptr(virDomainSnapshotDef) def = NULL; - xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file); - if (!node) + unsigned int parseFlags = VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL | + VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE; + bool cur; + + if (!(ctxt->node = testParseXMLDocFromFile(nodes[i], file))) return -1; - def = virDomainSnapshotDefParseNode(ctxt->doc, node, - privconn->xmlopt, - NULL, - &cur, - VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL | - VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE); - if (!def) + if (!(def = virDomainSnapshotDefParse(ctxt, privconn->xmlopt, NULL, + &cur, parseFlags))) return -1; if (!(snap = virDomainSnapshotAssignDef(domobj->snapshots, &def))) -- 2.37.3