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.
This commit is contained in:
Thomas Haller 2019-12-19 14:18:13 +01:00
parent 81e6fe963e
commit 8e212176b4
2 changed files with 41 additions and 0 deletions

View file

@ -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

View file

@ -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);