virsh: Refactor cleanup in 'cmdVolClone'

Automatically free 'newxml' and remove the 'cleanup' label and 'ret'
variable.

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-30 16:40:14 +01:00
parent e575bf082e
commit 3584f78d4b

View File

@ -570,12 +570,11 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd)
g_autoptr(virshStorageVol) newvol = NULL; g_autoptr(virshStorageVol) newvol = NULL;
const char *name = NULL; const char *name = NULL;
g_autofree char *origxml = NULL; g_autofree char *origxml = NULL;
xmlChar *newxml = NULL; g_autofree xmlChar *newxml = NULL;
bool ret = false;
unsigned int flags = 0; unsigned int flags = 0;
if (!(origvol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) if (!(origvol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
goto cleanup; return false;
if (vshCommandOptBool(cmd, "prealloc-metadata")) if (vshCommandOptBool(cmd, "prealloc-metadata"))
flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA; flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
@ -586,38 +585,30 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd)
origpool = virStoragePoolLookupByVolume(origvol); origpool = virStoragePoolLookupByVolume(origvol);
if (!origpool) { if (!origpool) {
vshError(ctl, "%s", _("failed to get parent pool")); vshError(ctl, "%s", _("failed to get parent pool"));
goto cleanup; return false;
} }
if (vshCommandOptStringReq(ctl, cmd, "newname", &name) < 0) if (vshCommandOptStringReq(ctl, cmd, "newname", &name) < 0)
goto cleanup; return false;
origxml = virStorageVolGetXMLDesc(origvol, 0); if (!(origxml = virStorageVolGetXMLDesc(origvol, 0)))
if (!origxml) return false;
goto cleanup;
newxml = virshMakeCloneXML(origxml, name); if (!(newxml = virshMakeCloneXML(origxml, name))) {
if (!newxml) {
vshError(ctl, "%s", _("Failed to allocate XML buffer")); vshError(ctl, "%s", _("Failed to allocate XML buffer"));
goto cleanup; return false;
} }
newvol = virStorageVolCreateXMLFrom(origpool, (char *) newxml, origvol, flags); if (!(newvol = virStorageVolCreateXMLFrom(origpool, (char *) newxml,
origvol, flags))) {
if (newvol != NULL) {
vshPrintExtra(ctl, _("Vol %s cloned from %s\n"),
virStorageVolGetName(newvol), virStorageVolGetName(origvol));
} else {
vshError(ctl, _("Failed to clone vol from %s"), vshError(ctl, _("Failed to clone vol from %s"),
virStorageVolGetName(origvol)); virStorageVolGetName(origvol));
goto cleanup; return false;
} }
ret = true; vshPrintExtra(ctl, _("Vol %s cloned from %s\n"),
virStorageVolGetName(newvol), virStorageVolGetName(origvol));
cleanup: return true;
xmlFree(newxml);
return ret;
} }
/* /*