core: cleanup nm_config_device_state_prune_unseen() and accept NMPlatform for skipping pruning

This commit is contained in:
Thomas Haller
2020-03-04 13:21:48 +01:00
parent 627b543a37
commit ad9e748816
3 changed files with 29 additions and 18 deletions

View File

@@ -2299,6 +2299,8 @@ _config_device_state_data_new (int ifindex, GKeyFile *kf)
return device_state; return device_state;
} }
#define DEVICE_STATE_FILENAME_LEN_MAX 60
/** /**
* nm_config_device_state_load: * nm_config_device_state_load:
* @ifindex: the ifindex for which the state is to load * @ifindex: the ifindex for which the state is to load
@@ -2310,7 +2312,7 @@ NMConfigDeviceStateData *
nm_config_device_state_load (int ifindex) nm_config_device_state_load (int ifindex)
{ {
NMConfigDeviceStateData *device_state; NMConfigDeviceStateData *device_state;
char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR) + 60]; char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + DEVICE_STATE_FILENAME_LEN_MAX + 1];
gs_unref_keyfile GKeyFile *kf = NULL; gs_unref_keyfile GKeyFile *kf = NULL;
const char *nm_owned_str; const char *nm_owned_str;
@@ -2394,7 +2396,7 @@ nm_config_device_state_write (int ifindex,
const char *next_server, const char *next_server,
const char *root_path) const char *root_path)
{ {
char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR) + 60]; char path[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + DEVICE_STATE_FILENAME_LEN_MAX + 1];
GError *local = NULL; GError *local = NULL;
gs_unref_keyfile GKeyFile *kf = NULL; gs_unref_keyfile GKeyFile *kf = NULL;
@@ -2477,35 +2479,43 @@ nm_config_device_state_write (int ifindex,
} }
void void
nm_config_device_state_prune_unseen (GHashTable *seen_ifindexes) nm_config_device_state_prune_unseen (GHashTable *preserve_ifindexes,
NMPlatform *preserve_in_platform)
{ {
GDir *dir; GDir *dir;
const char *fn; const char *fn;
int ifindex; char buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + DEVICE_STATE_FILENAME_LEN_MAX + 1] = NM_CONFIG_DEVICE_STATE_DIR"/";
gsize fn_len;
char buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/") + 30 + 3] = NM_CONFIG_DEVICE_STATE_DIR"/";
char *buf_p = &buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/")]; char *buf_p = &buf[NM_STRLEN (NM_CONFIG_DEVICE_STATE_DIR"/")];
g_return_if_fail (seen_ifindexes);
dir = g_dir_open (NM_CONFIG_DEVICE_STATE_DIR, 0, NULL); dir = g_dir_open (NM_CONFIG_DEVICE_STATE_DIR, 0, NULL);
if (!dir) if (!dir)
return; return;
while ((fn = g_dir_read_name (dir))) { while ((fn = g_dir_read_name (dir))) {
int ifindex;
gsize fn_len;
ifindex = _device_state_parse_filename (fn); ifindex = _device_state_parse_filename (fn);
if (ifindex <= 0) if (ifindex <= 0)
continue; continue;
if (g_hash_table_contains (seen_ifindexes, GINT_TO_POINTER (ifindex)))
if ( preserve_ifindexes
&& g_hash_table_contains (preserve_ifindexes, GINT_TO_POINTER (ifindex)))
continue; continue;
fn_len = strlen (fn) + 1; if ( preserve_in_platform
&& nm_platform_link_get (preserve_in_platform, ifindex))
continue;
fn_len = strlen (fn);
nm_assert (fn_len > 0);
nm_assert (&buf_p[fn_len] < &buf[G_N_ELEMENTS (buf)]); nm_assert (&buf_p[fn_len] < &buf[G_N_ELEMENTS (buf)]);
memcpy (buf_p, fn, fn_len); memcpy (buf_p, fn, fn_len + 1u);
nm_assert (({ nm_assert (({
char bb[30]; char bb[30];
nm_sprintf_buf (bb, "%d", ifindex);
nm_streq0 (bb, buf_p); nm_streq0 (nm_sprintf_buf (bb, "%d", ifindex),
buf_p);
})); }));
_LOGT ("device-state: prune #%d (%s)", ifindex, buf); _LOGT ("device-state: prune #%d (%s)", ifindex, buf);
(void) unlink (buf); (void) unlink (buf);

View File

@@ -258,7 +258,8 @@ gboolean nm_config_device_state_write (int ifindex,
const char *next_server, const char *next_server,
const char *root_path); const char *root_path);
void nm_config_device_state_prune_unseen (GHashTable *seen_ifindexes); void nm_config_device_state_prune_unseen (GHashTable *preserve_ifindexes,
NMPlatform *preserve_in_platform);
const GHashTable *nm_config_device_state_get_all (NMConfig *self); const GHashTable *nm_config_device_state_get_all (NMConfig *self);
const NMConfigDeviceStateData *nm_config_device_state_get (NMConfig *self, const NMConfigDeviceStateData *nm_config_device_state_get (NMConfig *self,

View File

@@ -6555,19 +6555,19 @@ void
nm_manager_write_device_state_all (NMManager *self) nm_manager_write_device_state_all (NMManager *self)
{ {
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
gs_unref_hashtable GHashTable *seen_ifindexes = NULL; gs_unref_hashtable GHashTable *preserve_ifindexes = NULL;
NMDevice *device; NMDevice *device;
seen_ifindexes = g_hash_table_new (nm_direct_hash, NULL); preserve_ifindexes = g_hash_table_new (nm_direct_hash, NULL);
c_list_for_each_entry (device, &priv->devices_lst_head, devices_lst) { c_list_for_each_entry (device, &priv->devices_lst_head, devices_lst) {
if (nm_manager_write_device_state (self, device)) { if (nm_manager_write_device_state (self, device)) {
g_hash_table_add (seen_ifindexes, g_hash_table_add (preserve_ifindexes,
GINT_TO_POINTER (nm_device_get_ip_ifindex (device))); GINT_TO_POINTER (nm_device_get_ip_ifindex (device)));
} }
} }
nm_config_device_state_prune_unseen (seen_ifindexes); nm_config_device_state_prune_unseen (preserve_ifindexes, NULL);
} }
static gboolean static gboolean