From 663a29f42e4a3fb80f9527621d7a8e9d1d277bbc Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Sun, 26 Sep 2021 09:25:41 +0200 Subject: [PATCH] virsh: Add wrapper for virStoragePoolFree Similarly to virshDomainFree add a wrapper for the snapshot object freeing function. Signed-off-by: Michal Privoznik Acked-by: Jonathon Jongsma --- build-aux/syntax-check.mk | 2 +- tools/virsh-completer-pool.c | 3 +- tools/virsh-completer-volume.c | 4 +- tools/virsh-domain.c | 3 +- tools/virsh-pool.c | 67 +++++++++++----------------------- tools/virsh-util.c | 11 ++++++ tools/virsh-util.h | 5 +++ tools/virsh-volume.c | 29 ++++----------- 8 files changed, 51 insertions(+), 73 deletions(-) diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 84cb895d86..111d2109e8 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -868,7 +868,7 @@ sc_gettext_init: $(_sc_search_regexp) sc_prohibit_obj_free_apis_in_virsh: - @prohibit='\bvir(Domain|DomainSnapshot|Interface|Secret)Free\b' \ + @prohibit='\bvir(Domain|DomainSnapshot|Interface|Secret|StoragePool)Free\b' \ in_vc_files='virsh.*\.[ch]$$' \ exclude='sc_prohibit_obj_free_apis_in_virsh' \ halt='avoid using public virXXXFree in virsh, use virsh-prefixed wrappers instead' \ diff --git a/tools/virsh-completer-pool.c b/tools/virsh-completer-pool.c index 9350eff2d3..84e9d6cc5a 100644 --- a/tools/virsh-completer-pool.c +++ b/tools/virsh-completer-pool.c @@ -21,6 +21,7 @@ #include #include "virsh-completer-pool.h" +#include "virsh-util.h" #include "conf/storage_conf.h" #include "viralloc.h" #include "virsh-pool.h" @@ -61,7 +62,7 @@ virshStoragePoolNameCompleter(vshControl *ctl, ret = g_steal_pointer(&tmp); for (i = 0; i < npools; i++) - virStoragePoolFree(pools[i]); + virshStoragePoolFree(pools[i]); g_free(pools); return ret; } diff --git a/tools/virsh-completer-volume.c b/tools/virsh-completer-volume.c index fcbc28b13b..1d83643c69 100644 --- a/tools/virsh-completer-volume.c +++ b/tools/virsh-completer-volume.c @@ -21,6 +21,7 @@ #include #include "virsh-completer-volume.h" +#include "virsh-util.h" #include "viralloc.h" #include "virsh-pool.h" #include "virsh.h" @@ -32,7 +33,7 @@ virshStorageVolNameCompleter(vshControl *ctl, unsigned int flags) { virshControl *priv = ctl->privData; - virStoragePoolPtr pool = NULL; + g_autoptr(virshStoragePool) pool = NULL; virStorageVolPtr *vols = NULL; int rc; int nvols = 0; @@ -63,7 +64,6 @@ virshStorageVolNameCompleter(vshControl *ctl, ret = g_steal_pointer(&tmp); cleanup: - virStoragePoolFree(pool); for (i = 0; i < nvols; i++) virStorageVolFree(vols[i]); g_free(vols); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 0391bdeb30..c7bf4d0015 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -3801,7 +3801,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd) } if (pool) { - virStoragePoolPtr storagepool = NULL; + g_autoptr(virshStoragePool) storagepool = NULL; if (!source) { vshError(ctl, @@ -3820,7 +3820,6 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd) } vol.vol = virStorageVolLookupByName(storagepool, source); - virStoragePoolFree(storagepool); } else { vol.vol = virStorageVolLookupByPath(priv->conn, source); diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c index fd9d5ead63..d391257f6e 100644 --- a/tools/virsh-pool.c +++ b/tools/virsh-pool.c @@ -20,6 +20,7 @@ #include #include "virsh-pool.h" +#include "virsh-util.h" #include "internal.h" #include "virbuffer.h" @@ -219,7 +220,7 @@ static const vshCmdOptDef opts_pool_autostart[] = { static bool cmdPoolAutostart(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; const char *name; int autostart; @@ -233,7 +234,6 @@ cmdPoolAutostart(vshControl *ctl, const vshCmd *cmd) vshError(ctl, _("failed to mark pool %s as autostarted"), name); else vshError(ctl, _("failed to unmark pool %s as autostarted"), name); - virStoragePoolFree(pool); return false; } @@ -242,7 +242,6 @@ cmdPoolAutostart(vshControl *ctl, const vshCmd *cmd) else vshPrintExtra(ctl, _("Pool %s unmarked as autostarted\n"), name); - virStoragePoolFree(pool); return true; } @@ -271,7 +270,7 @@ static const vshCmdOptDef opts_pool_create[] = { static bool cmdPoolCreate(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; const char *from = NULL; g_autofree char *buffer = NULL; bool build; @@ -307,7 +306,6 @@ cmdPoolCreate(vshControl *ctl, const vshCmd *cmd) vshPrintExtra(ctl, _("Pool %s created from %s\n"), virStoragePoolGetName(pool), from); - virStoragePoolFree(pool); return true; } @@ -462,7 +460,7 @@ static const vshCmdOptDef opts_pool_create_as[] = { static bool cmdPoolCreateAs(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; const char *name; g_autofree char *xml = NULL; bool printXML = vshCommandOptBool(cmd, "print-xml"); @@ -500,7 +498,6 @@ cmdPoolCreateAs(vshControl *ctl, const vshCmd *cmd) } vshPrintExtra(ctl, _("Pool %s created\n"), name); - virStoragePoolFree(pool); return true; } @@ -530,7 +527,7 @@ static const vshCmdOptDef opts_pool_define[] = { static bool cmdPoolDefine(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; const char *from = NULL; g_autofree char *buffer = NULL; unsigned int flags = 0; @@ -552,7 +549,6 @@ cmdPoolDefine(vshControl *ctl, const vshCmd *cmd) vshPrintExtra(ctl, _("Pool %s defined from %s\n"), virStoragePoolGetName(pool), from); - virStoragePoolFree(pool); return true; } @@ -572,7 +568,7 @@ static const vshCmdInfo info_pool_define_as[] = { static bool cmdPoolDefineAs(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; const char *name; g_autofree char *xml = NULL; bool printXML = vshCommandOptBool(cmd, "print-xml"); @@ -592,7 +588,6 @@ cmdPoolDefineAs(vshControl *ctl, const vshCmd *cmd) } vshPrintExtra(ctl, _("Pool %s defined\n"), name); - virStoragePoolFree(pool); return true; } @@ -620,7 +615,7 @@ static const vshCmdOptDef opts_pool_build[] = { static bool cmdPoolBuild(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; const char *name; unsigned int flags = 0; @@ -641,8 +636,6 @@ cmdPoolBuild(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); - return ret; } @@ -668,7 +661,7 @@ static const vshCmdOptDef opts_pool_destroy[] = { static bool cmdPoolDestroy(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; const char *name; @@ -682,7 +675,6 @@ cmdPoolDestroy(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); return ret; } @@ -708,7 +700,7 @@ static const vshCmdOptDef opts_pool_delete[] = { static bool cmdPoolDelete(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; const char *name; @@ -722,7 +714,6 @@ cmdPoolDelete(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); return ret; } @@ -748,7 +739,7 @@ static const vshCmdOptDef opts_pool_refresh[] = { static bool cmdPoolRefresh(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; const char *name; @@ -761,7 +752,6 @@ cmdPoolRefresh(vshControl *ctl, const vshCmd *cmd) vshError(ctl, _("Failed to refresh pool %s"), name); ret = false; } - virStoragePoolFree(pool); return ret; } @@ -792,7 +782,7 @@ static const vshCmdOptDef opts_pool_dumpxml[] = { static bool cmdPoolDumpXML(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; bool inactive = vshCommandOptBool(cmd, "inactive"); unsigned int flags = 0; @@ -811,7 +801,6 @@ cmdPoolDumpXML(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); return ret; } @@ -837,8 +826,7 @@ void virshStoragePoolListFree(struct virshStoragePoolList *list) if (list && list->pools) { for (i = 0; i < list->npools; i++) { - if (list->pools[i]) - virStoragePoolFree(list->pools[i]); + virshStoragePoolFree(list->pools[i]); } g_free(list->pools); } @@ -1003,8 +991,7 @@ virshStoragePoolListCollect(vshControl *ctl, remove_entry: /* the pool has to be removed as it failed one of the filters */ - virStoragePoolFree(list->pools[i]); - list->pools[i] = NULL; + g_clear_pointer(&list->pools[i], virshStoragePoolFree); deleted++; } @@ -1570,7 +1557,7 @@ static bool cmdPoolInfo(vshControl *ctl, const vshCmd *cmd) { virStoragePoolInfo info; - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; int autostart = 0; bool ret = true; bool bytes = false; @@ -1630,7 +1617,6 @@ cmdPoolInfo(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); return ret; } @@ -1656,13 +1642,12 @@ static const vshCmdOptDef opts_pool_name[] = { static bool cmdPoolName(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; if (!(pool = virshCommandOptPoolBy(ctl, cmd, "pool", NULL, VIRSH_BYUUID))) return false; vshPrint(ctl, "%s\n", virStoragePoolGetName(pool)); - virStoragePoolFree(pool); return true; } @@ -1691,7 +1676,7 @@ static const vshCmdOptDef opts_pool_start[] = { static bool cmdPoolStart(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; const char *name = NULL; bool build; @@ -1723,7 +1708,6 @@ cmdPoolStart(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); return ret; } @@ -1749,7 +1733,7 @@ static const vshCmdOptDef opts_pool_undefine[] = { static bool cmdPoolUndefine(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; bool ret = true; const char *name; @@ -1763,7 +1747,6 @@ cmdPoolUndefine(vshControl *ctl, const vshCmd *cmd) ret = false; } - virStoragePoolFree(pool); return ret; } @@ -1789,7 +1772,7 @@ static const vshCmdOptDef opts_pool_uuid[] = { static bool cmdPoolUuid(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; char uuid[VIR_UUID_STRING_BUFLEN]; if (!(pool = virshCommandOptPoolBy(ctl, cmd, "pool", NULL, VIRSH_BYNAME))) @@ -1800,7 +1783,6 @@ cmdPoolUuid(vshControl *ctl, const vshCmd *cmd) else vshError(ctl, "%s", _("failed to get pool UUID")); - virStoragePoolFree(pool); return true; } @@ -1827,8 +1809,8 @@ static bool cmdPoolEdit(vshControl *ctl, const vshCmd *cmd) { bool ret = false; - virStoragePoolPtr pool = NULL; - virStoragePoolPtr pool_edited = NULL; + g_autoptr(virshStoragePool) pool = NULL; + g_autoptr(virshStoragePool) pool_edited = NULL; unsigned int flags = VIR_STORAGE_XML_INACTIVE; g_autofree char *tmp_desc = NULL; virshControl *priv = ctl->privData; @@ -1865,11 +1847,6 @@ cmdPoolEdit(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - if (pool) - virStoragePoolFree(pool); - if (pool_edited) - virStoragePoolFree(pool_edited); - return ret; } @@ -2018,7 +1995,7 @@ static const vshCmdOptDef opts_pool_event[] = { static bool cmdPoolEvent(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool = NULL; + g_autoptr(virshStoragePool) pool = NULL; bool ret = false; int eventId = -1; int timeout = 0; @@ -2088,8 +2065,6 @@ cmdPoolEvent(vshControl *ctl, const vshCmd *cmd) if (eventId >= 0 && virConnectStoragePoolEventDeregisterAny(priv->conn, eventId) < 0) ret = false; - if (pool) - virStoragePoolFree(pool); return ret; } diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 82523f2575..d537501387 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -307,6 +307,17 @@ virshSecretFree(virSecretPtr secret) } +void +virshStoragePoolFree(virStoragePoolPtr pool) +{ + if (!pool) + return; + + vshSaveLibvirtHelperError(); + virStoragePoolFree(pool); /* sc_prohibit_obj_free_apis_in_virsh */ +} + + int virshDomainGetXMLFromDom(vshControl *ctl, virDomainPtr dom, diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 7165755550..3ff6f16784 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -64,6 +64,11 @@ void virshSecretFree(virSecretPtr secret); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshSecret, virshSecretFree); +typedef virStoragePool virshStoragePool; +void +virshStoragePoolFree(virStoragePoolPtr pool); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshStoragePool, virshStoragePoolFree); + int virshDomainState(vshControl *ctl, virDomainPtr dom, diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 38bb62a48f..6e8f7721a3 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -83,7 +83,7 @@ virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd, const char **name, unsigned int flags) { virStorageVolPtr vol = NULL; - virStoragePoolPtr pool = NULL; + g_autoptr(virshStoragePool) pool = NULL; const char *n = NULL, *p = NULL; virshControl *priv = ctl->privData; @@ -102,7 +102,6 @@ virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd, if (virStoragePoolIsActive(pool) != 1) { vshError(ctl, _("pool '%s' is not active"), p); - virStoragePoolFree(pool); return NULL; } } @@ -145,7 +144,7 @@ virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd, /* If the pool was specified, then make sure that the returned * volume is from the given pool */ if (pool && vol) { - virStoragePoolPtr volpool = NULL; + g_autoptr(virshStoragePool) volpool = NULL; if ((volpool = virStoragePoolLookupByVolume(vol))) { if (STRNEQ(virStoragePoolGetName(volpool), @@ -157,13 +156,9 @@ virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd, virStorageVolFree(vol); vol = NULL; } - virStoragePoolFree(volpool); } } - if (pool) - virStoragePoolFree(pool); - return vol; } @@ -234,7 +229,7 @@ virshVolSize(const char *data, unsigned long long *val) static bool cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; virStorageVolPtr vol = NULL; g_autofree char *xml = NULL; bool printXML = vshCommandOptBool(cmd, "print-xml"); @@ -373,7 +368,6 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) cleanup: if (vol) virStorageVolFree(vol); - virStoragePoolFree(pool); return ret; } @@ -403,7 +397,7 @@ static const vshCmdOptDef opts_vol_create[] = { static bool cmdVolCreate(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; virStorageVolPtr vol; const char *from = NULL; bool ret = false; @@ -434,7 +428,6 @@ cmdVolCreate(vshControl *ctl, const vshCmd *cmd) } cleanup: - virStoragePoolFree(pool); return ret; } @@ -474,7 +467,7 @@ static const vshCmdOptDef opts_vol_create_from[] = { static bool cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool = NULL; + g_autoptr(virshStoragePool) pool = NULL; virStorageVolPtr newvol = NULL, inputvol = NULL; const char *from = NULL; bool ret = false; @@ -513,8 +506,6 @@ cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - if (pool) - virStoragePoolFree(pool); if (inputvol) virStorageVolFree(inputvol); if (newvol) @@ -582,7 +573,7 @@ static const vshCmdOptDef opts_vol_clone[] = { static bool cmdVolClone(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr origpool = NULL; + g_autoptr(virshStoragePool) origpool = NULL; virStorageVolPtr origvol = NULL, newvol = NULL; const char *name = NULL; g_autofree char *origxml = NULL; @@ -637,8 +628,6 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd) virStorageVolFree(origvol); if (newvol) virStorageVolFree(newvol); - if (origpool) - virStoragePoolFree(origpool); return ret; } @@ -1395,7 +1384,7 @@ static bool cmdVolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) { virStorageVolInfo volumeInfo; - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; const char *unit; double val; bool details = vshCommandOptBool(cmd, "details"); @@ -1521,7 +1510,6 @@ cmdVolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) /* Cleanup remaining memory */ VIR_FREE(volInfoTexts); - virStoragePoolFree(pool); virshStorageVolListFree(list); /* Return the desired value */ @@ -1585,7 +1573,7 @@ static const vshCmdOptDef opts_vol_pool[] = { static bool cmdVolPool(vshControl *ctl, const vshCmd *cmd) { - virStoragePoolPtr pool; + g_autoptr(virshStoragePool) pool = NULL; virStorageVolPtr vol; char uuid[VIR_UUID_STRING_BUFLEN]; @@ -1615,7 +1603,6 @@ cmdVolPool(vshControl *ctl, const vshCmd *cmd) /* Cleanup */ virStorageVolFree(vol); - virStoragePoolFree(pool); return true; }