diff --git a/system-settings/plugins/keyfile/nm-keyfile-connection.c b/system-settings/plugins/keyfile/nm-keyfile-connection.c index 0c9912e21a..048cd0cd4f 100644 --- a/system-settings/plugins/keyfile/nm-keyfile-connection.c +++ b/system-settings/plugins/keyfile/nm-keyfile-connection.c @@ -105,7 +105,10 @@ commit_changes (NMSettingsConnection *connection, char *path = NULL; GError *error = NULL; - if (!nm_keyfile_plugin_write_connection (NM_CONNECTION (connection), KEYFILE_DIR, 0, 0, &path, &error)) { + if (!nm_keyfile_plugin_write_connection (NM_CONNECTION (connection), + priv->path, + &path, + &error)) { callback (connection, error, user_data); g_clear_error (&error); return; diff --git a/system-settings/plugins/keyfile/plugin.c b/system-settings/plugins/keyfile/plugin.c index b72ec10518..ba56467d7b 100644 --- a/system-settings/plugins/keyfile/plugin.c +++ b/system-settings/plugins/keyfile/plugin.c @@ -73,31 +73,21 @@ static NMSettingsConnection * _internal_new_connection (SCPluginKeyfile *self, const char *full_path, NMConnection *source, - const char **out_cid, GError **error) { SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self); - const char *cid, *uuid; NMKeyfileConnection *connection; g_return_val_if_fail (full_path != NULL, NULL); connection = nm_keyfile_connection_new (full_path, source, error); - if (!connection) - return NULL; + if (connection) { + g_hash_table_insert (priv->hash, + (gpointer) nm_keyfile_connection_get_path (connection), + connection); + } - cid = nm_connection_get_id (NM_CONNECTION (connection)); - g_assert (cid); - uuid = nm_connection_get_uuid (NM_CONNECTION (connection)); - g_assert (uuid); - - g_hash_table_insert (priv->hash, - (gpointer) nm_keyfile_connection_get_path (connection), - connection); - - if (out_cid) - *out_cid = cid; - return NM_SETTINGS_CONNECTION (connection); + return (NMSettingsConnection *) connection; } static void @@ -106,7 +96,7 @@ read_connections (NMSystemConfigInterface *config) SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config); GDir *dir; GError *error = NULL; - const char *item, *cid; + const char *item; dir = g_dir_open (KEYFILE_DIR, 0, &error); if (!dir) { @@ -128,9 +118,10 @@ read_connections (NMSystemConfigInterface *config) full_path = g_build_filename (KEYFILE_DIR, item, NULL); PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, "parsing %s ... ", item); - connection = _internal_new_connection (self, full_path, NULL, &cid, &error); + connection = _internal_new_connection (self, full_path, NULL, &error); if (connection) { - PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, " read connection '%s'", cid); + PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, " read connection '%s'", + nm_connection_get_id (NM_CONNECTION (connection))); } else { PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, " error: %s", (error && error->message) ? error->message : "(unknown)"); @@ -408,8 +399,8 @@ add_connection (NMSystemConfigInterface *config, char *path = NULL; /* Write it out first, then add the connection to our internal list */ - if (nm_keyfile_plugin_write_connection (connection, KEYFILE_DIR, 0, 0, &path, error)) { - added = _internal_new_connection (self, path, connection, NULL, error); + if (nm_keyfile_plugin_write_connection (connection, NULL, &path, error)) { + added = _internal_new_connection (self, path, connection, error); g_free (path); } return added; diff --git a/system-settings/plugins/keyfile/tests/test-keyfile.c b/system-settings/plugins/keyfile/tests/test-keyfile.c index e79c8a3307..1aed7a0e68 100644 --- a/system-settings/plugins/keyfile/tests/test-keyfile.c +++ b/system-settings/plugins/keyfile/tests/test-keyfile.c @@ -693,7 +693,7 @@ test_write_wired_connection (void) /* Write out the connection */ owner_uid = geteuid (); owner_grp = getegid (); - success = nm_keyfile_plugin_write_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); ASSERT (success == TRUE, "connection-write", "failed to allocate write keyfile: %s", error ? error->message : "(none)"); @@ -953,7 +953,7 @@ test_write_ip6_wired_connection (void) /* Write out the connection */ owner_uid = geteuid (); owner_grp = getegid (); - success = nm_keyfile_plugin_write_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); ASSERT (success == TRUE, "connection-write", "failed to allocate write keyfile: %s", error ? error->message : "(none)"); @@ -1271,7 +1271,7 @@ test_write_wireless_connection (void) /* Write out the connection */ owner_uid = geteuid (); owner_grp = getegid (); - success = nm_keyfile_plugin_write_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); ASSERT (success == TRUE, "connection-write", "failed to allocate write keyfile: %s", error ? error->message : "(none)"); @@ -1401,7 +1401,7 @@ test_write_string_ssid (void) /* Write out the connection */ owner_uid = geteuid (); owner_grp = getegid (); - success = nm_keyfile_plugin_write_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); ASSERT (success == TRUE, "connection-write", "failed to allocate write keyfile: %s", error ? error->message : "(none)"); @@ -1699,7 +1699,7 @@ test_write_bt_dun_connection (void) /* Write out the connection */ owner_uid = geteuid (); owner_grp = getegid (); - success = nm_keyfile_plugin_write_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); ASSERT (success == TRUE, "connection-write", "failed to allocate write keyfile: %s", error ? error->message : "(none)"); @@ -1961,7 +1961,7 @@ test_write_gsm_connection (void) /* Write out the connection */ owner_uid = geteuid (); owner_grp = getegid (); - success = nm_keyfile_plugin_write_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); ASSERT (success == TRUE, "connection-write", "failed to allocate write keyfile: %s", error ? error->message : "(none)"); diff --git a/system-settings/plugins/keyfile/writer.c b/system-settings/plugins/keyfile/writer.c index 2fcd306e0c..87772cdf8e 100644 --- a/system-settings/plugins/keyfile/writer.c +++ b/system-settings/plugins/keyfile/writer.c @@ -687,13 +687,14 @@ _writer_id_to_filename (const char *id) return filename; } -gboolean -nm_keyfile_plugin_write_connection (NMConnection *connection, - const char *keyfile_dir, - uid_t owner_uid, - pid_t owner_grp, - char **out_path, - GError **error) +static gboolean +_internal_write_connection (NMConnection *connection, + const char *keyfile_dir, + uid_t owner_uid, + pid_t owner_grp, + const char *existing_path, + char **out_path, + GError **error) { GKeyFile *key_file; char *data; @@ -722,6 +723,8 @@ nm_keyfile_plugin_write_connection (NMConnection *connection, path = g_build_filename (keyfile_dir, filename, NULL); g_free (filename); + /* If the file already exists */ + g_file_set_contents (path, data, len, error); if (chown (path, owner_uid, owner_grp) < 0) { g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, @@ -747,3 +750,34 @@ out: g_key_file_free (key_file); return success; } + +gboolean +nm_keyfile_plugin_write_connection (NMConnection *connection, + const char *existing_path, + char **out_path, + GError **error) +{ + return _internal_write_connection (connection, + KEYFILE_DIR, + 0, 0, + existing_path, + out_path, + error); +} + +gboolean +nm_keyfile_plugin_write_test_connection (NMConnection *connection, + const char *keyfile_dir, + uid_t owner_uid, + pid_t owner_grp, + char **out_path, + GError **error) +{ + return _internal_write_connection (connection, + keyfile_dir, + owner_uid, owner_grp, + NULL, + out_path, + error); +} + diff --git a/system-settings/plugins/keyfile/writer.h b/system-settings/plugins/keyfile/writer.h index 730a9b3bda..a602f2f4a3 100644 --- a/system-settings/plugins/keyfile/writer.h +++ b/system-settings/plugins/keyfile/writer.h @@ -27,10 +27,15 @@ #include gboolean nm_keyfile_plugin_write_connection (NMConnection *connection, - const char *keyfile_dir, - uid_t owner_uid, - pid_t owner_grp, + const char *existing_path, char **out_path, GError **error); +gboolean nm_keyfile_plugin_write_test_connection (NMConnection *connection, + const char *keyfile_dir, + uid_t owner_uid, + pid_t owner_grp, + char **out_path, + GError **error); + #endif /* _KEYFILE_PLUGIN_WRITER_H */