diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index 08e9febfa..c85b5c4b5 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -2808,6 +2808,7 @@ ifnet_update_parsers_by_connection (NMConnection *connection, const char *config_file, const char *wpa_file, gchar **out_new_name, + gchar **out_backup, GError **error) { NMSettingConnection *s_con; @@ -2891,7 +2892,7 @@ ifnet_update_parsers_by_connection (NMConnection *connection, /* connection id will be displayed in nm-applet */ update_connection_id (connection, conn_name); - success = ifnet_flush_to_file (config_file); + success = ifnet_flush_to_file (config_file, out_backup); if (success) wpa_flush_to_file (wpa_file); @@ -2905,12 +2906,13 @@ out: gboolean ifnet_delete_connection_in_parsers (const char *conn_name, const char *config_file, - const char *wpa_file) + const char *wpa_file, + gchar **out_backup) { gboolean result = FALSE; ifnet_delete_network (conn_name); - result = ifnet_flush_to_file (config_file); + result = ifnet_flush_to_file (config_file, out_backup); if (result) { /* connection may not have security information * so simply ignore the return value*/ @@ -3005,6 +3007,7 @@ char * ifnet_add_new_connection (NMConnection *connection, const char *config_file, const char *wpa_file, + gchar **out_backup, GError **error) { NMSettingConnection *s_con; @@ -3048,6 +3051,7 @@ ifnet_add_new_connection (NMConnection *connection, config_file, wpa_file, NULL, + out_backup, error); } diff --git a/src/settings/plugins/ifnet/connection_parser.h b/src/settings/plugins/ifnet/connection_parser.h index 46ae9195e..e8596a64e 100644 --- a/src/settings/plugins/ifnet/connection_parser.h +++ b/src/settings/plugins/ifnet/connection_parser.h @@ -34,14 +34,17 @@ gboolean ifnet_update_parsers_by_connection (NMConnection *connection, const char *config_file, const char *wpa_file, gchar **out_new_name, + gchar **out_backup, GError **error); gboolean ifnet_delete_connection_in_parsers (const char *conn_name, const char *config_file, - const char *wpa_file); + const char *wpa_file, + gchar **out_backup); char * ifnet_add_new_connection (NMConnection *connection, const char *config_file, const char *wpa_file, + gchar **out_backup, GError ** error); #endif diff --git a/src/settings/plugins/ifnet/net_parser.c b/src/settings/plugins/ifnet/net_parser.c index 2562e6133..f37366afe 100644 --- a/src/settings/plugins/ifnet/net_parser.c +++ b/src/settings/plugins/ifnet/net_parser.c @@ -590,7 +590,7 @@ done: } gboolean -ifnet_flush_to_file (const char *config_file) +ifnet_flush_to_file (const char *config_file, gchar **out_backup) { GIOChannel *channel; GError **error = NULL; @@ -600,18 +600,20 @@ ifnet_flush_to_file (const char *config_file) gchar *out_line = NULL; gsize bytes_written; gboolean result = FALSE; + gchar *backup; if (!net_parser_data_changed) return TRUE; if (!conn_table || !global_settings_table) return FALSE; - backup_file (config_file); + backup = backup_file (config_file); channel = g_io_channel_new_file (config_file, "w", NULL); if (!channel) { PLUGIN_WARN (IFNET_PLUGIN_NAME, "Can't open file %s for writing", config_file); + g_free (backup); return FALSE; } g_hash_table_iter_init (&iter, global_settings_table); @@ -714,7 +716,13 @@ ifnet_flush_to_file (const char *config_file) } result = TRUE; net_parser_data_changed = FALSE; + done: + if (result && out_backup) + *out_backup = backup; + else + g_free (backup); + g_io_channel_shutdown (channel, FALSE, NULL); g_io_channel_unref (channel); return result; diff --git a/src/settings/plugins/ifnet/net_parser.h b/src/settings/plugins/ifnet/net_parser.h index 8868dbe0a..47086c762 100644 --- a/src/settings/plugins/ifnet/net_parser.h +++ b/src/settings/plugins/ifnet/net_parser.h @@ -38,7 +38,7 @@ const char *ifnet_get_global_setting (const char *group, const char *key); gboolean ifnet_has_network (const char *conn_name); /* Writer functions */ -gboolean ifnet_flush_to_file (const char *config_file); +gboolean ifnet_flush_to_file (const char *config_file, gchar **out_backup); void ifnet_set_data (const char *conn_name, const char *key, const char *value); gboolean ifnet_add_network (const char *name, const char *type); gboolean ifnet_delete_network (const char *conn_name); diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index 40e9c100d..17eb16771 100644 --- a/src/settings/plugins/ifnet/net_utils.c +++ b/src/settings/plugins/ifnet/net_utils.c @@ -950,7 +950,7 @@ get_dhcp_hostname_and_client_id (char **hostname, char **client_id) g_free (contents); } -void backup_file (const gchar* target) +gchar *backup_file (const gchar* target) { GFile *source, *backup; gchar* backup_path; @@ -961,8 +961,11 @@ void backup_file (const gchar* target) backup = g_file_new_for_path (backup_path); g_file_copy (source, backup, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, error); - if (error && *error) + if (error && *error) { PLUGIN_WARN (IFNET_PLUGIN_NAME, "Backup failed: %s", (*error)->message); + g_free (backup_path); + backup_path = NULL; + } - g_free (backup_path); + return backup_path; } diff --git a/src/settings/plugins/ifnet/net_utils.h b/src/settings/plugins/ifnet/net_utils.h index 925ad6153..c2ed83a13 100644 --- a/src/settings/plugins/ifnet/net_utils.h +++ b/src/settings/plugins/ifnet/net_utils.h @@ -77,5 +77,5 @@ gboolean is_true (const char *str); void get_dhcp_hostname_and_client_id (char **hostname, char **client_id); -void backup_file (const gchar* target); +gchar *backup_file (const gchar* target); #endif diff --git a/src/settings/plugins/ifnet/nm-ifnet-connection.c b/src/settings/plugins/ifnet/nm-ifnet-connection.c index 97c4327b6..873a62ecd 100644 --- a/src/settings/plugins/ifnet/nm-ifnet-connection.c +++ b/src/settings/plugins/ifnet/nm-ifnet-connection.c @@ -108,6 +108,7 @@ commit_changes (NMSettingsConnection *connection, CONF_NET_FILE, WPA_SUPPLICANT_CONF, &new_name, + NULL, &error)) { PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Failed to update %s", priv->conn_name); reload_parsers (); @@ -135,8 +136,7 @@ do_delete (NMSettingsConnection *connection, NMIfnetConnectionPrivate *priv = NM_IFNET_CONNECTION_GET_PRIVATE (connection); g_signal_emit (connection, signals[IFNET_CANCEL_MONITORS], 0); - if (!ifnet_delete_connection_in_parsers - (priv->conn_name, CONF_NET_FILE, WPA_SUPPLICANT_CONF)) { + if (!ifnet_delete_connection_in_parsers (priv->conn_name, CONF_NET_FILE, WPA_SUPPLICANT_CONF, NULL)) { PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Failed to delete %s", priv->conn_name); reload_parsers (); diff --git a/src/settings/plugins/ifnet/plugin.c b/src/settings/plugins/ifnet/plugin.c index c3a8bc7da..5100ad3e4 100644 --- a/src/settings/plugins/ifnet/plugin.c +++ b/src/settings/plugins/ifnet/plugin.c @@ -360,7 +360,7 @@ add_connection (NMSystemConfigInterface *config, nm_connection_for_each_setting_value (source, check_flagged_secrets, &has_flagged_secrets); if (!has_flagged_secrets) { - conn_name = ifnet_add_new_connection (source, CONF_NET_FILE, WPA_SUPPLICANT_CONF, error); + conn_name = ifnet_add_new_connection (source, CONF_NET_FILE, WPA_SUPPLICANT_CONF, NULL, error); if (conn_name) connection = nm_ifnet_connection_new (conn_name, source); reload_connections (config); diff --git a/src/settings/plugins/ifnet/tests/test_all.c b/src/settings/plugins/ifnet/tests/test_all.c index 378564b67..4c3126010 100644 --- a/src/settings/plugins/ifnet/tests/test_all.c +++ b/src/settings/plugins/ifnet/tests/test_all.c @@ -305,6 +305,16 @@ test_new_connection () g_object_unref (connection); } +static void +kill_backup (char **path) +{ + if (path) { + unlink (*path); + g_free (*path); + *path = NULL; + } +} + #define NET_GEN_NAME "net.generate" #define SUP_GEN_NAME "wpa_supplicant.conf.generate" @@ -314,6 +324,7 @@ test_update_connection (const char *basepath) GError *error = NULL; NMConnection *connection; gboolean success; + char *backup = NULL; connection = ifnet_update_connection_from_config_block ("eth0", basepath, &error); ASSERT (connection != NULL, "get connection", @@ -324,7 +335,9 @@ test_update_connection (const char *basepath) NET_GEN_NAME, SUP_GEN_NAME, NULL, + &backup, &error); + kill_backup (&backup); ASSERT (success, "update connection", "update connection failed %s", "eth0"); g_object_unref (connection); @@ -336,7 +349,9 @@ test_update_connection (const char *basepath) NET_GEN_NAME, SUP_GEN_NAME, NULL, + &backup, &error); + kill_backup (&backup); ASSERT (success, "update connection", "update connection failed %s", "0xab3ace"); g_object_unref (connection); @@ -345,17 +360,21 @@ test_update_connection (const char *basepath) } static void -test_add_connection () +test_add_connection (const char *basepath) { NMConnection *connection; + char *backup = NULL; - connection = ifnet_update_connection_from_config_block ("eth0", NULL, NULL); - ASSERT (ifnet_add_new_connection (connection, NET_GEN_NAME, SUP_GEN_NAME, NULL), + connection = ifnet_update_connection_from_config_block ("eth0", basepath, NULL); + ASSERT (ifnet_add_new_connection (connection, NET_GEN_NAME, SUP_GEN_NAME, &backup, NULL), "add connection", "add connection failed: %s", "eth0"); + kill_backup (&backup); g_object_unref (connection); - connection = ifnet_update_connection_from_config_block ("myxjtu2", NULL, NULL); - ASSERT (ifnet_add_new_connection (connection, NET_GEN_NAME, SUP_GEN_NAME, NULL), + + connection = ifnet_update_connection_from_config_block ("myxjtu2", basepath, NULL); + ASSERT (ifnet_add_new_connection (connection, NET_GEN_NAME, SUP_GEN_NAME, &backup, NULL), "add connection", "add connection failed: %s", "myxjtu2"); + kill_backup (&backup); g_object_unref (connection); unlink (NET_GEN_NAME); @@ -367,20 +386,24 @@ test_delete_connection () { GError *error = NULL; NMConnection *connection; + char *backup = NULL; connection = ifnet_update_connection_from_config_block ("eth7", NULL, &error); ASSERT (connection != NULL, "get connection", "get connection failed: %s", error ? error->message : "None"); - ASSERT (ifnet_delete_connection_in_parsers ("eth7", NET_GEN_NAME, SUP_GEN_NAME), + ASSERT (ifnet_delete_connection_in_parsers ("eth7", NET_GEN_NAME, SUP_GEN_NAME, &backup), "delete connection", "delete connection failed: %s", "eth7"); + kill_backup (&backup); g_object_unref (connection); + connection = ifnet_update_connection_from_config_block ("qiaomuf", NULL, &error); ASSERT (connection != NULL, "get connection", "get connection failed: %s", error ? error->message : "None"); - ASSERT (ifnet_delete_connection_in_parsers ("qiaomuf", NET_GEN_NAME, SUP_GEN_NAME), + ASSERT (ifnet_delete_connection_in_parsers ("qiaomuf", NET_GEN_NAME, SUP_GEN_NAME, &backup), "delete connection", "delete connection failed: %s", "qiaomuf"); + kill_backup (&backup); g_object_unref (connection); unlink (NET_GEN_NAME); @@ -429,7 +452,7 @@ main (int argc, char **argv) test_convert_ipv4_routes_block (); test_new_connection (); test_update_connection (argv[1]); - test_add_connection (); + test_add_connection (argv[1]); test_delete_connection (); test_missing_config ();