mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-18 10:35:20 +00:00
tools: Tiny clean-ups for two functions in virsh-completer.c
These two functions were duplicating some cleanup paths, so let's just merge both cleanup and error paths together. To distinguish whether we need to clean-up the return value let's keep it in @tmp until the function is successful in which case we set @ret to the value of @tmp and set @tmp to NULL. Signed-off-by: Martin Kletzander <mkletzan@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
9e1a808e91
commit
08e190fdda
@ -98,6 +98,7 @@ virshDomainInterfaceCompleter(vshControl *ctl,
|
|||||||
size_t i;
|
size_t i;
|
||||||
unsigned int domainXMLFlags = 0;
|
unsigned int domainXMLFlags = 0;
|
||||||
char **ret = NULL;
|
char **ret = NULL;
|
||||||
|
char **tmp = NULL;
|
||||||
|
|
||||||
virCheckFlags(VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, NULL);
|
virCheckFlags(VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, NULL);
|
||||||
|
|
||||||
@ -108,39 +109,35 @@ virshDomainInterfaceCompleter(vshControl *ctl,
|
|||||||
domainXMLFlags = VIR_DOMAIN_XML_INACTIVE;
|
domainXMLFlags = VIR_DOMAIN_XML_INACTIVE;
|
||||||
|
|
||||||
if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0)
|
if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0)
|
||||||
goto error;
|
goto cleanup;
|
||||||
|
|
||||||
ninterfaces = virXPathNodeSet("./devices/interface", ctxt, &interfaces);
|
ninterfaces = virXPathNodeSet("./devices/interface", ctxt, &interfaces);
|
||||||
if (ninterfaces < 0)
|
if (ninterfaces < 0)
|
||||||
goto error;
|
goto cleanup;
|
||||||
|
|
||||||
if (VIR_ALLOC_N(ret, ninterfaces + 1) < 0)
|
if (VIR_ALLOC_N(tmp, ninterfaces + 1) < 0)
|
||||||
goto error;
|
goto cleanup;
|
||||||
|
|
||||||
for (i = 0; i < ninterfaces; i++) {
|
for (i = 0; i < ninterfaces; i++) {
|
||||||
ctxt->node = interfaces[i];
|
ctxt->node = interfaces[i];
|
||||||
|
|
||||||
if (!(flags & VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC) &&
|
if (!(flags & VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC) &&
|
||||||
(ret[i] = virXPathString("string(./target/@dev)", ctxt)))
|
(tmp[i] = virXPathString("string(./target/@dev)", ctxt)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* In case we are dealing with inactive domain XML there's no
|
/* In case we are dealing with inactive domain XML there's no
|
||||||
* <target dev=''/>. Offer MAC addresses then. */
|
* <target dev=''/>. Offer MAC addresses then. */
|
||||||
if (!(ret[i] = virXPathString("string(./mac/@address)", ctxt)))
|
if (!(tmp[i] = virXPathString("string(./mac/@address)", ctxt)))
|
||||||
goto error;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VIR_STEAL_PTR(ret, tmp);
|
||||||
|
cleanup:
|
||||||
VIR_FREE(interfaces);
|
VIR_FREE(interfaces);
|
||||||
xmlFreeDoc(xmldoc);
|
xmlFreeDoc(xmldoc);
|
||||||
xmlXPathFreeContext(ctxt);
|
xmlXPathFreeContext(ctxt);
|
||||||
|
virStringListFree(tmp);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
error:
|
|
||||||
VIR_FREE(interfaces);
|
|
||||||
xmlFreeDoc(xmldoc);
|
|
||||||
xmlXPathFreeContext(ctxt);
|
|
||||||
virStringListFree(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -155,6 +152,7 @@ virshDomainDiskTargetCompleter(vshControl *ctl,
|
|||||||
xmlNodePtr *disks = NULL;
|
xmlNodePtr *disks = NULL;
|
||||||
int ndisks;
|
int ndisks;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
char **tmp = NULL;
|
||||||
char **ret = NULL;
|
char **ret = NULL;
|
||||||
|
|
||||||
virCheckFlags(0, NULL);
|
virCheckFlags(0, NULL);
|
||||||
@ -163,32 +161,28 @@ virshDomainDiskTargetCompleter(vshControl *ctl,
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0)
|
if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0)
|
||||||
goto error;
|
goto cleanup;
|
||||||
|
|
||||||
ndisks = virXPathNodeSet("./devices/disk", ctxt, &disks);
|
ndisks = virXPathNodeSet("./devices/disk", ctxt, &disks);
|
||||||
if (ndisks < 0)
|
if (ndisks < 0)
|
||||||
goto error;
|
goto cleanup;
|
||||||
|
|
||||||
if (VIR_ALLOC_N(ret, ndisks + 1) < 0)
|
if (VIR_ALLOC_N(tmp, ndisks + 1) < 0)
|
||||||
goto error;
|
goto cleanup;
|
||||||
|
|
||||||
for (i = 0; i < ndisks; i++) {
|
for (i = 0; i < ndisks; i++) {
|
||||||
ctxt->node = disks[i];
|
ctxt->node = disks[i];
|
||||||
if (!(ret[i] = virXPathString("string(./target/@dev)", ctxt)))
|
if (!(tmp[i] = virXPathString("string(./target/@dev)", ctxt)))
|
||||||
goto error;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VIR_STEAL_PTR(ret, tmp);
|
||||||
|
cleanup:
|
||||||
VIR_FREE(disks);
|
VIR_FREE(disks);
|
||||||
xmlFreeDoc(xmldoc);
|
xmlFreeDoc(xmldoc);
|
||||||
xmlXPathFreeContext(ctxt);
|
xmlXPathFreeContext(ctxt);
|
||||||
|
virStringListFree(tmp);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
error:
|
|
||||||
VIR_FREE(disks);
|
|
||||||
xmlFreeDoc(xmldoc);
|
|
||||||
xmlXPathFreeContext(ctxt);
|
|
||||||
virStringListFree(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user