summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAna Cabral <acabral@redhat.com>2022-02-10 09:08:45 +0100
committerAna Cabral <acabral@redhat.com>2022-02-11 12:26:01 +0100
commit20aa8d049ca1ac5074e491ab167e440115ddaaa7 (patch)
treedc43c3e45d3df0bd471a318902e02d8145233049
parent43eb4b3bfb646228cf22677bb3d1968b62cd6636 (diff)
downloadNetworkManager-20aa8d049ca1ac5074e491ab167e440115ddaaa7.tar.gz
keyfile: write ethernet group always on the top of the file
-rw-r--r--src/libnm-core-impl/nm-keyfile-utils.c19
-rw-r--r--src/libnm-core-impl/nm-keyfile.c18
-rw-r--r--src/libnm-core-intern/nm-keyfile-utils.h2
3 files changed, 35 insertions, 4 deletions
diff --git a/src/libnm-core-impl/nm-keyfile-utils.c b/src/libnm-core-impl/nm-keyfile-utils.c
index 0e2e2d45b7..c599aefa7b 100644
--- a/src/libnm-core-impl/nm-keyfile-utils.c
+++ b/src/libnm-core-impl/nm-keyfile-utils.c
@@ -682,3 +682,22 @@ nm_keyfile_key_decode(const char *key, char **out_to_free)
#endif
return name;
}
+
+void
+nm_keyfile_add_group(GKeyFile *keyfile, const char *group)
+{
+ nm_assert(keyfile);
+ nm_assert(group);
+
+ /* You can only call this function if the group doesn't exist yet.
+ * Because, we are about to add a dummy key, so we would have to
+ * be sure that the key doesn't exist. */
+ nm_assert(!g_key_file_has_group(keyfile, group));
+
+ /* Ensure the group is present.
+ * There is no API for that, so add and remove a dummy key.
+ * For a profile it matters whether a setting is present or not,
+ * and we need to ensure that we persist the presence of the setting to keyfile*/
+ g_key_file_set_value(keyfile, group, ".X", "1");
+ g_key_file_remove_key(keyfile, group, ".X", NULL);
+}
diff --git a/src/libnm-core-impl/nm-keyfile.c b/src/libnm-core-impl/nm-keyfile.c
index 4ff9b5827a..55d853a063 100644
--- a/src/libnm-core-impl/nm-keyfile.c
+++ b/src/libnm-core-impl/nm-keyfile.c
@@ -2305,8 +2305,21 @@ wired_s390_options_writer_full(KeyfileWriterInfo *info,
{
NMSettingWired *s_wired = NM_SETTING_WIRED(setting);
guint i, n;
+ const char *setting_alias;
n = nm_setting_wired_get_num_s390_options(s_wired);
+ if (n == 0)
+ return;
+
+ setting_alias = nm_keyfile_plugin_get_alias_for_setting_name(NM_SETTING_WIRED_SETTING_NAME);
+ if (!g_key_file_has_group(info->keyfile, NM_SETTING_WIRED_SETTING_NAME)
+ && !g_key_file_has_group(info->keyfile, setting_alias)) {
+ /* groups in the keyfile are ordered. When we are about to add [ethernet-s390-options],
+ * we want to also have an [ethernet] group, first. */
+
+ nm_keyfile_add_group(info->keyfile, setting_alias ?: NM_SETTING_WIRED_SETTING_NAME);
+ }
+
for (i = 0; i < n; i++) {
gs_free char *key_to_free = NULL;
const char *opt_key;
@@ -4190,10 +4203,7 @@ nm_keyfile_write(NMConnection *connection,
|| g_key_file_has_group(info.keyfile, setting_name)) {
/* we have a section for the setting. Nothing to do. */
} else {
- /* ensure the group is present. There is no API for that, so add and remove
- * a dummy key. */
- g_key_file_set_value(info.keyfile, setting_alias ?: setting_name, ".X", "1");
- g_key_file_remove_key(info.keyfile, setting_alias ?: setting_name, ".X", NULL);
+ nm_keyfile_add_group(info.keyfile, setting_alias ?: setting_name);
}
if (NM_IS_SETTING_WIREGUARD(setting)) {
diff --git a/src/libnm-core-intern/nm-keyfile-utils.h b/src/libnm-core-intern/nm-keyfile-utils.h
index 45741646b7..f79f020f67 100644
--- a/src/libnm-core-intern/nm-keyfile-utils.h
+++ b/src/libnm-core-intern/nm-keyfile-utils.h
@@ -92,4 +92,6 @@ const char *nm_keyfile_key_encode(const char *name, char **out_to_free);
const char *nm_keyfile_key_decode(const char *key, char **out_to_free);
+void nm_keyfile_add_group(GKeyFile *keyfile, const char *group);
+
#endif /* __NM_KEYFILE_UTILS_H__ */