mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-08 11:19:16 +02:00
config: refactor nm_config_new() by extracting function _get_config_dir_files()
(cherry picked from commit ef57828442)
This commit is contained in:
parent
83edb5aee3
commit
cfc435be0b
1 changed files with 54 additions and 38 deletions
|
|
@ -567,6 +567,58 @@ read_base_config (GKeyFile *keyfile,
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
sort_asciibetically (gconstpointer a, gconstpointer b)
|
||||||
|
{
|
||||||
|
const char *s1 = *(const char **)a;
|
||||||
|
const char *s2 = *(const char **)b;
|
||||||
|
|
||||||
|
return strcmp (s1, s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static GPtrArray *
|
||||||
|
_get_config_dir_files (const char *config_main_file,
|
||||||
|
const char *config_dir,
|
||||||
|
char **out_config_description)
|
||||||
|
{
|
||||||
|
GFile *dir;
|
||||||
|
GFileEnumerator *direnum;
|
||||||
|
GFileInfo *info;
|
||||||
|
GPtrArray *confs;
|
||||||
|
GString *config_description;
|
||||||
|
const char *name;
|
||||||
|
|
||||||
|
g_return_val_if_fail (config_main_file, NULL);
|
||||||
|
g_return_val_if_fail (config_dir, NULL);
|
||||||
|
g_return_val_if_fail (out_config_description && !*out_config_description, NULL);
|
||||||
|
|
||||||
|
confs = g_ptr_array_new_with_free_func (g_free);
|
||||||
|
config_description = g_string_new (config_main_file);
|
||||||
|
dir = g_file_new_for_path (config_dir);
|
||||||
|
direnum = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, NULL);
|
||||||
|
if (direnum) {
|
||||||
|
while ((info = g_file_enumerator_next_file (direnum, NULL, NULL))) {
|
||||||
|
name = g_file_info_get_name (info);
|
||||||
|
if (g_str_has_suffix (name, ".conf")) {
|
||||||
|
g_ptr_array_add (confs, g_build_filename (config_dir, name, NULL));
|
||||||
|
if (confs->len == 1)
|
||||||
|
g_string_append (config_description, " and conf.d: ");
|
||||||
|
else
|
||||||
|
g_string_append (config_description, ", ");
|
||||||
|
g_string_append (config_description, name);
|
||||||
|
}
|
||||||
|
g_object_unref (info);
|
||||||
|
}
|
||||||
|
g_object_unref (direnum);
|
||||||
|
}
|
||||||
|
g_object_unref (dir);
|
||||||
|
|
||||||
|
g_ptr_array_sort (confs, sort_asciibetically);
|
||||||
|
|
||||||
|
*out_config_description = g_string_free (config_description, FALSE);
|
||||||
|
return confs;
|
||||||
|
}
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
@ -648,29 +700,15 @@ nm_config_setup (const NMConfigCmdLineOptions *cli, GError **error)
|
||||||
return singleton_instance;
|
return singleton_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
sort_asciibetically (gconstpointer a, gconstpointer b)
|
|
||||||
{
|
|
||||||
const char *s1 = *(const char **)a;
|
|
||||||
const char *s2 = *(const char **)b;
|
|
||||||
|
|
||||||
return strcmp (s1, s2);
|
|
||||||
}
|
|
||||||
|
|
||||||
NMConfig *
|
NMConfig *
|
||||||
nm_config_new (const NMConfigCmdLineOptions *cli, GError **error)
|
nm_config_new (const NMConfigCmdLineOptions *cli, GError **error)
|
||||||
{
|
{
|
||||||
NMConfigPrivate *priv = NULL;
|
NMConfigPrivate *priv = NULL;
|
||||||
GFile *dir;
|
|
||||||
GFileEnumerator *direnum;
|
|
||||||
GFileInfo *info;
|
|
||||||
GPtrArray *confs;
|
|
||||||
const char *name;
|
|
||||||
int i;
|
int i;
|
||||||
GString *config_description;
|
|
||||||
NMConfig *self;
|
NMConfig *self;
|
||||||
char *connectivity_uri, *connectivity_response;
|
char *connectivity_uri, *connectivity_response;
|
||||||
guint connectivity_interval;
|
guint connectivity_interval;
|
||||||
|
GPtrArray *confs;
|
||||||
|
|
||||||
self = NM_CONFIG (g_object_new (NM_TYPE_CONFIG, NULL));
|
self = NM_CONFIG (g_object_new (NM_TYPE_CONFIG, NULL));
|
||||||
priv = NM_CONFIG_GET_PRIVATE (self);
|
priv = NM_CONFIG_GET_PRIVATE (self);
|
||||||
|
|
@ -692,29 +730,7 @@ nm_config_new (const NMConfigCmdLineOptions *cli, GError **error)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
confs = g_ptr_array_new_with_free_func (g_free);
|
confs = _get_config_dir_files (priv->config_main_file, priv->config_dir, &priv->config_description);
|
||||||
config_description = g_string_new (priv->config_main_file);
|
|
||||||
dir = g_file_new_for_path (priv->config_dir);
|
|
||||||
direnum = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, NULL);
|
|
||||||
if (direnum) {
|
|
||||||
while ((info = g_file_enumerator_next_file (direnum, NULL, NULL))) {
|
|
||||||
name = g_file_info_get_name (info);
|
|
||||||
if (g_str_has_suffix (name, ".conf")) {
|
|
||||||
g_ptr_array_add (confs, g_build_filename (priv->config_dir, name, NULL));
|
|
||||||
if (confs->len == 1)
|
|
||||||
g_string_append (config_description, " and conf.d: ");
|
|
||||||
else
|
|
||||||
g_string_append (config_description, ", ");
|
|
||||||
g_string_append (config_description, name);
|
|
||||||
}
|
|
||||||
g_object_unref (info);
|
|
||||||
}
|
|
||||||
g_object_unref (direnum);
|
|
||||||
}
|
|
||||||
g_object_unref (dir);
|
|
||||||
|
|
||||||
g_ptr_array_sort (confs, sort_asciibetically);
|
|
||||||
priv->config_description = g_string_free (config_description, FALSE);
|
|
||||||
for (i = 0; i < confs->len; i++) {
|
for (i = 0; i < confs->len; i++) {
|
||||||
if (!read_config (priv->keyfile, confs->pdata[i], error)) {
|
if (!read_config (priv->keyfile, confs->pdata[i], error)) {
|
||||||
g_object_unref (self);
|
g_object_unref (self);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue