conf: storage: Remove virStoragePoolDefParseNode

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>
This commit is contained in:
Peter Krempa 2022-09-22 17:00:25 +02:00
parent ee3b96bcb0
commit b3e33a0ef7
4 changed files with 13 additions and 45 deletions

View 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 * static virStoragePoolDef *
virStoragePoolDefParse(const char *xmlStr, virStoragePoolDefParse(const char *xmlStr,
const char *filename, const char *filename,
unsigned int flags) unsigned int flags)
{ {
virStoragePoolDef *ret = NULL;
g_autoptr(xmlDoc) xml = NULL; g_autoptr(xmlDoc) xml = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
bool validate = flags & VIR_STORAGE_POOL_DEFINE_VALIDATE; 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);
} }

View File

@ -278,10 +278,6 @@ virStoragePoolDefParseString(const char *xml,
virStoragePoolDef * virStoragePoolDef *
virStoragePoolDefParseFile(const char *filename); virStoragePoolDefParseFile(const char *filename);
virStoragePoolDef *
virStoragePoolDefParseNode(xmlDocPtr xml,
xmlNodePtr root);
char * char *
virStoragePoolDefFormat(virStoragePoolDef *def); virStoragePoolDefFormat(virStoragePoolDef *def);

View File

@ -1054,7 +1054,6 @@ virStoragePartedFsTypeToString;
virStoragePoolDefFormat; virStoragePoolDefFormat;
virStoragePoolDefFree; virStoragePoolDefFree;
virStoragePoolDefParseFile; virStoragePoolDefParseFile;
virStoragePoolDefParseNode;
virStoragePoolDefParseSourceString; virStoragePoolDefParseSourceString;
virStoragePoolDefParseString; virStoragePoolDefParseString;
virStoragePoolFormatDiskTypeFromString; virStoragePoolFormatDiskTypeFromString;

View File

@ -1145,20 +1145,15 @@ testParseInterfaces(testDriver *privconn,
static int static int
testOpenVolumesForPool(const char *file, testOpenVolumesForPool(const char *file,
xmlXPathContextPtr ctxt, xmlXPathContextPtr ctxt,
virStoragePoolObj *obj, virStoragePoolObj *obj)
int objidx)
{ {
virStoragePoolDef *def = virStoragePoolObjGetDef(obj); virStoragePoolDef *def = virStoragePoolObjGetDef(obj);
size_t i; size_t i;
int num; int num;
g_autofree char *vol_xpath = NULL;
g_autofree xmlNodePtr *nodes = NULL; g_autofree xmlNodePtr *nodes = NULL;
g_autoptr(virStorageVolDef) volDef = NULL; g_autoptr(virStorageVolDef) volDef = NULL;
/* Find storage volumes */ num = virXPathNodeSet("/pool/volume", ctxt, &nodes);
vol_xpath = g_strdup_printf("/node/pool[%d]/volume", objidx);
num = virXPathNodeSet(vol_xpath, ctxt, &nodes);
if (num < 0) if (num < 0)
return -1; return -1;
@ -1195,6 +1190,7 @@ testParseStorage(testDriver *privconn,
const char *file, const char *file,
xmlXPathContextPtr ctxt) xmlXPathContextPtr ctxt)
{ {
VIR_XPATH_NODE_AUTORESTORE(ctxt)
int num; int num;
size_t i; size_t i;
virStoragePoolObj *obj; virStoragePoolObj *obj;
@ -1206,12 +1202,11 @@ testParseStorage(testDriver *privconn,
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
virStoragePoolDef *def; virStoragePoolDef *def;
xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file);
if (!node) if (!(ctxt->node = testParseXMLDocFromFile(nodes[i], file)))
return -1; return -1;
def = virStoragePoolDefParseNode(ctxt->doc, node); if (!(def = virStoragePoolDefParseXML(ctxt)))
if (!def)
return -1; return -1;
if (!(obj = virStoragePoolObjListAdd(privconn->pools, &def, 0))) { if (!(obj = virStoragePoolObjListAdd(privconn->pools, &def, 0))) {
@ -1226,7 +1221,7 @@ testParseStorage(testDriver *privconn,
virStoragePoolObjSetActive(obj, true); virStoragePoolObjSetActive(obj, true);
/* Find storage volumes */ /* Find storage volumes */
if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) { if (testOpenVolumesForPool(file, ctxt, obj) < 0) {
virStoragePoolObjEndAPI(&obj); virStoragePoolObjEndAPI(&obj);
return -1; return -1;
} }