From d39bd3998e7f29b803bddedc35ce50ef208e14b9 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Sun, 26 Sep 2021 13:18:17 +0200 Subject: [PATCH] virsh: Add wrapper for virNWFilterFree 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-nwfilter.c | 3 ++- tools/virsh-nwfilter.c | 22 +++++++--------------- tools/virsh-util.c | 11 +++++++++++ tools/virsh-util.h | 5 +++++ 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 6c230826bd..5daf5afcd0 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|Network|NodeDevice|Secret|StoragePool|StorageVol)Free\b' \ + @prohibit='\bvir(Domain|DomainSnapshot|Interface|Network|NodeDevice|NWFilter|Secret|StoragePool|StorageVol)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-nwfilter.c b/tools/virsh-completer-nwfilter.c index 859f72f6e9..9850dbba5d 100644 --- a/tools/virsh-completer-nwfilter.c +++ b/tools/virsh-completer-nwfilter.c @@ -21,6 +21,7 @@ #include #include "virsh-completer-nwfilter.h" +#include "virsh-util.h" #include "viralloc.h" #include "virsh.h" #include "virstring.h" @@ -56,7 +57,7 @@ virshNWFilterNameCompleter(vshControl *ctl, ret = g_steal_pointer(&tmp); for (i = 0; i < nnwfilters; i++) - virNWFilterFree(nwfilters[i]); + virshNWFilterFree(nwfilters[i]); g_free(nwfilters); return ret; } diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c index 33164f623f..09ceaf6ec9 100644 --- a/tools/virsh-nwfilter.c +++ b/tools/virsh-nwfilter.c @@ -20,6 +20,7 @@ #include #include "virsh-nwfilter.h" +#include "virsh-util.h" #include "internal.h" #include "viralloc.h" @@ -91,7 +92,7 @@ static const vshCmdOptDef opts_nwfilter_define[] = { static bool cmdNWFilterDefine(vshControl *ctl, const vshCmd *cmd) { - virNWFilterPtr nwfilter; + g_autoptr(virshNWFilter) nwfilter = NULL; const char *from = NULL; bool ret = true; g_autofree char *buffer = NULL; @@ -115,7 +116,6 @@ cmdNWFilterDefine(vshControl *ctl, const vshCmd *cmd) if (nwfilter != NULL) { vshPrintExtra(ctl, _("Network filter %s defined from %s\n"), virNWFilterGetName(nwfilter), from); - virNWFilterFree(nwfilter); } else { vshError(ctl, _("Failed to define network filter from %s"), from); ret = false; @@ -149,7 +149,7 @@ static const vshCmdOptDef opts_nwfilter_undefine[] = { static bool cmdNWFilterUndefine(vshControl *ctl, const vshCmd *cmd) { - virNWFilterPtr nwfilter; + g_autoptr(virshNWFilter) nwfilter = NULL; bool ret = true; const char *name; @@ -163,7 +163,6 @@ cmdNWFilterUndefine(vshControl *ctl, const vshCmd *cmd) ret = false; } - virNWFilterFree(nwfilter); return ret; } @@ -193,7 +192,7 @@ static const vshCmdOptDef opts_nwfilter_dumpxml[] = { static bool cmdNWFilterDumpXML(vshControl *ctl, const vshCmd *cmd) { - virNWFilterPtr nwfilter; + g_autoptr(virshNWFilter) nwfilter = NULL; bool ret = true; g_autofree char *dump = NULL; @@ -207,7 +206,6 @@ cmdNWFilterDumpXML(vshControl *ctl, const vshCmd *cmd) ret = false; } - virNWFilterFree(nwfilter); return ret; } @@ -239,8 +237,7 @@ virshNWFilterListFree(struct virshNWFilterList *list) if (list && list->filters) { for (i = 0; i < list->nfilters; i++) { - if (list->filters[i]) - virNWFilterFree(list->filters[i]); + virshNWFilterFree(list->filters[i]); } g_free(list->filters); } @@ -418,8 +415,8 @@ static bool cmdNWFilterEdit(vshControl *ctl, const vshCmd *cmd) { bool ret = false; - virNWFilterPtr nwfilter = NULL; - virNWFilterPtr nwfilter_edited = NULL; + g_autoptr(virshNWFilter) nwfilter = NULL; + g_autoptr(virshNWFilter) nwfilter_edited = NULL; virshControl *priv = ctl->privData; nwfilter = virshCommandOptNWFilter(ctl, cmd, NULL); @@ -445,11 +442,6 @@ cmdNWFilterEdit(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - if (nwfilter) - virNWFilterFree(nwfilter); - if (nwfilter_edited) - virNWFilterFree(nwfilter_edited); - return ret; } diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 5034f4773f..fc2839d8fc 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -318,6 +318,17 @@ virshNodeDeviceFree(virNodeDevicePtr device) } +void +virshNWFilterFree(virNWFilterPtr nwfilter) +{ + if (!nwfilter) + return; + + vshSaveLibvirtHelperError(); + virNWFilterFree(nwfilter); /* sc_prohibit_obj_free_apis_in_virsh */ +} + + void virshSecretFree(virSecretPtr secret) { diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 06e311b21a..065055ddb1 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -69,6 +69,11 @@ void virshNodeDeviceFree(virNodeDevicePtr device); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNodeDevice, virshNodeDeviceFree); +typedef virNWFilter virshNWFilter; +void +virshNWFilterFree(virNWFilterPtr nwfilter); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNWFilter, virshNWFilterFree); + typedef virSecret virshSecret; void virshSecretFree(virSecretPtr secret);