ifcfg-rh: add allow_filename_cb() argument to write-ifcfg-rh function

The function determines the filename automatically, but we
need to blacklist certain names.

That is, because NetworkManager keeps a list of loaded files
in memory. When writing a new file, we really want to choose
a filename that is not yet taken. For that we must not only
consider files on disk, but also files that existed on the last
time of loading.
This commit is contained in:
Thomas Haller
2019-06-24 10:43:13 +02:00
parent e5b21344c5
commit e36cf1e890
5 changed files with 39 additions and 20 deletions

View File

@@ -206,6 +206,8 @@ commit_changes (NMSettingsConnection *connection,
if (!nms_ifcfg_rh_writer_write_connection (new_connection,
IFCFG_DIR,
filename,
NULL,
NULL,
&ifcfg_path,
&reread,
&reread_same,

View File

@@ -594,7 +594,7 @@ add_connection (NMSettingsPlugin *config,
gs_unref_object NMConnection *reread = NULL;
if (save_to_disk) {
if (!nms_ifcfg_rh_writer_write_connection (connection, IFCFG_DIR, NULL, &path, &reread, NULL, error))
if (!nms_ifcfg_rh_writer_write_connection (connection, IFCFG_DIR, NULL, NULL, NULL, &path, &reread, NULL, error))
return NULL;
} else {
if (!nms_ifcfg_rh_writer_can_write_connection (connection, error))

View File

@@ -3024,6 +3024,8 @@ static gboolean
do_write_construct (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
NMSIfcfgRHWriterAllowFilenameCb allow_filename_cb,
gpointer allow_filename_user_data,
shvarFile **out_ifcfg,
GHashTable **out_blobs,
GHashTable **out_secrets,
@@ -3067,30 +3069,31 @@ do_write_construct (NMConnection *connection,
ifcfg_name = g_strdup (filename);
} else if (ifcfg_dir) {
char *escaped;
gs_free char *escaped = NULL;
int i_path;
escaped = escape_id (nm_setting_connection_get_id (s_con));
ifcfg_name = g_strdup_printf ("%s/ifcfg-%s", ifcfg_dir, escaped);
/* If a file with this path already exists then we need another name.
* Multiple connections can have the same ID (ie if two connections with
* the same ID are visible to different users) but of course can't have
* the same path.
*/
if (g_file_test (ifcfg_name, G_FILE_TEST_EXISTS)) {
guint32 idx = 0;
for (i_path = 0; i_path < 10000; i_path++) {
gs_free char *path_candidate = NULL;
nm_clear_g_free (&ifcfg_name);
while (idx++ < 500) {
ifcfg_name = g_strdup_printf ("%s/ifcfg-%s-%u", ifcfg_dir, escaped, idx);
if (g_file_test (ifcfg_name, G_FILE_TEST_EXISTS) == FALSE)
break;
nm_clear_g_free (&ifcfg_name);
}
if (i_path == 0)
path_candidate = g_strdup_printf ("%s/ifcfg-%s", ifcfg_dir, escaped);
else
path_candidate = g_strdup_printf ("%s/ifcfg-%s-%d", ifcfg_dir, escaped, i_path);
if ( allow_filename_cb
&& !allow_filename_cb (path_candidate, allow_filename_user_data))
continue;
if (g_file_test (path_candidate, G_FILE_TEST_EXISTS))
continue;
ifcfg_name = g_steal_pointer (&path_candidate);
break;
}
g_free (escaped);
if (ifcfg_name == NULL) {
if (!ifcfg_name) {
g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
"Failed to find usable ifcfg file name");
return FALSE;
@@ -3377,6 +3380,8 @@ gboolean
nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
NMSIfcfgRHWriterAllowFilenameCb allow_filename_cb,
gpointer allow_filename_user_data,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,
@@ -3396,6 +3401,8 @@ nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
if (!do_write_construct (connection,
ifcfg_dir,
filename,
allow_filename_cb,
allow_filename_user_data,
&ifcfg,
&blobs,
&secrets,

View File

@@ -22,12 +22,18 @@
#include "nm-connection.h"
typedef gboolean (*NMSIfcfgRHWriterAllowFilenameCb) (const char *check_filename,
gpointer allow_filename_user_data);
gboolean nms_ifcfg_rh_writer_can_write_connection (NMConnection *connection,
GError **error);
gboolean nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
NMSIfcfgRHWriterAllowFilenameCb allow_filename_cb,
gpointer allow_filename_user_data,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,

View File

@@ -238,7 +238,7 @@ _assert_expected_content (NMConnection *connection, const char *filename, const
g_assert (_ifcfg_dir && _ifcfg_dir[0]); \
g_assert (_filename && _filename[0]); \
\
_success = nms_ifcfg_rh_writer_write_connection (_connection, _ifcfg_dir, _filename, NULL, _out_reread, _out_reread_same, &_error); \
_success = nms_ifcfg_rh_writer_write_connection (_connection, _ifcfg_dir, _filename, NULL, NULL, NULL, _out_reread, _out_reread_same, &_error); \
nmtst_assert_success (_success, _error); \
_assert_expected_content (_connection, _filename, _expected); \
} G_STMT_END
@@ -319,6 +319,8 @@ _writer_new_connection_reread (NMConnection *connection,
success = nms_ifcfg_rh_writer_write_connection (con_verified,
ifcfg_dir,
NULL,
NULL,
NULL,
&filename,
reread,
out_reread_same,
@@ -394,6 +396,8 @@ _writer_new_connection_fail (NMConnection *connection,
success = nms_ifcfg_rh_writer_write_connection (connection_normalized,
ifcfg_dir,
NULL,
NULL,
NULL,
&filename,
&reread,
NULL,