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:
@@ -206,6 +206,8 @@ commit_changes (NMSettingsConnection *connection,
|
|||||||
if (!nms_ifcfg_rh_writer_write_connection (new_connection,
|
if (!nms_ifcfg_rh_writer_write_connection (new_connection,
|
||||||
IFCFG_DIR,
|
IFCFG_DIR,
|
||||||
filename,
|
filename,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
&ifcfg_path,
|
&ifcfg_path,
|
||||||
&reread,
|
&reread,
|
||||||
&reread_same,
|
&reread_same,
|
||||||
|
@@ -594,7 +594,7 @@ add_connection (NMSettingsPlugin *config,
|
|||||||
gs_unref_object NMConnection *reread = NULL;
|
gs_unref_object NMConnection *reread = NULL;
|
||||||
|
|
||||||
if (save_to_disk) {
|
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;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
if (!nms_ifcfg_rh_writer_can_write_connection (connection, error))
|
if (!nms_ifcfg_rh_writer_can_write_connection (connection, error))
|
||||||
|
@@ -3024,6 +3024,8 @@ static gboolean
|
|||||||
do_write_construct (NMConnection *connection,
|
do_write_construct (NMConnection *connection,
|
||||||
const char *ifcfg_dir,
|
const char *ifcfg_dir,
|
||||||
const char *filename,
|
const char *filename,
|
||||||
|
NMSIfcfgRHWriterAllowFilenameCb allow_filename_cb,
|
||||||
|
gpointer allow_filename_user_data,
|
||||||
shvarFile **out_ifcfg,
|
shvarFile **out_ifcfg,
|
||||||
GHashTable **out_blobs,
|
GHashTable **out_blobs,
|
||||||
GHashTable **out_secrets,
|
GHashTable **out_secrets,
|
||||||
@@ -3067,30 +3069,31 @@ do_write_construct (NMConnection *connection,
|
|||||||
|
|
||||||
ifcfg_name = g_strdup (filename);
|
ifcfg_name = g_strdup (filename);
|
||||||
} else if (ifcfg_dir) {
|
} else if (ifcfg_dir) {
|
||||||
char *escaped;
|
gs_free char *escaped = NULL;
|
||||||
|
int i_path;
|
||||||
|
|
||||||
escaped = escape_id (nm_setting_connection_get_id (s_con));
|
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.
|
for (i_path = 0; i_path < 10000; i_path++) {
|
||||||
* Multiple connections can have the same ID (ie if two connections with
|
gs_free char *path_candidate = NULL;
|
||||||
* 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;
|
|
||||||
|
|
||||||
nm_clear_g_free (&ifcfg_name);
|
if (i_path == 0)
|
||||||
while (idx++ < 500) {
|
path_candidate = g_strdup_printf ("%s/ifcfg-%s", ifcfg_dir, escaped);
|
||||||
ifcfg_name = g_strdup_printf ("%s/ifcfg-%s-%u", ifcfg_dir, escaped, idx);
|
else
|
||||||
if (g_file_test (ifcfg_name, G_FILE_TEST_EXISTS) == FALSE)
|
path_candidate = g_strdup_printf ("%s/ifcfg-%s-%d", ifcfg_dir, escaped, i_path);
|
||||||
break;
|
|
||||||
nm_clear_g_free (&ifcfg_name);
|
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,
|
g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
|
||||||
"Failed to find usable ifcfg file name");
|
"Failed to find usable ifcfg file name");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -3377,6 +3380,8 @@ gboolean
|
|||||||
nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
|
nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
|
||||||
const char *ifcfg_dir,
|
const char *ifcfg_dir,
|
||||||
const char *filename,
|
const char *filename,
|
||||||
|
NMSIfcfgRHWriterAllowFilenameCb allow_filename_cb,
|
||||||
|
gpointer allow_filename_user_data,
|
||||||
char **out_filename,
|
char **out_filename,
|
||||||
NMConnection **out_reread,
|
NMConnection **out_reread,
|
||||||
gboolean *out_reread_same,
|
gboolean *out_reread_same,
|
||||||
@@ -3396,6 +3401,8 @@ nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
|
|||||||
if (!do_write_construct (connection,
|
if (!do_write_construct (connection,
|
||||||
ifcfg_dir,
|
ifcfg_dir,
|
||||||
filename,
|
filename,
|
||||||
|
allow_filename_cb,
|
||||||
|
allow_filename_user_data,
|
||||||
&ifcfg,
|
&ifcfg,
|
||||||
&blobs,
|
&blobs,
|
||||||
&secrets,
|
&secrets,
|
||||||
|
@@ -22,12 +22,18 @@
|
|||||||
|
|
||||||
#include "nm-connection.h"
|
#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,
|
gboolean nms_ifcfg_rh_writer_can_write_connection (NMConnection *connection,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
|
||||||
gboolean nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
|
gboolean nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
|
||||||
const char *ifcfg_dir,
|
const char *ifcfg_dir,
|
||||||
const char *filename,
|
const char *filename,
|
||||||
|
NMSIfcfgRHWriterAllowFilenameCb allow_filename_cb,
|
||||||
|
gpointer allow_filename_user_data,
|
||||||
char **out_filename,
|
char **out_filename,
|
||||||
NMConnection **out_reread,
|
NMConnection **out_reread,
|
||||||
gboolean *out_reread_same,
|
gboolean *out_reread_same,
|
||||||
|
@@ -238,7 +238,7 @@ _assert_expected_content (NMConnection *connection, const char *filename, const
|
|||||||
g_assert (_ifcfg_dir && _ifcfg_dir[0]); \
|
g_assert (_ifcfg_dir && _ifcfg_dir[0]); \
|
||||||
g_assert (_filename && _filename[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); \
|
nmtst_assert_success (_success, _error); \
|
||||||
_assert_expected_content (_connection, _filename, _expected); \
|
_assert_expected_content (_connection, _filename, _expected); \
|
||||||
} G_STMT_END
|
} G_STMT_END
|
||||||
@@ -319,6 +319,8 @@ _writer_new_connection_reread (NMConnection *connection,
|
|||||||
success = nms_ifcfg_rh_writer_write_connection (con_verified,
|
success = nms_ifcfg_rh_writer_write_connection (con_verified,
|
||||||
ifcfg_dir,
|
ifcfg_dir,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
&filename,
|
&filename,
|
||||||
reread,
|
reread,
|
||||||
out_reread_same,
|
out_reread_same,
|
||||||
@@ -394,6 +396,8 @@ _writer_new_connection_fail (NMConnection *connection,
|
|||||||
success = nms_ifcfg_rh_writer_write_connection (connection_normalized,
|
success = nms_ifcfg_rh_writer_write_connection (connection_normalized,
|
||||||
ifcfg_dir,
|
ifcfg_dir,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
&filename,
|
&filename,
|
||||||
&reread,
|
&reread,
|
||||||
NULL,
|
NULL,
|
||||||
|
Reference in New Issue
Block a user