virDomainHostdevSubsysSCSIiSCSIDefParseXML: Use XPath to fetch elements

Conver the code to the new approach which uses XPath to fetch known
elements rather than looping through all XML children.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-07-10 13:55:18 +02:00
parent a894223e7b
commit fdab2f1a31
1 changed files with 19 additions and 32 deletions

View File

@ -8295,9 +8295,12 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
virDomainXMLOptionPtr xmlopt) virDomainXMLOptionPtr xmlopt)
{ {
int auth_secret_usage = -1; int auth_secret_usage = -1;
xmlNodePtr cur;
virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &def->u.iscsi; virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &def->u.iscsi;
g_autoptr(virStorageAuthDef) authdef = NULL; g_autoptr(virStorageAuthDef) authdef = NULL;
xmlNodePtr node;
VIR_XPATH_NODE_AUTORESTORE(ctxt);
ctxt->node = sourcenode;
/* For the purposes of command line creation, this needs to look /* For the purposes of command line creation, this needs to look
* like a disk storage source */ * like a disk storage source */
@ -8328,42 +8331,26 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
return -1; return -1;
} }
cur = sourcenode->children; if ((node = virXPathNode("./auth", ctxt))) {
while (cur != NULL) { if (!(authdef = virStorageAuthDefParse(node, ctxt)))
if (cur->type == XML_ELEMENT_NODE && return -1;
virXMLNodeNameEqual(cur, "auth")) { if ((auth_secret_usage = virSecretUsageTypeFromString(authdef->secrettype)) < 0) {
if (iscsisrc->src->auth) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("invalid secret type %s"),
_("an <auth> definition already found for " authdef->secrettype);
"the <hostdev> iSCSI definition")); return -1;
return -1;
}
if (!(authdef = virStorageAuthDefParse(cur, ctxt)))
return -1;
if ((auth_secret_usage =
virSecretUsageTypeFromString(authdef->secrettype)) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("invalid secret type %s"),
authdef->secrettype);
return -1;
}
if (auth_secret_usage != VIR_SECRET_USAGE_TYPE_ISCSI) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("hostdev invalid secret type '%s'"),
authdef->secrettype);
return -1;
}
iscsisrc->src->auth = g_steal_pointer(&authdef);
} }
cur = cur->next; if (auth_secret_usage != VIR_SECRET_USAGE_TYPE_ISCSI) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("hostdev invalid secret type '%s'"),
authdef->secrettype);
return -1;
}
iscsisrc->src->auth = g_steal_pointer(&authdef);
} }
if (flags & VIR_DOMAIN_DEF_PARSE_STATUS && if (flags & VIR_DOMAIN_DEF_PARSE_STATUS &&
xmlopt && xmlopt->privateData.storageParse) { xmlopt && xmlopt->privateData.storageParse) {
VIR_XPATH_NODE_AUTORESTORE(ctxt);
ctxt->node = sourcenode;
if ((ctxt->node = virXPathNode("./privateData", ctxt)) && if ((ctxt->node = virXPathNode("./privateData", ctxt)) &&
xmlopt->privateData.storageParse(ctxt, iscsisrc->src) < 0) xmlopt->privateData.storageParse(ctxt, iscsisrc->src) < 0)
return -1; return -1;