From 9809eb4da1b12635d35de2cb6b7094df723ad0dc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 7 Jan 2015 13:25:41 +0100 Subject: [PATCH] config: move keyfile values to NMConfigData (cherry picked from commit ba74f9d2428c937014c3bcd02ac25591bbc1abc5) --- src/nm-config-data.c | 119 ++++++++++++++++++------- src/nm-config-data.h | 3 + src/nm-config.c | 41 +++------ src/nm-config.h | 3 +- src/settings/plugins/ifnet/plugin.c | 12 +-- src/settings/plugins/ifupdown/plugin.c | 6 +- src/tests/config/test-config.c | 16 ++-- 7 files changed, 121 insertions(+), 79 deletions(-) diff --git a/src/nm-config-data.c b/src/nm-config-data.c index 8683696e81..93a56df1e3 100644 --- a/src/nm-config-data.c +++ b/src/nm-config-data.c @@ -21,12 +21,17 @@ #include "nm-config-data.h" +#include + #include "nm-config.h" +#include "gsystem-local-alloc.h" typedef struct { char *config_main_file; char *config_description; + GKeyFile *keyfile; + struct { char *uri; char *response; @@ -39,6 +44,7 @@ enum { PROP_0, PROP_CONFIG_MAIN_FILE, PROP_CONFIG_DESCRIPTION, + PROP_KEYFILE, PROP_CONNECTIVITY_URI, PROP_CONNECTIVITY_INTERVAL, PROP_CONNECTIVITY_RESPONSE, @@ -68,6 +74,14 @@ nm_config_data_get_config_description (const NMConfigData *self) return NM_CONFIG_DATA_GET_PRIVATE (self)->config_description; } +char * +nm_config_data_get_value (const NMConfigData *self, const char *group, const char *key, GError **error) +{ + g_return_val_if_fail (self, NULL); + + return g_key_file_get_string (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, error); +} + const char * nm_config_data_get_connectivity_uri (const NMConfigData *self) { @@ -95,16 +109,51 @@ nm_config_data_get_connectivity_response (const NMConfigData *self) /************************************************************************/ +static gboolean +_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b) +{ + gs_strfreev char **groups = NULL; + guint i, j; + + if (kf_a == kf_b) + return TRUE; + + groups = g_key_file_get_groups (kf_a, NULL); + for (i = 0; groups && groups[i]; i++) { + gs_strfreev char **keys = NULL; + + keys = g_key_file_get_keys (kf_a, groups[i], NULL, NULL); + if (keys) { + for (j = 0; keys[j]; j++) { + gs_free char *key_a = g_key_file_get_value (kf_a, groups[i], keys[j], NULL); + gs_free char *key_b = g_key_file_get_value (kf_b, groups[i], keys[j], NULL); + + if (g_strcmp0 (key_a, key_b) != 0) + return FALSE; + } + } + } + return TRUE; +} + GHashTable * nm_config_data_diff (NMConfigData *old_data, NMConfigData *new_data) { GHashTable *changes; + NMConfigDataPrivate *priv_old, *priv_new; g_return_val_if_fail (NM_IS_CONFIG_DATA (old_data), NULL); g_return_val_if_fail (NM_IS_CONFIG_DATA (new_data), NULL); changes = g_hash_table_new (g_str_hash, g_str_equal); + priv_old = NM_CONFIG_DATA_GET_PRIVATE (old_data); + priv_new = NM_CONFIG_DATA_GET_PRIVATE (new_data); + + if ( !_keyfile_a_contains_all_in_b (priv_old->keyfile, priv_new->keyfile) + || !_keyfile_a_contains_all_in_b (priv_new->keyfile, priv_old->keyfile)) + g_hash_table_insert (changes, NM_CONFIG_CHANGES_VALUES, NULL); + if ( g_strcmp0 (nm_config_data_get_config_main_file (old_data), nm_config_data_get_config_main_file (new_data)) != 0 || g_strcmp0 (nm_config_data_get_config_description (old_data), nm_config_data_get_config_description (new_data)) != 0) g_hash_table_insert (changes, NM_CONFIG_CHANGES_CONFIG_FILES, NULL); @@ -170,14 +219,10 @@ set_property (GObject *object, case PROP_CONFIG_DESCRIPTION: priv->config_description = g_value_dup_string (value); break; - case PROP_CONNECTIVITY_URI: - priv->connectivity.uri = g_value_dup_string (value); - break; - case PROP_CONNECTIVITY_INTERVAL: - priv->connectivity.interval = g_value_get_uint (value); - break; - case PROP_CONNECTIVITY_RESPONSE: - priv->connectivity.response = g_value_dup_string (value); + case PROP_KEYFILE: + priv->keyfile = g_value_dup_boxed (value); + if (!priv->keyfile) + priv->keyfile = nm_config_create_keyfile (); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -209,30 +254,32 @@ nm_config_data_init (NMConfigData *self) { } +static void +constructed (GObject *object) +{ + NMConfigData *self = NM_CONFIG_DATA (object); + NMConfigDataPrivate *priv = NM_CONFIG_DATA_GET_PRIVATE (self); + int interval; + + priv->connectivity.uri = g_key_file_get_value (priv->keyfile, "connectivity", "uri", NULL); + priv->connectivity.response = g_key_file_get_value (priv->keyfile, "connectivity", "response", NULL); + + interval = g_key_file_get_integer (priv->keyfile, "connectivity", "interval", NULL); + priv->connectivity.interval = MAX (0, interval); + + G_OBJECT_CLASS (nm_config_data_parent_class)->constructed (object); +} + NMConfigData * nm_config_data_new (const char *config_main_file, const char *config_description, GKeyFile *keyfile) { - char *connectivity_uri, *connectivity_response; - guint connectivity_interval; - NMConfigData *config_data; - - connectivity_uri = g_key_file_get_value (keyfile, "connectivity", "uri", NULL); - connectivity_interval = g_key_file_get_integer (keyfile, "connectivity", "interval", NULL); - connectivity_response = g_key_file_get_value (keyfile, "connectivity", "response", NULL); - - config_data = g_object_new (NM_TYPE_CONFIG_DATA, - NM_CONFIG_DATA_CONFIG_MAIN_FILE, config_main_file, - NM_CONFIG_DATA_CONFIG_DESCRIPTION, config_description, - NM_CONFIG_DATA_CONNECTIVITY_URI, connectivity_uri, - NM_CONFIG_DATA_CONNECTIVITY_INTERVAL, connectivity_interval, - NM_CONFIG_DATA_CONNECTIVITY_RESPONSE, connectivity_response, - NULL); - g_free (connectivity_uri); - g_free (connectivity_response); - - return config_data; + return g_object_new (NM_TYPE_CONFIG_DATA, + NM_CONFIG_DATA_CONFIG_MAIN_FILE, config_main_file, + NM_CONFIG_DATA_CONFIG_DESCRIPTION, config_description, + NM_CONFIG_DATA_KEYFILE, keyfile, + NULL); } static void @@ -242,6 +289,7 @@ nm_config_data_class_init (NMConfigDataClass *config_class) g_type_class_add_private (config_class, sizeof (NMConfigDataPrivate)); + object_class->constructed = constructed; object_class->dispose = dispose; object_class->finalize = finalize; object_class->get_property = get_property; @@ -263,28 +311,33 @@ nm_config_data_class_init (NMConfigDataClass *config_class) G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property + (object_class, PROP_KEYFILE, + g_param_spec_boxed (NM_CONFIG_DATA_KEYFILE, "", "", + G_TYPE_KEY_FILE, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (object_class, PROP_CONNECTIVITY_URI, g_param_spec_string (NM_CONFIG_DATA_CONNECTIVITY_URI, "", "", NULL, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (object_class, PROP_CONNECTIVITY_INTERVAL, g_param_spec_uint (NM_CONFIG_DATA_CONNECTIVITY_INTERVAL, "", "", 0, G_MAXUINT, 0, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (object_class, PROP_CONNECTIVITY_RESPONSE, g_param_spec_string (NM_CONFIG_DATA_CONNECTIVITY_RESPONSE, "", "", NULL, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); } diff --git a/src/nm-config-data.h b/src/nm-config-data.h index cd6313cdf7..d3425632b0 100644 --- a/src/nm-config-data.h +++ b/src/nm-config-data.h @@ -38,6 +38,7 @@ G_BEGIN_DECLS #define NM_CONFIG_DATA_CONFIG_MAIN_FILE "config-main-file" #define NM_CONFIG_DATA_CONFIG_DESCRIPTION "config-description" +#define NM_CONFIG_DATA_KEYFILE "keyfile" #define NM_CONFIG_DATA_CONNECTIVITY_URI "connectivity-uri" #define NM_CONFIG_DATA_CONNECTIVITY_INTERVAL "connectivity-interval" #define NM_CONFIG_DATA_CONNECTIVITY_RESPONSE "connectivity-response" @@ -61,6 +62,8 @@ GHashTable *nm_config_data_diff (NMConfigData *old_data, NMConfigData *new_data) const char *nm_config_data_get_config_main_file (const NMConfigData *config_data); const char *nm_config_data_get_config_description (const NMConfigData *config_data); +char *nm_config_data_get_value (const NMConfigData *config_data, const char *group, const char *key, GError **error); + const char *nm_config_data_get_connectivity_uri (const NMConfigData *config_data); const guint nm_config_data_get_connectivity_interval (const NMConfigData *config_data); const char *nm_config_data_get_connectivity_response (const NMConfigData *config_data); diff --git a/src/nm-config.c b/src/nm-config.c index 10a63bca34..bddfe0f620 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -61,7 +61,6 @@ typedef struct { char *config_dir; char *no_auto_default_file; - GKeyFile *keyfile; char **plugins; gboolean monitor_connection_files; @@ -227,14 +226,6 @@ nm_config_get_configure_and_quit (NMConfig *config) return NM_CONFIG_GET_PRIVATE (config)->configure_and_quit; } -char * -nm_config_get_value (NMConfig *config, const char *group, const char *key, GError **error) -{ - NMConfigPrivate *priv = NM_CONFIG_GET_PRIVATE (config); - - return g_key_file_get_string (priv->keyfile, group, key, error); -} - gboolean nm_config_get_ignore_carrier (NMConfig *config, NMDevice *device) { @@ -721,7 +712,7 @@ nm_config_reload (NMConfig *self) new_data = nm_config_data_new (config_main_file, config_description, keyfile); g_free (config_main_file); g_free (config_description); - g_key_file_free (keyfile); + g_key_file_unref (keyfile); old_data = priv->config_data; changes = nm_config_data_diff (old_data, new_data); @@ -802,8 +793,6 @@ nm_config_new (const NMConfigCmdLineOptions *cli, GError **error) g_object_unref (self); return NULL; } - g_key_file_free (priv->keyfile); - priv->keyfile = keyfile; /* Initialize read only private members */ @@ -811,29 +800,29 @@ nm_config_new (const NMConfigCmdLineOptions *cli, GError **error) priv->no_auto_default_file = g_strdup (priv->cli.no_auto_default_file); else priv->no_auto_default_file = g_strdup (NM_NO_AUTO_DEFAULT_STATE_FILE); - priv->no_auto_default_orig = g_key_file_get_string_list (priv->keyfile, "main", "no-auto-default", NULL, NULL); + priv->no_auto_default_orig = g_key_file_get_string_list (keyfile, "main", "no-auto-default", NULL, NULL); - priv->plugins = g_key_file_get_string_list (priv->keyfile, "main", "plugins", NULL, NULL); + priv->plugins = g_key_file_get_string_list (keyfile, "main", "plugins", NULL, NULL); if (!priv->plugins) priv->plugins = g_new0 (char *, 1); - priv->monitor_connection_files = _get_bool_value (priv->keyfile, "main", "monitor-connection-files", FALSE); + priv->monitor_connection_files = _get_bool_value (keyfile, "main", "monitor-connection-files", FALSE); - priv->auth_polkit = _get_bool_value (priv->keyfile, "main", "auth-polkit", NM_CONFIG_DEFAULT_AUTH_POLKIT); + priv->auth_polkit = _get_bool_value (keyfile, "main", "auth-polkit", NM_CONFIG_DEFAULT_AUTH_POLKIT); - priv->dhcp_client = g_key_file_get_value (priv->keyfile, "main", "dhcp", NULL); - priv->dns_mode = g_key_file_get_value (priv->keyfile, "main", "dns", NULL); + priv->dhcp_client = g_key_file_get_value (keyfile, "main", "dhcp", NULL); + priv->dns_mode = g_key_file_get_value (keyfile, "main", "dns", NULL); - priv->log_level = g_key_file_get_value (priv->keyfile, "logging", "level", NULL); - priv->log_domains = g_key_file_get_value (priv->keyfile, "logging", "domains", NULL); + priv->log_level = g_key_file_get_value (keyfile, "logging", "level", NULL); + priv->log_domains = g_key_file_get_value (keyfile, "logging", "domains", NULL); - priv->debug = g_key_file_get_value (priv->keyfile, "main", "debug", NULL); + priv->debug = g_key_file_get_value (keyfile, "main", "debug", NULL); - priv->ignore_carrier = g_key_file_get_string_list (priv->keyfile, "main", "ignore-carrier", NULL, NULL); + priv->ignore_carrier = g_key_file_get_string_list (keyfile, "main", "ignore-carrier", NULL, NULL); - priv->configure_and_quit = _get_bool_value (priv->keyfile, "main", "configure-and-quit", FALSE); + priv->configure_and_quit = _get_bool_value (keyfile, "main", "configure-and-quit", FALSE); - priv->config_data_orig = nm_config_data_new (config_main_file, config_description, priv->keyfile); + priv->config_data_orig = nm_config_data_new (config_main_file, config_description, keyfile); /* Initialize mutable members. */ @@ -844,6 +833,7 @@ nm_config_new (const NMConfigCmdLineOptions *cli, GError **error) g_free (config_main_file); g_free (config_description); + g_key_file_unref (keyfile); return self; } @@ -853,8 +843,6 @@ nm_config_init (NMConfig *config) NMConfigPrivate *priv = NM_CONFIG_GET_PRIVATE (config); priv->auth_polkit = NM_CONFIG_DEFAULT_AUTH_POLKIT; - - priv->keyfile = nm_config_create_keyfile (); } static void @@ -864,7 +852,6 @@ finalize (GObject *gobject) g_free (priv->config_dir); g_free (priv->no_auto_default_file); - g_clear_pointer (&priv->keyfile, g_key_file_unref); g_strfreev (priv->plugins); g_free (priv->dhcp_client); g_free (priv->dns_mode); diff --git a/src/nm-config.h b/src/nm-config.h index a961199499..98a9d2af6d 100644 --- a/src/nm-config.h +++ b/src/nm-config.h @@ -44,6 +44,7 @@ G_BEGIN_DECLS #define NM_CONFIG_SIGNAL_CONFIG_CHANGED "config-changed" #define NM_CONFIG_CHANGES_CONFIG_FILES "config-files" +#define NM_CONFIG_CHANGES_VALUES "values" #define NM_CONFIG_CHANGES_CONNECTIVITY "connectivity" typedef struct NMConfigCmdLineOptions NMConfigCmdLineOptions; @@ -80,8 +81,6 @@ void nm_config_set_ethernet_no_auto_default (NMConfig *config, NMDevice *de gboolean nm_config_get_ignore_carrier (NMConfig *config, NMDevice *device); -char *nm_config_get_value (NMConfig *config, const char *group, const char *key, GError **error); - /* for main.c only */ NMConfigCmdLineOptions *nm_config_cmd_line_options_new (void); void nm_config_cmd_line_options_free (NMConfigCmdLineOptions *cli); diff --git a/src/settings/plugins/ifnet/plugin.c b/src/settings/plugins/ifnet/plugin.c index 33711639da..7f1eb4ff93 100644 --- a/src/settings/plugins/ifnet/plugin.c +++ b/src/settings/plugins/ifnet/plugin.c @@ -123,9 +123,9 @@ is_managed_plugin (void) { char *result = NULL; - result = nm_config_get_value (nm_config_get (), - IFNET_KEY_FILE_GROUP, IFNET_KEY_FILE_KEY_MANAGED, - NULL); + result = nm_config_data_get_value (nm_config_get_data_orig (nm_config_get ()), + IFNET_KEY_FILE_GROUP, IFNET_KEY_FILE_KEY_MANAGED, + NULL); if (result) { gboolean ret = is_true (result); g_free (result); @@ -264,9 +264,9 @@ reload_connections (NMSystemConfigInterface *config) nm_log_info (LOGD_SETTINGS, "Loading connections"); - str_auto_refresh = nm_config_get_value (nm_config_get (), - IFNET_KEY_FILE_GROUP, "auto_refresh", - NULL); + str_auto_refresh = nm_config_data_get_value (nm_config_get_data_orig (nm_config_get ()), + IFNET_KEY_FILE_GROUP, "auto_refresh", + NULL); if (str_auto_refresh && is_true (str_auto_refresh)) auto_refresh = TRUE; g_free (str_auto_refresh); diff --git a/src/settings/plugins/ifupdown/plugin.c b/src/settings/plugins/ifupdown/plugin.c index fbd3dd2379..1cbff29b3d 100644 --- a/src/settings/plugins/ifupdown/plugin.c +++ b/src/settings/plugins/ifupdown/plugin.c @@ -456,9 +456,9 @@ SCPluginIfupdown_init (NMSystemConfigInterface *config) g_hash_table_destroy (auto_ifaces); /* Check the config file to find out whether to manage interfaces */ - value = nm_config_get_value (nm_config_get (), - IFUPDOWN_KEY_FILE_GROUP, IFUPDOWN_KEY_FILE_KEY_MANAGED, - &error); + value = nm_config_data_get_value (nm_config_get_data_orig (nm_config_get ()), + IFUPDOWN_KEY_FILE_GROUP, IFUPDOWN_KEY_FILE_KEY_MANAGED, + &error); if (error) { nm_log_info (LOGD_SETTINGS, "loading system config file (%s) caused error: %s", nm_config_data_get_config_main_file (nm_config_get_data (nm_config_get ())), diff --git a/src/tests/config/test-config.c b/src/tests/config/test-config.c index 4ffdccea3a..e677243103 100644 --- a/src/tests/config/test-config.c +++ b/src/tests/config/test-config.c @@ -108,16 +108,16 @@ test_config_simple (void) g_assert_cmpstr (plugins[1], ==, "bar"); g_assert_cmpstr (plugins[2], ==, "baz"); - value = nm_config_get_value (config, "extra-section", "extra-key", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "extra-key", NULL); g_assert_cmpstr (value, ==, "some value"); g_free (value); - value = nm_config_get_value (config, "extra-section", "no-key", &error); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "extra-section", "no-key", &error); g_assert (!value); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND); g_clear_error (&error); - value = nm_config_get_value (config, "no-section", "no-key", &error); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "no-section", "no-key", &error); g_assert (!value); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND); g_clear_error (&error); @@ -252,21 +252,21 @@ test_config_confdir (void) g_assert_cmpstr (plugins[3], ==, "one"); g_assert_cmpstr (plugins[4], ==, "two"); - value = nm_config_get_value (config, "main", "extra", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "extra", NULL); g_assert_cmpstr (value, ==, "hello"); g_free (value); - value = nm_config_get_value (config, "main", "new", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "main", "new", NULL); g_assert_cmpstr (value, ==, "something"); /* not ",something" */ g_free (value); - value = nm_config_get_value (config, "order", "a", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "a", NULL); g_assert_cmpstr (value, ==, "90"); g_free (value); - value = nm_config_get_value (config, "order", "b", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "b", NULL); g_assert_cmpstr (value, ==, "10"); g_free (value); - value = nm_config_get_value (config, "order", "c", NULL); + value = nm_config_data_get_value (nm_config_get_data_orig (config), "order", "c", NULL); g_assert_cmpstr (value, ==, "0"); g_free (value);