settings: refactor replace_and_commit()

The virtual function replace_and_commit() had only one implementation: ifcfg-rh.

Refactor the code, to delegate less. That is, the main part of
replace-and-commit is not delegated to a virtual function.
Now, the virtual function is only a pre-check hook, so that
the ifcfg-rh implementation can abort the function.

There are no functional changes.
This commit is contained in:
Thomas Haller 2017-10-19 08:24:00 +02:00
parent 0a8822ce9b
commit 027229a4b0
3 changed files with 37 additions and 51 deletions

View file

@ -604,44 +604,40 @@ ignore_cb (NMSettingsConnection *self,
{
}
/* Replaces the settings in this connection with those in 'new_connection'. If
* any changes are made, commits them to permanent storage and to any other
* subsystems watching this connection. Before returning, 'callback' is run
* with the given 'user_data' along with any errors encountered.
*/
static void
replace_and_commit (NMSettingsConnection *self,
NMConnection *new_connection,
NMSettingsConnectionCommitFunc callback,
gpointer user_data)
{
GError *error = NULL;
NMSettingsConnectionCommitReason commit_reason = NM_SETTINGS_CONNECTION_COMMIT_REASON_USER_ACTION;
if (g_strcmp0 (nm_connection_get_id (NM_CONNECTION (self)),
nm_connection_get_id (new_connection)) != 0)
commit_reason |= NM_SETTINGS_CONNECTION_COMMIT_REASON_ID_CHANGED;
if (nm_settings_connection_replace_settings (self, new_connection, TRUE, "replace-and-commit-disk", &error))
nm_settings_connection_commit_changes (self, commit_reason, callback, user_data);
else {
g_assert (error);
if (callback)
callback (self, error, user_data);
g_clear_error (&error);
}
}
void
nm_settings_connection_replace_and_commit (NMSettingsConnection *self,
NMConnection *new_connection,
NMSettingsConnectionCommitFunc callback,
gpointer user_data)
{
gs_free_error GError *error = NULL;
NMSettingsConnectionClass *klass;
NMSettingsConnectionCommitReason commit_reason = NM_SETTINGS_CONNECTION_COMMIT_REASON_USER_ACTION;
g_return_if_fail (NM_IS_SETTINGS_CONNECTION (self));
g_return_if_fail (NM_IS_CONNECTION (new_connection));
NM_SETTINGS_CONNECTION_GET_CLASS (self)->replace_and_commit (self, new_connection, callback, user_data);
klass = NM_SETTINGS_CONNECTION_GET_CLASS (self);
if (nm_streq0 (nm_connection_get_id (NM_CONNECTION (self)),
nm_connection_get_id (new_connection)))
commit_reason |= NM_SETTINGS_CONNECTION_COMMIT_REASON_ID_CHANGED;
if (klass->can_commit) {
if (!klass->can_commit (self, &error)) {
if (callback)
callback (self, error, user_data);
return;
}
}
if (!nm_settings_connection_replace_settings (self, new_connection, TRUE, "replace-and-commit-disk", &error)) {
if (callback)
callback (self, error, user_data);
return;
}
nm_settings_connection_commit_changes (self, commit_reason, callback, user_data);
}
static void
@ -2902,7 +2898,6 @@ nm_settings_connection_class_init (NMSettingsConnectionClass *class)
object_class->get_property = get_property;
object_class->set_property = set_property;
class->replace_and_commit = replace_and_commit;
class->commit_changes = commit_changes;
class->delete = do_delete;
class->supports_secrets = supports_secrets;

View file

@ -112,11 +112,8 @@ struct _NMSettingsConnection {
struct _NMSettingsConnectionClass {
NMExportedObjectClass parent;
/* virtual methods */
void (*replace_and_commit) (NMSettingsConnection *self,
NMConnection *new_connection,
NMSettingsConnectionCommitFunc callback,
gpointer user_data);
gboolean (*can_commit) (NMSettingsConnection *self,
GError **error);
void (*commit_changes) (NMSettingsConnection *self,
NMSettingsConnectionCommitReason commit_reason,

View file

@ -306,27 +306,21 @@ nm_ifcfg_connection_get_unrecognized_spec (NMIfcfgConnection *self)
return NM_IFCFG_CONNECTION_GET_PRIVATE (self)->unrecognized_spec;
}
static void
replace_and_commit (NMSettingsConnection *connection,
NMConnection *new_connection,
NMSettingsConnectionCommitFunc callback,
gpointer user_data)
static gboolean
can_commit (NMSettingsConnection *connection,
GError **error)
{
const char *filename;
GError *error = NULL;
filename = nm_settings_connection_get_filename (connection);
if (filename && utils_has_complex_routes (filename)) {
if (callback) {
error = g_error_new_literal (NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
"Cannot modify a connection that has an associated 'rule-' or 'rule6-' file");
callback (connection, error, user_data);
g_clear_error (&error);
}
return;
if ( filename
&& utils_has_complex_routes (filename)) {
g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
"Cannot modify a connection that has an associated 'rule-' or 'rule6-' file");
return FALSE;
}
NM_SETTINGS_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->replace_and_commit (connection, new_connection, callback, user_data);
return TRUE;
}
static void
@ -530,7 +524,7 @@ nm_ifcfg_connection_class_init (NMIfcfgConnectionClass *ifcfg_connection_class)
object_class->dispose = dispose;
settings_class->delete = do_delete;
settings_class->replace_and_commit = replace_and_commit;
settings_class->can_commit = can_commit;
settings_class->commit_changes = commit_changes;
obj_properties[PROP_UNMANAGED_SPEC] =