From a03b9b81c882e3387854d82235e6bf0ebedcb959 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 26 Apr 2018 11:46:16 +0200 Subject: cli: sort active-connection for `nmcli connection show $PROFILE` output There might be multiple active connections. Output them in a defined order. --- clients/cli/connections.c | 26 +++++++++++++--------- .../test_003-069.expected | 12 +++++----- .../test_003-070.expected | 12 +++++----- .../test_003-091.expected | 2 +- .../test_003-092.expected | 2 +- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 26b56067b7..eb9c1ae1b0 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -865,36 +865,42 @@ construct_header_name (const char *base, const char *spec) return g_strdup_printf ("%s (%s)", base, spec); } +static int +get_ac_for_connection_cmp (gconstpointer pa, gconstpointer pb, gpointer user_data) +{ + NMActiveConnection *ac_a = *((NMActiveConnection *const*) pa); + NMActiveConnection *ac_b = *((NMActiveConnection *const*) pb); + + return active_connection_cmp (ac_a, ac_b); +} + static NMActiveConnection * get_ac_for_connection (const GPtrArray *active_cons, NMConnection *connection, GPtrArray **out_result) { - const char *con_path, *ac_con_path; guint i; NMActiveConnection *best_candidate = NULL; GPtrArray *result = out_result ? *out_result : NULL; - con_path = nm_connection_get_path (connection); for (i = 0; i < active_cons->len; i++) { NMActiveConnection *candidate = g_ptr_array_index (active_cons, i); NMRemoteConnection *con; con = nm_active_connection_get_connection (candidate); - if (NM_CONNECTION (con) != connection) { - /* also compare the D-Bus paths. Why? I don't know. */ - ac_con_path = con ? nm_connection_get_path (NM_CONNECTION (con)) : NULL; - if (!nm_streq0 (ac_con_path, con_path)) - continue; - } + if (NM_CONNECTION (con) != connection) + continue; if (!out_result) return candidate; - if (!best_candidate) - best_candidate = candidate; if (!result) result = g_ptr_array_new_with_free_func (g_object_unref); g_ptr_array_add (result, g_object_ref (candidate)); } + if (result) { + g_ptr_array_sort_with_data (result, get_ac_for_connection_cmp, NULL); + best_candidate = result->pdata[0]; + } + NM_SET_OUT (out_result, result); return best_candidate; } diff --git a/clients/tests/test-client.check-on-disk/test_003-069.expected b/clients/tests/test-client.check-on-disk/test_003-069.expected index 49877a059d..a966f20fd8 100644 --- a/clients/tests/test-client.check-on-disk/test_003-069.expected +++ b/clients/tests/test-client.check-on-disk/test_003-069.expected @@ -84,26 +84,26 @@ proxy.pac-url: -- proxy.pac-script: -- GENERAL.NAME: ethernet GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL -GENERAL.DEVICES: eth0 -GENERAL.STATE: deactivating +GENERAL.DEVICES: eth1 +GENERAL.STATE: activated GENERAL.DEFAULT: no GENERAL.DEFAULT6: no GENERAL.SPEC-OBJECT: -- GENERAL.VPN: no -GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 +GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/2 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3 GENERAL.ZONE: -- GENERAL.MASTER-PATH: -- GENERAL.NAME: ethernet GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL -GENERAL.DEVICES: eth1 -GENERAL.STATE: activated +GENERAL.DEVICES: eth0 +GENERAL.STATE: deactivating GENERAL.DEFAULT: no GENERAL.DEFAULT6: no GENERAL.SPEC-OBJECT: -- GENERAL.VPN: no -GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/2 +GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3 GENERAL.ZONE: -- GENERAL.MASTER-PATH: -- diff --git a/clients/tests/test-client.check-on-disk/test_003-070.expected b/clients/tests/test-client.check-on-disk/test_003-070.expected index ecefcf3860..aaf8307126 100644 --- a/clients/tests/test-client.check-on-disk/test_003-070.expected +++ b/clients/tests/test-client.check-on-disk/test_003-070.expected @@ -84,26 +84,26 @@ proxy.pac-url: -- proxy.pac-script: -- GENERAL.NAME: ethernet GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL -GENERAL.DEVICES: eth0 -GENERAL.STATE: deactivating +GENERAL.DEVICES: eth1 +GENERAL.STATE: activated GENERAL.DEFAULT: nie GENERAL.DEFAULT6: nie GENERAL.SPEC-OBJECT: -- GENERAL.VPN: nie -GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 +GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/2 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3 GENERAL.ZONE: -- GENERAL.MASTER-PATH: -- GENERAL.NAME: ethernet GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL -GENERAL.DEVICES: eth1 -GENERAL.STATE: activated +GENERAL.DEVICES: eth0 +GENERAL.STATE: deactivating GENERAL.DEFAULT: nie GENERAL.DEFAULT6: nie GENERAL.SPEC-OBJECT: -- GENERAL.VPN: nie -GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/2 +GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3 GENERAL.ZONE: -- GENERAL.MASTER-PATH: -- diff --git a/clients/tests/test-client.check-on-disk/test_003-091.expected b/clients/tests/test-client.check-on-disk/test_003-091.expected index 754ca9a0dd..c72d1f76c3 100644 --- a/clients/tests/test-client.check-on-disk/test_003-091.expected +++ b/clients/tests/test-client.check-on-disk/test_003-091.expected @@ -9,6 +9,6 @@ stdout: 0 bytes stderr: 103 bytes >>> ** -nmcli:ERROR:clients/cli/connections.c:1062:fill_output_active_connection: assertion failed: (s_con) +nmcli:ERROR:clients/cli/connections.c:1068:fill_output_active_connection: assertion failed: (s_con) <<< diff --git a/clients/tests/test-client.check-on-disk/test_003-092.expected b/clients/tests/test-client.check-on-disk/test_003-092.expected index 338027ef8b..c00d63d082 100644 --- a/clients/tests/test-client.check-on-disk/test_003-092.expected +++ b/clients/tests/test-client.check-on-disk/test_003-092.expected @@ -9,6 +9,6 @@ stdout: 0 bytes stderr: 103 bytes >>> ** -nmcli:ERROR:clients/cli/connections.c:1062:fill_output_active_connection: assertion failed: (s_con) +nmcli:ERROR:clients/cli/connections.c:1068:fill_output_active_connection: assertion failed: (s_con) <<< -- cgit v1.2.1