keyfile: split nm_keyfile_plugin_write_connection()

The non-test function doesn't need to take as many arguments, which
makes the code a lot cleaner to look at in the plugin and connection
subclass.
This commit is contained in:
Dan Williams
2011-02-09 17:23:10 -06:00
parent f1d0293517
commit eddc66e36d
5 changed files with 71 additions and 38 deletions

View File

@@ -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;

View File

@@ -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;
cid = nm_connection_get_id (NM_CONNECTION (connection));
g_assert (cid);
uuid = nm_connection_get_uuid (NM_CONNECTION (connection));
g_assert (uuid);
if (connection) {
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;

View File

@@ -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)");

View File

@@ -687,11 +687,12 @@ _writer_id_to_filename (const char *id)
return filename;
}
gboolean
nm_keyfile_plugin_write_connection (NMConnection *connection,
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)
{
@@ -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);
}

View File

@@ -27,6 +27,11 @@
#include <nm-connection.h>
gboolean nm_keyfile_plugin_write_connection (NMConnection *connection,
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,