keyfile: add option to nms_keyfile_writer_connection() to follow the preferred name

There was already a force_rename argument to nms_keyfile_writer_connection(), which
-- if TRUE -- means to always rename the file, if it exists.

What we also want, is to follow the change of a connection.id. So we don't want
to force a rename, if we already use the preferred name, but we also want to rename
otherwise.

Extend the boolean "force_rename" argument to a NMTernary, where NM_TERNARY_DEFAULT
now means to follow the preferred name.
This commit is contained in:
Thomas Haller 2023-06-26 22:17:21 +02:00
parent cfe2cede12
commit c1f2616618
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 12 additions and 7 deletions

View file

@ -922,7 +922,7 @@ nms_keyfile_plugin_update_connection(NMSKeyfilePlugin *self,
_get_plugin_dir(priv),
previous_filename,
FALSE,
force_rename,
!!force_rename,
nm_sett_util_allow_filename_cb,
NM_SETT_UTIL_ALLOW_FILENAME_DATA(&priv->storages, previous_filename),
&full_filename,

View file

@ -195,7 +195,7 @@ _internal_write_connection(NMConnection *connection,
pid_t owner_grp,
const char *existing_path,
gboolean existing_path_read_only,
gboolean force_rename,
NMTernary force_rename,
NMSKeyfileWriterAllowFilenameCb allow_filename_cb,
gpointer allow_filename_user_data,
char **out_path,
@ -212,6 +212,7 @@ _internal_write_connection(NMConnection *connection,
gs_free_error GError *local_err = NULL;
int errsv;
gboolean rename;
gboolean rename_follow;
int i_path;
gs_unref_object NMConnection *reread = NULL;
gboolean reread_same = FALSE;
@ -223,8 +224,12 @@ _internal_write_connection(NMConnection *connection,
nm_assert(!shadowed_owned || shadowed_storage);
rename = force_rename || existing_path_read_only
|| (existing_path && !nm_utils_file_is_in_path(existing_path, keyfile_dir));
rename = existing_path_read_only
|| (existing_path && !nm_utils_file_is_in_path(existing_path, keyfile_dir))
|| force_rename == NM_TERNARY_TRUE;
/* Follow the connection.id upon change. */
rename_follow = !rename && existing_path && force_rename == NM_TERNARY_DEFAULT;
id = nm_connection_get_id(connection);
nm_assert(id && *id);
@ -283,7 +288,7 @@ _internal_write_connection(NMConnection *connection,
gboolean is_existing_path;
if (i_path == -2) {
if (!existing_path || rename)
if (!existing_path || rename || rename_follow)
continue;
path_candidate = g_strdup(existing_path);
} else if (i_path == -1) {
@ -427,7 +432,7 @@ nms_keyfile_writer_connection(NMConnection *connection,
const char *profile_dir,
const char *existing_path,
gboolean existing_path_read_only,
gboolean force_rename,
NMTernary force_rename,
NMSKeyfileWriterAllowFilenameCb allow_filename_cb,
gpointer allow_filename_user_data,
char **out_path,

View file

@ -22,7 +22,7 @@ gboolean nms_keyfile_writer_connection(NMConnection *connectio
const char *profile_dir,
const char *existing_path,
gboolean existing_path_read_only,
gboolean force_rename,
NMTernary force_rename,
NMSKeyfileWriterAllowFilenameCb allow_filename_cb,
gpointer allow_filename_user_data,
char **out_path,