summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-08-01 09:10:56 +0200
committerThomas Haller <thaller@redhat.com>2019-08-05 10:11:01 +0200
commitb298f2e6058a5453c73a5a19c273b17b5b4b91b1 (patch)
tree4bdb893ea5cdf0398624bdaf651df0f3b4ce8c84
parentcf6cd06422e2de798c53ce3472255228059b0540 (diff)
downloadNetworkManager-b298f2e6058a5453c73a5a19c273b17b5b4b91b1.tar.gz
cli: use cleanup macro for freeing AddAndActivateInfo
We should prefer the cleanup macors nm_auto*() because they express ownership in code. Also, they allow to return early without additional cleanup code. That way we can refactor if-else blocks. Also, in cases where we intentionally pass on the reference, we use g_steal_pointer(), which literally spells out what happens in code.
-rw-r--r--clients/cli/devices.c114
1 files changed, 57 insertions, 57 deletions
diff --git a/clients/cli/devices.c b/clients/cli/devices.c
index 33df2c06a1..8354fcac75 100644
--- a/clients/cli/devices.c
+++ b/clients/cli/devices.c
@@ -1869,16 +1869,19 @@ add_and_activate_info_free (AddAndActivateInfo *info)
nm_g_slice_free (info);
}
+NM_AUTO_DEFINE_FCN0 (AddAndActivateInfo *, _nm_auto_free_add_and_activate_info, add_and_activate_info_free)
+#define nm_auto_free_add_and_activate_info nm_auto (_nm_auto_free_add_and_activate_info)
+
static void
add_and_activate_cb (GObject *client,
GAsyncResult *result,
gpointer user_data)
{
- AddAndActivateInfo *info = user_data;
+ nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data;
NmCli *nmc = info->nmc;
NMDevice *device = info->device;
- NMActiveConnection *active;
- GError *error = NULL;
+ gs_unref_object NMActiveConnection *active = NULL;
+ gs_free_error GError *error = NULL;
if (info->create)
active = nm_client_add_and_activate_connection_finish (NM_CLIENT (client), result, &error);
@@ -1886,37 +1889,36 @@ add_and_activate_cb (GObject *client,
active = nm_client_activate_connection_finish (NM_CLIENT (client), result, &error);
if (error) {
- if (info->hotspot)
+ if (info->hotspot) {
g_string_printf (nmc->return_text, _("Error: Failed to setup a Wi-Fi hotspot: %s"),
error->message);
- else if (info->create)
+ } else if (info->create) {
g_string_printf (nmc->return_text, _("Error: Failed to add/activate new connection: %s"),
error->message);
- else
+ } else {
g_string_printf (nmc->return_text, _("Error: Failed to activate connection: %s"),
error->message);
- g_error_free (error);
+ }
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
quit ();
- } else {
- if (nmc->nowait_flag) {
- g_object_unref (active);
- quit ();
- } else {
- g_object_ref (device);
- g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active);
- g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device);
+ return;
+ }
- connected_state_cb (device, active);
+ if (nmc->nowait_flag) {
+ quit ();
+ return;
+ }
- g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc); /* Exit if timeout expires */
+ g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active);
+ g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device);
- if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
- progress_id = g_timeout_add (120, progress_cb, device);
- }
- }
+ connected_state_cb (g_object_ref (device),
+ g_steal_pointer (&active));
+
+ g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc); /* Exit if timeout expires */
- add_and_activate_info_free (info);
+ if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
+ progress_id = g_timeout_add (120, progress_cb, device);
}
static void
@@ -1945,9 +1947,9 @@ create_connect_connection_for_device (AddAndActivateInfo *info)
static void
connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data)
{
- AddAndActivateInfo *info = user_data;
+ nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data;
NmCli *nmc = info->nmc;
- NMActiveConnection *active;
+ gs_unref_object NMActiveConnection *active = NULL;
GError *error = NULL;
const GPtrArray *devices;
NMDevice *device;
@@ -1958,7 +1960,7 @@ connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data)
/* If no connection existed for the device, create one and activate it */
if (g_error_matches (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_CONNECTION)) {
info->create = TRUE;
- create_connect_connection_for_device (info);
+ create_connect_connection_for_device (g_steal_pointer (&info));
return;
}
@@ -1967,42 +1969,41 @@ connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data)
g_error_free (error);
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
quit ();
- } else {
- g_assert (active);
- devices = nm_active_connection_get_devices (active);
- if (devices->len == 0) {
- g_string_printf (nmc->return_text, _("Error: Device activation failed: device was disconnected"));
- nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
- g_object_unref (active);
- quit ();
- add_and_activate_info_free (info);
- return;
- }
+ return;
+ }
- device = g_ptr_array_index (devices, 0);
+ nm_assert (NM_IS_ACTIVE_CONNECTION (active));
- if (nmc->nowait_flag) {
- g_object_unref (active);
- quit ();
- } else {
- if (nmc->secret_agent) {
- NMRemoteConnection *connection = nm_active_connection_get_connection (active);
+ devices = nm_active_connection_get_devices (active);
+ if (devices->len == 0) {
+ g_string_printf (nmc->return_text, _("Error: Device activation failed: device was disconnected"));
+ nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
+ quit ();
+ return;
+ }
- nm_secret_agent_simple_enable (nmc->secret_agent,
- nm_connection_get_path (NM_CONNECTION (connection)));
- }
+ device = g_ptr_array_index (devices, 0);
- g_object_ref (device);
- g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active);
- g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device);
+ if (nmc->nowait_flag) {
+ quit ();
+ return;
+ }
- connected_state_cb (device, active);
+ if (nmc->secret_agent) {
+ NMRemoteConnection *connection = nm_active_connection_get_connection (active);
- /* Start timer not to loop forever if "notify::state" signal is not issued */
- g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc);
- }
+ nm_secret_agent_simple_enable (nmc->secret_agent,
+ nm_connection_get_path (NM_CONNECTION (connection)));
}
- add_and_activate_info_free (info);
+
+ g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active);
+ g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device);
+
+ connected_state_cb (g_object_ref (device),
+ g_steal_pointer (&active));
+
+ /* Start timer not to loop forever if "notify::state" signal is not issued */
+ g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc);
}
static NMCResultCode
@@ -3148,7 +3149,7 @@ static void
activate_update2_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
NMRemoteConnection *remote_con = NM_REMOTE_CONNECTION (source_object);
- AddAndActivateInfo *info = user_data;
+ nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data;
NmCli *nmc = info->nmc;
gs_unref_variant GVariant *ret = NULL;
GError *error = NULL;
@@ -3160,7 +3161,6 @@ activate_update2_cb (GObject *source_object, GAsyncResult *res, gpointer user_da
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
quit ();
- add_and_activate_info_free (info);
return;
}
@@ -3170,7 +3170,7 @@ activate_update2_cb (GObject *source_object, GAsyncResult *res, gpointer user_da
info->specific_object,
NULL,
add_and_activate_cb,
- info);
+ g_steal_pointer (&info));
}
static NMCResultCode