virsh: Add wrapper for virNodeDeviceFree

Similarly to virshDomainFree add a wrapper for the snapshot object
freeing function.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Michal Privoznik 2021-09-26 11:44:30 +02:00
parent db4df06533
commit 40a3a5b608
5 changed files with 34 additions and 36 deletions

View File

@ -868,7 +868,7 @@ sc_gettext_init:
$(_sc_search_regexp) $(_sc_search_regexp)
sc_prohibit_obj_free_apis_in_virsh: sc_prohibit_obj_free_apis_in_virsh:
@prohibit='\bvir(Domain|DomainSnapshot|Interface|Network|Secret|StoragePool|StorageVol)Free\b' \ @prohibit='\bvir(Domain|DomainSnapshot|Interface|Network|NodeDevice|Secret|StoragePool|StorageVol)Free\b' \
in_vc_files='virsh.*\.[ch]$$' \ in_vc_files='virsh.*\.[ch]$$' \
exclude='sc_prohibit_obj_free_apis_in_virsh' \ exclude='sc_prohibit_obj_free_apis_in_virsh' \
halt='avoid using public virXXXFree in virsh, use virsh-prefixed wrappers instead' \ halt='avoid using public virXXXFree in virsh, use virsh-prefixed wrappers instead' \

View File

@ -21,6 +21,7 @@
#include <config.h> #include <config.h>
#include "virsh-completer-nodedev.h" #include "virsh-completer-nodedev.h"
#include "virsh-util.h"
#include "conf/node_device_conf.h" #include "conf/node_device_conf.h"
#include "viralloc.h" #include "viralloc.h"
#include "virsh-nodedev.h" #include "virsh-nodedev.h"
@ -58,7 +59,7 @@ virshNodeDeviceNameCompleter(vshControl *ctl,
ret = g_steal_pointer(&tmp); ret = g_steal_pointer(&tmp);
for (i = 0; i < ndevs; i++) for (i = 0; i < ndevs; i++)
virNodeDeviceFree(devs[i]); virshNodeDeviceFree(devs[i]);
g_free(devs); g_free(devs);
return ret; return ret;
} }

View File

@ -20,6 +20,7 @@
#include <config.h> #include <config.h>
#include "virsh-nodedev.h" #include "virsh-nodedev.h"
#include "virsh-util.h"
#include "internal.h" #include "internal.h"
#include "viralloc.h" #include "viralloc.h"
@ -55,7 +56,7 @@ static const vshCmdOptDef opts_node_device_create[] = {
static bool static bool
cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevicePtr dev = NULL; g_autoptr(virshNodeDevice) dev = NULL;
const char *from = NULL; const char *from = NULL;
g_autofree char *buffer = NULL; g_autofree char *buffer = NULL;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
@ -73,7 +74,6 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
vshPrintExtra(ctl, _("Node device %s created from %s\n"), vshPrintExtra(ctl, _("Node device %s created from %s\n"),
virNodeDeviceGetName(dev), from); virNodeDeviceGetName(dev), from);
virNodeDeviceFree(dev);
return true; return true;
} }
@ -140,7 +140,7 @@ vshFindNodeDevice(vshControl *ctl, const char *value)
static bool static bool
cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevice *dev = NULL; g_autoptr(virshNodeDevice) dev = NULL;
bool ret = false; bool ret = false;
const char *device_value = NULL; const char *device_value = NULL;
@ -160,8 +160,6 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
if (dev)
virNodeDeviceFree(dev);
return ret; return ret;
} }
@ -207,8 +205,7 @@ virshNodeDeviceListFree(struct virshNodeDeviceList *list)
if (list && list->devices) { if (list && list->devices) {
for (i = 0; i < list->ndevices; i++) { for (i = 0; i < list->ndevices; i++) {
if (list->devices[i]) virshNodeDeviceFree(list->devices[i]);
virNodeDeviceFree(list->devices[i]);
} }
g_free(list->devices); g_free(list->devices);
} }
@ -327,8 +324,7 @@ virshNodeDeviceListCollect(vshControl *ctl,
remove_entry: remove_entry:
/* the device has to be removed as it failed one of the filters */ /* the device has to be removed as it failed one of the filters */
virNodeDeviceFree(list->devices[i]); g_clear_pointer(&list->devices[i], virshNodeDeviceFree);
list->devices[i] = NULL;
deleted++; deleted++;
} }
@ -576,7 +572,7 @@ static const vshCmdOptDef opts_node_device_dumpxml[] = {
static bool static bool
cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevicePtr device = NULL; g_autoptr(virshNodeDevice) device = NULL;
g_autofree char *xml = NULL; g_autofree char *xml = NULL;
const char *device_value = NULL; const char *device_value = NULL;
bool ret = false; bool ret = false;
@ -596,8 +592,6 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
if (device)
virNodeDeviceFree(device);
return ret; return ret;
} }
@ -634,7 +628,7 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
{ {
const char *name = NULL; const char *name = NULL;
const char *driverName = NULL; const char *driverName = NULL;
virNodeDevicePtr device; g_autoptr(virshNodeDevice) device = NULL;
bool ret = true; bool ret = true;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
@ -664,7 +658,6 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
else else
vshError(ctl, _("Failed to detach device %s"), name); vshError(ctl, _("Failed to detach device %s"), name);
virNodeDeviceFree(device);
return ret; return ret;
} }
@ -696,7 +689,7 @@ static bool
cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
{ {
const char *name = NULL; const char *name = NULL;
virNodeDevicePtr device; g_autoptr(virshNodeDevice) device = NULL;
bool ret = true; bool ret = true;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
@ -715,7 +708,6 @@ cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
ret = false; ret = false;
} }
virNodeDeviceFree(device);
return ret; return ret;
} }
@ -747,7 +739,7 @@ static bool
cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
{ {
const char *name = NULL; const char *name = NULL;
virNodeDevicePtr device; g_autoptr(virshNodeDevice) device = NULL;
bool ret = true; bool ret = true;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
@ -766,7 +758,6 @@ cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
ret = false; ret = false;
} }
virNodeDeviceFree(device);
return ret; return ret;
} }
@ -910,7 +901,7 @@ static const vshCmdOptDef opts_node_device_event[] = {
static bool static bool
cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevicePtr dev = NULL; g_autoptr(virshNodeDevice) dev = NULL;
bool ret = false; bool ret = false;
int eventId = -1; int eventId = -1;
int timeout = 0; int timeout = 0;
@ -988,8 +979,6 @@ cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd)
if (eventId >= 0 && if (eventId >= 0 &&
virConnectNodeDeviceEventDeregisterAny(priv->conn, eventId) < 0) virConnectNodeDeviceEventDeregisterAny(priv->conn, eventId) < 0)
ret = false; ret = false;
if (dev)
virNodeDeviceFree(dev);
return ret; return ret;
} }
@ -1020,7 +1009,7 @@ static const vshCmdOptDef opts_node_device_undefine[] = {
static bool static bool
cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
{ {
virNodeDevice *dev = NULL; g_autoptr(virshNodeDevice) dev = NULL;
bool ret = false; bool ret = false;
const char *device_value = NULL; const char *device_value = NULL;
@ -1041,8 +1030,6 @@ cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
ret = true; ret = true;
cleanup: cleanup:
if (dev)
virNodeDeviceFree(dev);
return ret; return ret;
} }
@ -1071,7 +1058,7 @@ static const vshCmdOptDef opts_node_device_define[] = {
static bool static bool
cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
{ {
virNodeDevice *dev = NULL; g_autoptr(virshNodeDevice) dev = NULL;
const char *from = NULL; const char *from = NULL;
g_autofree char *buffer = NULL; g_autofree char *buffer = NULL;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
@ -1089,7 +1076,6 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
vshPrintExtra(ctl, _("Node device '%s' defined from '%s'\n"), vshPrintExtra(ctl, _("Node device '%s' defined from '%s'\n"),
virNodeDeviceGetName(dev), from); virNodeDeviceGetName(dev), from);
virNodeDeviceFree(dev);
return true; return true;
} }
@ -1121,7 +1107,7 @@ static bool
cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd)
{ {
const char *name = NULL; const char *name = NULL;
virNodeDevice *device; g_autoptr(virshNodeDevice) device = NULL;
bool ret = true; bool ret = true;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
@ -1140,7 +1126,6 @@ cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd)
ret = false; ret = false;
} }
virNodeDeviceFree(device);
return ret; return ret;
} }
@ -1175,7 +1160,7 @@ static const vshCmdOptDef opts_node_device_autostart[] = {
static bool static bool
cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevice *dev = NULL; g_autoptr(virshNodeDevice) dev = NULL;
bool ret = false; bool ret = false;
const char *name = NULL; const char *name = NULL;
int autostart; int autostart;
@ -1204,8 +1189,6 @@ cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
if (dev)
virNodeDeviceFree(dev);
return ret; return ret;
} }
@ -1237,7 +1220,7 @@ static const vshCmdOptDef opts_node_device_info[] = {
static bool static bool
cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd) cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
{ {
virNodeDevicePtr device = NULL; g_autoptr(virshNodeDevice) device = NULL;
const char *device_value = NULL; const char *device_value = NULL;
bool ret = false; bool ret = false;
int autostart; int autostart;
@ -1265,8 +1248,6 @@ cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
ret = true; ret = true;
cleanup: cleanup:
if (device)
virNodeDeviceFree(device);
return ret; return ret;
} }

View File

@ -307,6 +307,17 @@ virshNetworkFree(virNetworkPtr network)
} }
void
virshNodeDeviceFree(virNodeDevicePtr device)
{
if (!device)
return;
vshSaveLibvirtHelperError();
virNodeDeviceFree(device); /* sc_prohibit_obj_free_apis_in_virsh */
}
void void
virshSecretFree(virSecretPtr secret) virshSecretFree(virSecretPtr secret)
{ {

View File

@ -64,6 +64,11 @@ void
virshNetworkFree(virNetworkPtr network); virshNetworkFree(virNetworkPtr network);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNetwork, virshNetworkFree); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNetwork, virshNetworkFree);
typedef virNodeDevice virshNodeDevice;
void
virshNodeDeviceFree(virNodeDevicePtr device);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNodeDevice, virshNodeDeviceFree);
typedef virSecret virshSecret; typedef virSecret virshSecret;
void void
virshSecretFree(virSecretPtr secret); virshSecretFree(virSecretPtr secret);