qemuDomainDetachDeviceLiveAndConfig: Parse XML twice rather than use virDomainDeviceDefCopy

'virDomainDeviceDefCopy' formats the definition and parses it back.
Since we already are parsing the XML here, we're better off parsing it
twice and save the formatting step.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-11-22 17:01:31 +01:00
parent 645afd640c
commit 333fb6714e

View File

@ -7865,8 +7865,8 @@ qemuDomainDetachDeviceLiveAndConfig(virQEMUDriver *driver,
{ {
qemuDomainObjPrivate *priv = vm->privateData; qemuDomainObjPrivate *priv = vm->privateData;
g_autoptr(virQEMUDriverConfig) cfg = NULL; g_autoptr(virQEMUDriverConfig) cfg = NULL;
g_autoptr(virDomainDeviceDef) dev = NULL; g_autoptr(virDomainDeviceDef) dev_config = NULL;
virDomainDeviceDef *dev_copy = NULL; g_autoptr(virDomainDeviceDef) dev_live = NULL;
unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE;
g_autoptr(virDomainDef) vmdef = NULL; g_autoptr(virDomainDef) vmdef = NULL;
int ret = -1; int ret = -1;
@ -7880,21 +7880,15 @@ qemuDomainDetachDeviceLiveAndConfig(virQEMUDriver *driver,
!(flags & VIR_DOMAIN_AFFECT_LIVE)) !(flags & VIR_DOMAIN_AFFECT_LIVE))
parse_flags |= VIR_DOMAIN_DEF_PARSE_INACTIVE; parse_flags |= VIR_DOMAIN_DEF_PARSE_INACTIVE;
dev = dev_copy = virDomainDeviceDefParse(xml, vm->def, if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
driver->xmlopt, priv->qemuCaps, if (!(dev_config = virDomainDeviceDefParse(xml, vm->def, driver->xmlopt,
parse_flags); priv->qemuCaps, parse_flags)))
if (dev == NULL) goto cleanup;
goto cleanup; }
if (flags & VIR_DOMAIN_AFFECT_CONFIG && if (flags & VIR_DOMAIN_AFFECT_LIVE) {
flags & VIR_DOMAIN_AFFECT_LIVE) { if (!(dev_live = virDomainDeviceDefParse(xml, vm->def, driver->xmlopt,
/* If we are affecting both CONFIG and LIVE priv->qemuCaps, parse_flags)))
* create a deep copy of device as adding
* to CONFIG takes one instance.
*/
dev_copy = virDomainDeviceDefCopy(dev, vm->def,
driver->xmlopt, priv->qemuCaps);
if (!dev_copy)
goto cleanup; goto cleanup;
} }
@ -7904,7 +7898,7 @@ qemuDomainDetachDeviceLiveAndConfig(virQEMUDriver *driver,
if (!vmdef) if (!vmdef)
goto cleanup; goto cleanup;
if (qemuDomainDetachDeviceConfig(vmdef, dev_copy, priv->qemuCaps, if (qemuDomainDetachDeviceConfig(vmdef, dev_config, priv->qemuCaps,
parse_flags, parse_flags,
driver->xmlopt) < 0) driver->xmlopt) < 0)
goto cleanup; goto cleanup;
@ -7913,7 +7907,7 @@ qemuDomainDetachDeviceLiveAndConfig(virQEMUDriver *driver,
if (flags & VIR_DOMAIN_AFFECT_LIVE) { if (flags & VIR_DOMAIN_AFFECT_LIVE) {
int rc; int rc;
if ((rc = qemuDomainDetachDeviceLive(vm, dev, driver, false)) < 0) if ((rc = qemuDomainDetachDeviceLive(vm, dev_live, driver, false)) < 0)
goto cleanup; goto cleanup;
if (rc == 0 && qemuDomainUpdateDeviceList(vm, VIR_ASYNC_JOB_NONE) < 0) if (rc == 0 && qemuDomainUpdateDeviceList(vm, VIR_ASYNC_JOB_NONE) < 0)
@ -7933,8 +7927,6 @@ qemuDomainDetachDeviceLiveAndConfig(virQEMUDriver *driver,
ret = 0; ret = 0;
cleanup: cleanup:
if (dev != dev_copy)
virDomainDeviceDefFree(dev_copy);
return ret; return ret;
} }