From 8e212176b464b9ef84aeadaa37de675d6a7510f2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 19 Dec 2019 14:18:13 +0100 Subject: [PATCH] ifcfg-rh: add svUnsetDirtyWellknown() function Helper function to remove all variables that are still dirty (not visited) and well-known. Also add svWriteFileWithoutDirtyWellknown() to clear the lines before persisting to disk. --- src/settings/plugins/ifcfg-rh/shvar.c | 33 +++++++++++++++++++++++++++ src/settings/plugins/ifcfg-rh/shvar.h | 8 +++++++ 2 files changed, 41 insertions(+) diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index 7da0666814..c39b542571 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -1253,6 +1253,39 @@ svUnsetAll (shvarFile *s, SvKeyType match_key_type) return changed; } +gboolean +svUnsetDirtyWellknown (shvarFile *s, NMTernary new_dirty_value) +{ + shvarLine *line; + gboolean changed = FALSE; + + g_return_val_if_fail (s, FALSE); + + c_list_for_each_entry (line, &s->lst_head, lst) { + const NMSIfcfgKeyTypeInfo *ti; + + ASSERT_shvarLine (line); + + if ( line->dirty + && line->key + && line->line + && (ti = nms_ifcfg_rh_utils_is_well_known_key (line->key)) + && !NM_FLAGS_HAS (ti->key_flags, NMS_IFCFG_KEY_TYPE_KEEP_WHEN_DIRTY)) { + if (nm_clear_g_free (&line->line)) { + ASSERT_shvarLine (line); + changed = TRUE; + } + } + + if (new_dirty_value != NM_TERNARY_DEFAULT) + line->dirty = (new_dirty_value != NM_TERNARY_FALSE); + } + + if (changed) + s->modified = TRUE; + return changed; +} + /* Same as svSetValueStr() but it preserves empty @value -- contrary to * svSetValueStr() for which "" effectively means to remove the value. */ gboolean diff --git a/src/settings/plugins/ifcfg-rh/shvar.h b/src/settings/plugins/ifcfg-rh/shvar.h index c3bbababa8..bfee795992 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.h +++ b/src/settings/plugins/ifcfg-rh/shvar.h @@ -79,6 +79,7 @@ gboolean svSetValueEnum (shvarFile *s, const char *key, GType gtype, int value); gboolean svUnsetValue (shvarFile *s, const char *key); gboolean svUnsetAll (shvarFile *s, SvKeyType match_key_type); +gboolean svUnsetDirtyWellknown (shvarFile *s, NMTernary new_dirty_value); /* Write the current contents iff modified. Returns FALSE on error * and TRUE on success. Do not write if no values have been modified. @@ -88,6 +89,13 @@ gboolean svUnsetAll (shvarFile *s, SvKeyType match_key_type); */ gboolean svWriteFile (shvarFile *s, int mode, GError **error); +static inline gboolean +svWriteFileWithoutDirtyWellknown (shvarFile *s, int mode, GError **error) +{ + svUnsetDirtyWellknown (s, NM_TERNARY_FALSE); + return svWriteFile (s, mode, error); +} + /* Close the file descriptor (if open) and free the shvarFile. */ void svCloseFile (shvarFile *s);