]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Add wrapper for virNodeDeviceFree
authorMichal Privoznik <mprivozn@redhat.com>
Sun, 26 Sep 2021 09:44:30 +0000 (11:44 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 6 Oct 2021 07:26:25 +0000 (09:26 +0200)
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>
build-aux/syntax-check.mk
tools/virsh-completer-nodedev.c
tools/virsh-nodedev.c
tools/virsh-util.c
tools/virsh-util.h

index 0c0d844d6c22e4b989657d2a1832950140b5f84a..6c230826bd810bfbf6efe025d083c63f53ea8349 100644 (file)
@@ -868,7 +868,7 @@ sc_gettext_init:
          $(_sc_search_regexp)
 
 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]$$' \
        exclude='sc_prohibit_obj_free_apis_in_virsh' \
        halt='avoid using public virXXXFree in virsh, use virsh-prefixed wrappers instead' \
index d595b687fd5dcc7e375ba2c056b79d278553f984..d10bf2b78cf1d6bfe16e225a8e834fc7dff8f044 100644 (file)
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #include "virsh-completer-nodedev.h"
+#include "virsh-util.h"
 #include "conf/node_device_conf.h"
 #include "viralloc.h"
 #include "virsh-nodedev.h"
@@ -58,7 +59,7 @@ virshNodeDeviceNameCompleter(vshControl *ctl,
     ret = g_steal_pointer(&tmp);
 
     for (i = 0; i < ndevs; i++)
-        virNodeDeviceFree(devs[i]);
+        virshNodeDeviceFree(devs[i]);
     g_free(devs);
     return ret;
 }
index f72359121f7183a0ec345bbfe88f1804b677df4c..c989a77ad21ac3e65a57c81afe0942566b375d2d 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <config.h>
 #include "virsh-nodedev.h"
+#include "virsh-util.h"
 
 #include "internal.h"
 #include "viralloc.h"
@@ -55,7 +56,7 @@ static const vshCmdOptDef opts_node_device_create[] = {
 static bool
 cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     const char *from = NULL;
     g_autofree char *buffer = NULL;
     virshControl *priv = ctl->privData;
@@ -73,7 +74,6 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
 
     vshPrintExtra(ctl, _("Node device %s created from %s\n"),
                   virNodeDeviceGetName(dev), from);
-    virNodeDeviceFree(dev);
     return true;
 }
 
@@ -140,7 +140,7 @@ vshFindNodeDevice(vshControl *ctl, const char *value)
 static bool
 cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     const char *device_value = NULL;
 
@@ -160,8 +160,6 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -207,8 +205,7 @@ virshNodeDeviceListFree(struct virshNodeDeviceList *list)
 
     if (list && list->devices) {
         for (i = 0; i < list->ndevices; i++) {
-            if (list->devices[i])
-                virNodeDeviceFree(list->devices[i]);
+            virshNodeDeviceFree(list->devices[i]);
         }
         g_free(list->devices);
     }
@@ -327,8 +324,7 @@ virshNodeDeviceListCollect(vshControl *ctl,
 
  remove_entry:
         /* the device has to be removed as it failed one of the filters */
-        virNodeDeviceFree(list->devices[i]);
-        list->devices[i] = NULL;
+        g_clear_pointer(&list->devices[i], virshNodeDeviceFree);
         deleted++;
     }
 
@@ -576,7 +572,7 @@ static const vshCmdOptDef opts_node_device_dumpxml[] = {
 static bool
 cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr device = NULL;
+    g_autoptr(virshNodeDevice) device = NULL;
     g_autofree char *xml = NULL;
     const char *device_value = NULL;
     bool ret = false;
@@ -596,8 +592,6 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (device)
-        virNodeDeviceFree(device);
     return ret;
 }
 
@@ -634,7 +628,7 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
     const char *driverName = NULL;
-    virNodeDevicePtr device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -664,7 +658,6 @@ cmdNodeDeviceDetach(vshControl *ctl, const vshCmd *cmd)
     else
         vshError(ctl, _("Failed to detach device %s"), name);
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -696,7 +689,7 @@ static bool
 cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
-    virNodeDevicePtr device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -715,7 +708,6 @@ cmdNodeDeviceReAttach(vshControl *ctl, const vshCmd *cmd)
         ret = false;
     }
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -747,7 +739,7 @@ static bool
 cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
-    virNodeDevicePtr device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -766,7 +758,6 @@ cmdNodeDeviceReset(vshControl *ctl, const vshCmd *cmd)
         ret = false;
     }
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -910,7 +901,7 @@ static const vshCmdOptDef opts_node_device_event[] = {
 static bool
 cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     int eventId = -1;
     int timeout = 0;
@@ -988,8 +979,6 @@ cmdNodeDeviceEvent(vshControl *ctl, const vshCmd *cmd)
     if (eventId >= 0 &&
         virConnectNodeDeviceEventDeregisterAny(priv->conn, eventId) < 0)
         ret = false;
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -1020,7 +1009,7 @@ static const vshCmdOptDef opts_node_device_undefine[] = {
 static bool
 cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     const char *device_value = NULL;
 
@@ -1041,8 +1030,6 @@ cmdNodeDeviceUndefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 
     ret = true;
  cleanup:
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -1071,7 +1058,7 @@ static const vshCmdOptDef opts_node_device_define[] = {
 static bool
 cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     const char *from = NULL;
     g_autofree char *buffer = NULL;
     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"),
                   virNodeDeviceGetName(dev), from);
-    virNodeDeviceFree(dev);
     return true;
 }
 
@@ -1121,7 +1107,7 @@ static bool
 cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd)
 {
     const char *name = NULL;
-    virNodeDevice *device;
+    g_autoptr(virshNodeDevice) device = NULL;
     bool ret = true;
     virshControl *priv = ctl->privData;
 
@@ -1140,7 +1126,6 @@ cmdNodeDeviceStart(vshControl *ctl, const vshCmd *cmd)
         ret = false;
     }
 
-    virNodeDeviceFree(device);
     return ret;
 }
 
@@ -1175,7 +1160,7 @@ static const vshCmdOptDef opts_node_device_autostart[] = {
 static bool
 cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevice *dev = NULL;
+    g_autoptr(virshNodeDevice) dev = NULL;
     bool ret = false;
     const char *name = NULL;
     int autostart;
@@ -1204,8 +1189,6 @@ cmdNodeDeviceAutostart(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (dev)
-        virNodeDeviceFree(dev);
     return ret;
 }
 
@@ -1237,7 +1220,7 @@ static const vshCmdOptDef opts_node_device_info[] = {
 static bool
 cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
 {
-    virNodeDevicePtr device = NULL;
+    g_autoptr(virshNodeDevice) device = NULL;
     const char *device_value = NULL;
     bool ret = false;
     int autostart;
@@ -1265,8 +1248,6 @@ cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    if (device)
-        virNodeDeviceFree(device);
     return ret;
 }
 
index f7b649983e4c34f5928e687bc01857cbb5fa5d8c..5034f4773f658240c3fa588ae442e66149677377 100644 (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
 virshSecretFree(virSecretPtr secret)
 {
index e8df0a66185fb42c917caf814d51a99652cbc8eb..06e311b21a216596e60539642fbe2e8002884703 100644 (file)
@@ -64,6 +64,11 @@ void
 virshNetworkFree(virNetworkPtr network);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNetwork, virshNetworkFree);
 
+typedef virNodeDevice virshNodeDevice;
+void
+virshNodeDeviceFree(virNodeDevicePtr device);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshNodeDevice, virshNodeDeviceFree);
+
 typedef virSecret virshSecret;
 void
 virshSecretFree(virSecretPtr secret);