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:
@@ -105,7 +105,10 @@ commit_changes (NMSettingsConnection *connection,
|
|||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
GError *error = 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);
|
callback (connection, error, user_data);
|
||||||
g_clear_error (&error);
|
g_clear_error (&error);
|
||||||
return;
|
return;
|
||||||
|
@@ -73,31 +73,21 @@ static NMSettingsConnection *
|
|||||||
_internal_new_connection (SCPluginKeyfile *self,
|
_internal_new_connection (SCPluginKeyfile *self,
|
||||||
const char *full_path,
|
const char *full_path,
|
||||||
NMConnection *source,
|
NMConnection *source,
|
||||||
const char **out_cid,
|
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
|
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
|
||||||
const char *cid, *uuid;
|
|
||||||
NMKeyfileConnection *connection;
|
NMKeyfileConnection *connection;
|
||||||
|
|
||||||
g_return_val_if_fail (full_path != NULL, NULL);
|
g_return_val_if_fail (full_path != NULL, NULL);
|
||||||
|
|
||||||
connection = nm_keyfile_connection_new (full_path, source, error);
|
connection = nm_keyfile_connection_new (full_path, source, error);
|
||||||
if (!connection)
|
if (connection) {
|
||||||
return NULL;
|
g_hash_table_insert (priv->hash,
|
||||||
|
(gpointer) nm_keyfile_connection_get_path (connection),
|
||||||
|
connection);
|
||||||
|
}
|
||||||
|
|
||||||
cid = nm_connection_get_id (NM_CONNECTION (connection));
|
return (NMSettingsConnection *) 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -106,7 +96,7 @@ read_connections (NMSystemConfigInterface *config)
|
|||||||
SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config);
|
SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config);
|
||||||
GDir *dir;
|
GDir *dir;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
const char *item, *cid;
|
const char *item;
|
||||||
|
|
||||||
dir = g_dir_open (KEYFILE_DIR, 0, &error);
|
dir = g_dir_open (KEYFILE_DIR, 0, &error);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
@@ -128,9 +118,10 @@ read_connections (NMSystemConfigInterface *config)
|
|||||||
full_path = g_build_filename (KEYFILE_DIR, item, NULL);
|
full_path = g_build_filename (KEYFILE_DIR, item, NULL);
|
||||||
PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, "parsing %s ... ", item);
|
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) {
|
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 {
|
} else {
|
||||||
PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, " error: %s",
|
PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, " error: %s",
|
||||||
(error && error->message) ? error->message : "(unknown)");
|
(error && error->message) ? error->message : "(unknown)");
|
||||||
@@ -408,8 +399,8 @@ add_connection (NMSystemConfigInterface *config,
|
|||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
|
|
||||||
/* Write it out first, then add the connection to our internal list */
|
/* 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)) {
|
if (nm_keyfile_plugin_write_connection (connection, NULL, &path, error)) {
|
||||||
added = _internal_new_connection (self, path, connection, NULL, error);
|
added = _internal_new_connection (self, path, connection, error);
|
||||||
g_free (path);
|
g_free (path);
|
||||||
}
|
}
|
||||||
return added;
|
return added;
|
||||||
|
@@ -693,7 +693,7 @@ test_write_wired_connection (void)
|
|||||||
/* Write out the connection */
|
/* Write out the connection */
|
||||||
owner_uid = geteuid ();
|
owner_uid = geteuid ();
|
||||||
owner_grp = getegid ();
|
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,
|
ASSERT (success == TRUE,
|
||||||
"connection-write", "failed to allocate write keyfile: %s",
|
"connection-write", "failed to allocate write keyfile: %s",
|
||||||
error ? error->message : "(none)");
|
error ? error->message : "(none)");
|
||||||
@@ -953,7 +953,7 @@ test_write_ip6_wired_connection (void)
|
|||||||
/* Write out the connection */
|
/* Write out the connection */
|
||||||
owner_uid = geteuid ();
|
owner_uid = geteuid ();
|
||||||
owner_grp = getegid ();
|
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,
|
ASSERT (success == TRUE,
|
||||||
"connection-write", "failed to allocate write keyfile: %s",
|
"connection-write", "failed to allocate write keyfile: %s",
|
||||||
error ? error->message : "(none)");
|
error ? error->message : "(none)");
|
||||||
@@ -1271,7 +1271,7 @@ test_write_wireless_connection (void)
|
|||||||
/* Write out the connection */
|
/* Write out the connection */
|
||||||
owner_uid = geteuid ();
|
owner_uid = geteuid ();
|
||||||
owner_grp = getegid ();
|
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,
|
ASSERT (success == TRUE,
|
||||||
"connection-write", "failed to allocate write keyfile: %s",
|
"connection-write", "failed to allocate write keyfile: %s",
|
||||||
error ? error->message : "(none)");
|
error ? error->message : "(none)");
|
||||||
@@ -1401,7 +1401,7 @@ test_write_string_ssid (void)
|
|||||||
/* Write out the connection */
|
/* Write out the connection */
|
||||||
owner_uid = geteuid ();
|
owner_uid = geteuid ();
|
||||||
owner_grp = getegid ();
|
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,
|
ASSERT (success == TRUE,
|
||||||
"connection-write", "failed to allocate write keyfile: %s",
|
"connection-write", "failed to allocate write keyfile: %s",
|
||||||
error ? error->message : "(none)");
|
error ? error->message : "(none)");
|
||||||
@@ -1699,7 +1699,7 @@ test_write_bt_dun_connection (void)
|
|||||||
/* Write out the connection */
|
/* Write out the connection */
|
||||||
owner_uid = geteuid ();
|
owner_uid = geteuid ();
|
||||||
owner_grp = getegid ();
|
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,
|
ASSERT (success == TRUE,
|
||||||
"connection-write", "failed to allocate write keyfile: %s",
|
"connection-write", "failed to allocate write keyfile: %s",
|
||||||
error ? error->message : "(none)");
|
error ? error->message : "(none)");
|
||||||
@@ -1961,7 +1961,7 @@ test_write_gsm_connection (void)
|
|||||||
/* Write out the connection */
|
/* Write out the connection */
|
||||||
owner_uid = geteuid ();
|
owner_uid = geteuid ();
|
||||||
owner_grp = getegid ();
|
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,
|
ASSERT (success == TRUE,
|
||||||
"connection-write", "failed to allocate write keyfile: %s",
|
"connection-write", "failed to allocate write keyfile: %s",
|
||||||
error ? error->message : "(none)");
|
error ? error->message : "(none)");
|
||||||
|
@@ -687,13 +687,14 @@ _writer_id_to_filename (const char *id)
|
|||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
static gboolean
|
||||||
nm_keyfile_plugin_write_connection (NMConnection *connection,
|
_internal_write_connection (NMConnection *connection,
|
||||||
const char *keyfile_dir,
|
const char *keyfile_dir,
|
||||||
uid_t owner_uid,
|
uid_t owner_uid,
|
||||||
pid_t owner_grp,
|
pid_t owner_grp,
|
||||||
char **out_path,
|
const char *existing_path,
|
||||||
GError **error)
|
char **out_path,
|
||||||
|
GError **error)
|
||||||
{
|
{
|
||||||
GKeyFile *key_file;
|
GKeyFile *key_file;
|
||||||
char *data;
|
char *data;
|
||||||
@@ -722,6 +723,8 @@ nm_keyfile_plugin_write_connection (NMConnection *connection,
|
|||||||
path = g_build_filename (keyfile_dir, filename, NULL);
|
path = g_build_filename (keyfile_dir, filename, NULL);
|
||||||
g_free (filename);
|
g_free (filename);
|
||||||
|
|
||||||
|
/* If the file already exists */
|
||||||
|
|
||||||
g_file_set_contents (path, data, len, error);
|
g_file_set_contents (path, data, len, error);
|
||||||
if (chown (path, owner_uid, owner_grp) < 0) {
|
if (chown (path, owner_uid, owner_grp) < 0) {
|
||||||
g_set_error (error, KEYFILE_PLUGIN_ERROR, 0,
|
g_set_error (error, KEYFILE_PLUGIN_ERROR, 0,
|
||||||
@@ -747,3 +750,34 @@ out:
|
|||||||
g_key_file_free (key_file);
|
g_key_file_free (key_file);
|
||||||
return success;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -27,10 +27,15 @@
|
|||||||
#include <nm-connection.h>
|
#include <nm-connection.h>
|
||||||
|
|
||||||
gboolean nm_keyfile_plugin_write_connection (NMConnection *connection,
|
gboolean nm_keyfile_plugin_write_connection (NMConnection *connection,
|
||||||
const char *keyfile_dir,
|
const char *existing_path,
|
||||||
uid_t owner_uid,
|
|
||||||
pid_t owner_grp,
|
|
||||||
char **out_path,
|
char **out_path,
|
||||||
GError **error);
|
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 */
|
#endif /* _KEYFILE_PLUGIN_WRITER_H */
|
||||||
|
Reference in New Issue
Block a user