From d5c0d345d65aa0b4514cb86b61fce743420aea22 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 2 Oct 2018 19:44:31 +0200 Subject: keyfile: refactor setting default ID/UUID in nm_keyfile_read() Split out the functionality for auto-detecting the ID and UUID of a connection. First of all, nm_keyfile_read() is already overcomplicated. The next commit will require the caller to explicitly call these functions. --- libnm-core/nm-keyfile-internal.h | 6 ++++ libnm-core/nm-keyfile.c | 59 ++++++++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/libnm-core/nm-keyfile-internal.h b/libnm-core/nm-keyfile-internal.h index d6a147116..a620d9aa1 100644 --- a/libnm-core/nm-keyfile-internal.h +++ b/libnm-core/nm-keyfile-internal.h @@ -101,6 +101,12 @@ NMConnection *nm_keyfile_read (GKeyFile *keyfile, void *user_data, GError **error); +gboolean nm_keyfile_read_ensure_id (NMConnection *connection, + const char *fallback_id); + +gboolean nm_keyfile_read_ensure_uuid (NMConnection *connection, + const char *fallback_uuid_seed); + /*****************************************************************************/ typedef enum { diff --git a/libnm-core/nm-keyfile.c b/libnm-core/nm-keyfile.c index be37d2a8e..4de0ca415 100644 --- a/libnm-core/nm-keyfile.c +++ b/libnm-core/nm-keyfile.c @@ -2812,6 +2812,46 @@ read_vpn_secrets (KeyfileReaderInfo *info, NMSettingVpn *s_vpn) } } +gboolean +nm_keyfile_read_ensure_id (NMConnection *connection, + const char *fallback_id) +{ + NMSettingConnection *s_con; + + g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); + g_return_val_if_fail (fallback_id, FALSE); + + s_con = nm_connection_get_setting_connection (connection); + g_return_val_if_fail (NM_IS_SETTING_CONNECTION (s_con), FALSE); + + if (nm_setting_connection_get_id (s_con)) + return FALSE; + + g_object_set (s_con, NM_SETTING_CONNECTION_ID, fallback_id, NULL); + return TRUE; +} + +gboolean +nm_keyfile_read_ensure_uuid (NMConnection *connection, + const char *fallback_uuid_seed) +{ + NMSettingConnection *s_con; + gs_free char *hashed_uuid = NULL; + + g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); + g_return_val_if_fail (fallback_uuid_seed, FALSE); + + s_con = nm_connection_get_setting_connection (connection); + g_return_val_if_fail (NM_IS_SETTING_CONNECTION (s_con), FALSE); + + if (nm_setting_connection_get_uuid (s_con)) + return FALSE; + + hashed_uuid = _nm_utils_uuid_generate_from_strings ("keyfile", fallback_uuid_seed, NULL); + g_object_set (s_con, NM_SETTING_CONNECTION_UUID, hashed_uuid, NULL); + return TRUE; +} + /** * nm_keyfile_read: * @keyfile: the keyfile from which to create the connection @@ -2902,23 +2942,14 @@ nm_keyfile_read (GKeyFile *keyfile, nm_connection_add_setting (connection, NM_SETTING (s_con)); } - /* Make sure that we have 'id' even if not explicitly specified in the keyfile */ - if ( keyfile_name - && !nm_setting_connection_get_id (s_con)) { - gs_free char *base_name = NULL; + if (keyfile_name) { + gs_free char *basename = g_path_get_basename (keyfile_name); - base_name = g_path_get_basename (keyfile_name); - g_object_set (s_con, NM_SETTING_CONNECTION_ID, base_name, NULL); + nm_keyfile_read_ensure_id (connection, basename); } - /* Make sure that we have 'uuid' even if not explicitly specified in the keyfile */ - if ( keyfile_name - && !nm_setting_connection_get_uuid (s_con)) { - gs_free char *hashed_uuid = NULL; - - hashed_uuid = _nm_utils_uuid_generate_from_strings ("keyfile", keyfile_name, NULL); - g_object_set (s_con, NM_SETTING_CONNECTION_UUID, hashed_uuid, NULL); - } + if (keyfile_name) + nm_keyfile_read_ensure_uuid (connection, keyfile_name); /* Make sure that we have 'interface-name' even if it was specified in the * "wrong" (ie, deprecated) group. -- cgit v1.2.3