mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-05 06:50:17 +01:00
settings: refactor virtual commit_changes() function
Don't delegate so much to the virtual function commit_changes(). Calling the callback is not the task of the virtual function, because every implementation must do that. There are some minor changes in behavior for ifnet, where we now first setup the monitors and reload the parsers, before invoking the callback.
This commit is contained in:
parent
027229a4b0
commit
ede1e08ac1
5 changed files with 68 additions and 106 deletions
|
|
@ -640,42 +640,41 @@ nm_settings_connection_replace_and_commit (NMSettingsConnection *self,
|
|||
nm_settings_connection_commit_changes (self, commit_reason, callback, user_data);
|
||||
}
|
||||
|
||||
static void
|
||||
commit_changes (NMSettingsConnection *self,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
NMSettingsConnectionCommitFunc callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
/* Subclasses only call this function if the save was successful, so at
|
||||
* this point the connection is synced to disk and no longer unsaved.
|
||||
*/
|
||||
set_unsaved (self, FALSE);
|
||||
|
||||
g_object_ref (self);
|
||||
callback (self, NULL, user_data);
|
||||
g_object_unref (self);
|
||||
}
|
||||
|
||||
void
|
||||
nm_settings_connection_commit_changes (NMSettingsConnection *self,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
NMSettingsConnectionCommitFunc callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
gs_free_error GError *error = NULL;
|
||||
NMSettingsConnectionClass *klass;
|
||||
|
||||
g_return_if_fail (NM_IS_SETTINGS_CONNECTION (self));
|
||||
|
||||
if (NM_SETTINGS_CONNECTION_GET_CLASS (self)->commit_changes) {
|
||||
NM_SETTINGS_CONNECTION_GET_CLASS (self)->commit_changes (self,
|
||||
commit_reason,
|
||||
callback ? callback : ignore_cb,
|
||||
user_data);
|
||||
} else {
|
||||
GError *error = g_error_new (NM_SETTINGS_ERROR,
|
||||
NM_SETTINGS_ERROR_FAILED,
|
||||
"%s: %s:%d commit_changes() unimplemented", __func__, __FILE__, __LINE__);
|
||||
if (callback)
|
||||
callback (self, error, user_data);
|
||||
g_error_free (error);
|
||||
klass = NM_SETTINGS_CONNECTION_GET_CLASS (self);
|
||||
|
||||
if (!klass->commit_changes) {
|
||||
g_set_error (&error,
|
||||
NM_SETTINGS_ERROR,
|
||||
NM_SETTINGS_ERROR_FAILED,
|
||||
"writing settings not supported");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!klass->commit_changes (self,
|
||||
commit_reason,
|
||||
&error)) {
|
||||
nm_assert (error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
set_unsaved (self, FALSE);
|
||||
|
||||
out:
|
||||
if (callback) {
|
||||
g_object_ref (self);
|
||||
callback (self, error, user_data);
|
||||
g_object_unref (self);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2898,7 +2897,6 @@ nm_settings_connection_class_init (NMSettingsConnectionClass *class)
|
|||
object_class->get_property = get_property;
|
||||
object_class->set_property = set_property;
|
||||
|
||||
class->commit_changes = commit_changes;
|
||||
class->delete = do_delete;
|
||||
class->supports_secrets = supports_secrets;
|
||||
|
||||
|
|
|
|||
|
|
@ -115,10 +115,9 @@ struct _NMSettingsConnectionClass {
|
|||
gboolean (*can_commit) (NMSettingsConnection *self,
|
||||
GError **error);
|
||||
|
||||
void (*commit_changes) (NMSettingsConnection *self,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
NMSettingsConnectionCommitFunc callback,
|
||||
gpointer user_data);
|
||||
gboolean (*commit_changes) (NMSettingsConnection *self,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
GError **error);
|
||||
|
||||
void (*delete) (NMSettingsConnection *self,
|
||||
NMSettingsConnectionDeleteFunc callback,
|
||||
|
|
|
|||
|
|
@ -323,52 +323,40 @@ can_commit (NMSettingsConnection *connection,
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
static gboolean
|
||||
commit_changes (NMSettingsConnection *connection,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
NMSettingsConnectionCommitFunc callback,
|
||||
gpointer user_data)
|
||||
GError **error)
|
||||
{
|
||||
GError *error = NULL;
|
||||
gboolean success = FALSE;
|
||||
char *ifcfg_path = NULL;
|
||||
const char *filename;
|
||||
|
||||
filename = nm_settings_connection_get_filename (connection);
|
||||
if (filename) {
|
||||
success = writer_update_connection (NM_CONNECTION (connection),
|
||||
IFCFG_DIR,
|
||||
filename,
|
||||
NULL,
|
||||
NULL,
|
||||
&error);
|
||||
} else {
|
||||
success = writer_new_connection (NM_CONNECTION (connection),
|
||||
IFCFG_DIR,
|
||||
&ifcfg_path,
|
||||
NULL,
|
||||
NULL,
|
||||
&error);
|
||||
if (success) {
|
||||
nm_settings_connection_set_filename (connection, ifcfg_path);
|
||||
g_free (ifcfg_path);
|
||||
}
|
||||
if (!filename) {
|
||||
gs_free char *ifcfg_path = NULL;
|
||||
|
||||
if (!writer_new_connection (NM_CONNECTION (connection),
|
||||
IFCFG_DIR,
|
||||
&ifcfg_path,
|
||||
NULL,
|
||||
NULL,
|
||||
error))
|
||||
return FALSE;
|
||||
nm_settings_connection_set_filename (connection, ifcfg_path);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
/* Chain up to parent to handle success */
|
||||
NM_SETTINGS_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->commit_changes (connection, commit_reason, callback, user_data);
|
||||
} else {
|
||||
/* Otherwise immediate error */
|
||||
callback (connection, error, user_data);
|
||||
g_error_free (error);
|
||||
}
|
||||
return writer_update_connection (NM_CONNECTION (connection),
|
||||
IFCFG_DIR,
|
||||
filename,
|
||||
NULL,
|
||||
NULL,
|
||||
error);
|
||||
}
|
||||
|
||||
static void
|
||||
do_delete (NMSettingsConnection *connection,
|
||||
NMSettingsConnectionDeleteFunc callback,
|
||||
gpointer user_data)
|
||||
NMSettingsConnectionDeleteFunc callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE ((NMIfcfgConnection *) connection);
|
||||
const char *filename;
|
||||
|
|
|
|||
|
|
@ -74,58 +74,45 @@ nm_ifnet_connection_get_conn_name (NMIfnetConnection *connection)
|
|||
return NM_IFNET_CONNECTION_GET_PRIVATE (connection)->conn_name;
|
||||
}
|
||||
|
||||
static void
|
||||
static gboolean
|
||||
commit_changes (NMSettingsConnection *connection,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
NMSettingsConnectionCommitFunc callback,
|
||||
gpointer user_data)
|
||||
GError **error)
|
||||
{
|
||||
GError *error = NULL;
|
||||
NMIfnetConnectionPrivate *priv = NM_IFNET_CONNECTION_GET_PRIVATE ((NMIfnetConnection *) connection);
|
||||
gchar *new_name = NULL;
|
||||
char *new_name = NULL;
|
||||
gboolean success = FALSE;
|
||||
|
||||
g_signal_emit (connection, signals[IFNET_CANCEL_MONITORS], 0);
|
||||
|
||||
if (priv->conn_name) {
|
||||
/* Existing connection; update it */
|
||||
success = ifnet_update_parsers_by_connection (NM_CONNECTION (connection),
|
||||
priv->conn_name,
|
||||
CONF_NET_FILE,
|
||||
WPA_SUPPLICANT_CONF,
|
||||
&new_name,
|
||||
NULL,
|
||||
&error);
|
||||
error);
|
||||
} else {
|
||||
/* New connection, add it */
|
||||
success = ifnet_add_new_connection (NM_CONNECTION (connection),
|
||||
CONF_NET_FILE,
|
||||
WPA_SUPPLICANT_CONF,
|
||||
&new_name,
|
||||
NULL,
|
||||
&error);
|
||||
if (success)
|
||||
reload_parsers ();
|
||||
error);
|
||||
}
|
||||
|
||||
g_assert (!!success == (new_name != NULL));
|
||||
if (success) {
|
||||
/* update connection name */
|
||||
g_assert (new_name);
|
||||
g_free (priv->conn_name);
|
||||
priv->conn_name = new_name;
|
||||
|
||||
NM_SETTINGS_CONNECTION_CLASS (nm_ifnet_connection_parent_class)->commit_changes (connection, commit_reason, callback, user_data);
|
||||
nm_log_info (LOGD_SETTINGS, "Successfully updated %s", priv->conn_name);
|
||||
} else {
|
||||
nm_log_warn (LOGD_SETTINGS, "Failed to update %s",
|
||||
priv->conn_name ? priv->conn_name :
|
||||
nm_connection_get_id (NM_CONNECTION (connection)));
|
||||
reload_parsers ();
|
||||
callback (connection, error, user_data);
|
||||
g_error_free (error);
|
||||
}
|
||||
|
||||
reload_parsers ();
|
||||
|
||||
g_signal_emit (connection, signals[IFNET_SETUP_MONITORS], 0);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
|||
|
|
@ -50,14 +50,12 @@ G_DEFINE_TYPE (NMSKeyfileConnection, nms_keyfile_connection, NM_TYPE_SETTINGS_CO
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
static void
|
||||
static gboolean
|
||||
commit_changes (NMSettingsConnection *connection,
|
||||
NMSettingsConnectionCommitReason commit_reason,
|
||||
NMSettingsConnectionCommitFunc callback,
|
||||
gpointer user_data)
|
||||
GError **error)
|
||||
{
|
||||
char *path = NULL;
|
||||
GError *error = NULL;
|
||||
gs_free char *path = NULL;
|
||||
gs_unref_object NMConnection *reread = NULL;
|
||||
gboolean reread_same = FALSE;
|
||||
|
||||
|
|
@ -68,11 +66,8 @@ commit_changes (NMSettingsConnection *connection,
|
|||
&path,
|
||||
&reread,
|
||||
&reread_same,
|
||||
&error)) {
|
||||
callback (connection, error, user_data);
|
||||
g_clear_error (&error);
|
||||
return;
|
||||
}
|
||||
error))
|
||||
return FALSE;
|
||||
|
||||
/* Update the filename if it changed */
|
||||
if ( path
|
||||
|
|
@ -105,12 +100,7 @@ commit_changes (NMSettingsConnection *connection,
|
|||
}
|
||||
}
|
||||
|
||||
g_free (path);
|
||||
|
||||
NM_SETTINGS_CONNECTION_CLASS (nms_keyfile_connection_parent_class)->commit_changes (connection,
|
||||
commit_reason,
|
||||
callback,
|
||||
user_data);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue