ifcfg-rh: merge new_connection() and update_connection() functions

They are basically the same, with a minor difference where the @filename
argument determines whether to write a new file or do an update.

Also, rename them, to give them a nms_* prefix in the header file.
This commit is contained in:
Thomas Haller 2017-10-20 13:48:57 +02:00
parent 3c3fc089ad
commit 6cb46619ed
5 changed files with 58 additions and 89 deletions

View file

@ -335,33 +335,27 @@ commit_changes (NMSettingsConnection *connection,
gs_unref_object NMConnection *reread = NULL;
gboolean reread_same = TRUE;
const char *operation_message;
gs_free char *ifcfg_path = NULL;
nm_assert (out_reread_connection && !*out_reread_connection);
nm_assert (!out_logmsg_change || !*out_logmsg_change);
filename = nm_settings_connection_get_filename (connection);
if (!filename) {
gs_free char *ifcfg_path = NULL;
if (!nms_ifcfg_rh_writer_write_connection (new_connection ?: NM_CONNECTION (connection),
IFCFG_DIR,
filename,
&ifcfg_path,
&reread,
&reread_same,
error))
return FALSE;
if (!writer_new_connection (new_connection ?: NM_CONNECTION (connection),
IFCFG_DIR,
&ifcfg_path,
&reread,
&reread_same,
error))
return FALSE;
nm_assert ((!filename && ifcfg_path) || (filename && !ifcfg_path));
if (ifcfg_path) {
nm_settings_connection_set_filename (connection, ifcfg_path);
operation_message = "persist";
} else {
if (!writer_update_connection (new_connection ?: NM_CONNECTION (connection),
IFCFG_DIR,
filename,
&reread,
&reread_same,
error))
return FALSE;
} else
operation_message = "update";
}
if (reread && !reread_same)
*out_reread_connection = g_steal_pointer (&reread);

View file

@ -683,11 +683,11 @@ add_connection (NMSettingsPlugin *config,
/* Ensure we reject attempts to add the connection long before we're
* asked to write it to disk.
*/
if (!writer_can_write_connection (connection, error))
if (!nms_ifcfg_rh_writer_can_write_connection (connection, error))
return NULL;
if (save_to_disk) {
if (!writer_new_connection (connection, IFCFG_DIR, &path, NULL, NULL, error))
if (!nms_ifcfg_rh_writer_write_connection (connection, IFCFG_DIR, NULL, &path, NULL, NULL, error))
return NULL;
}
return NM_SETTINGS_CONNECTION (update_connection (self, connection, path, NULL, FALSE, NULL, error));

View file

@ -2761,14 +2761,14 @@ escape_id (const char *id)
return escaped;
}
static gboolean
write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error)
gboolean
nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error)
{
NMSettingConnection *s_con;
nm_auto_shvar_file_close shvarFile *ifcfg = NULL;
@ -2781,7 +2781,14 @@ write_connection (NMConnection *connection,
nm_assert (_nm_connection_verify (connection, NULL) == NM_SETTING_VERIFY_SUCCESS);
nm_assert (!out_reread || !*out_reread);
if (!writer_can_write_connection (connection, error))
if ( filename
&& utils_has_complex_routes (filename)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
"Cannot modify a connection that has an associated 'rule-' or 'rule6-' file");
return FALSE;
}
if (!nms_ifcfg_rh_writer_can_write_connection (connection, error))
return FALSE;
s_con = nm_connection_get_setting_connection (connection);
@ -2950,7 +2957,7 @@ write_connection (NMConnection *connection,
}
gboolean
writer_can_write_connection (NMConnection *connection, GError **error)
nms_ifcfg_rh_writer_can_write_connection (NMConnection *connection, GError **error)
{
NMSettingConnection *s_con;
@ -2974,31 +2981,3 @@ writer_can_write_connection (NMConnection *connection, GError **error)
return FALSE;
}
gboolean
writer_new_connection (NMConnection *connection,
const char *ifcfg_dir,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error)
{
return write_connection (connection, ifcfg_dir, NULL, out_filename, out_reread, out_reread_same, error);
}
gboolean
writer_update_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error)
{
if (utils_has_complex_routes (filename)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
"Cannot modify a connection that has an associated 'rule-' or 'rule6-' file");
return FALSE;
}
return write_connection (connection, ifcfg_dir, filename, NULL, out_reread, out_reread_same, error);
}

View file

@ -18,26 +18,20 @@
* Copyright (C) 2009 Red Hat, Inc.
*/
#ifndef _WRITER_H_
#define _WRITER_H_
#ifndef __NMS_IFCFG_RH_WRITER_H__
#define __NMS_IFCFG_RH_WRITER_H__
#include "nm-connection.h"
gboolean writer_can_write_connection (NMConnection *connection,
GError **error);
gboolean nms_ifcfg_rh_writer_can_write_connection (NMConnection *connection,
GError **error);
gboolean writer_new_connection (NMConnection *connection,
const char *ifcfg_dir,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error);
gboolean nms_ifcfg_rh_writer_write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
char **out_filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error);
gboolean writer_update_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
NMConnection **out_reread,
gboolean *out_reread_same,
GError **error);
#endif /* _WRITER_H_ */
#endif /* __NMS_IFCFG_RH_WRITER_H__ */

View file

@ -232,7 +232,7 @@ _assert_expected_content (NMConnection *connection, const char *filename, const
g_assert (_ifcfg_dir && _ifcfg_dir[0]); \
g_assert (_filename && _filename[0]); \
\
_success = writer_update_connection (_connection, _ifcfg_dir, _filename, _out_reread, _out_reread_same, &_error); \
_success = nms_ifcfg_rh_writer_write_connection (_connection, _ifcfg_dir, _filename, NULL, _out_reread, _out_reread_same, &_error); \
nmtst_assert_success (_success, _error); \
_assert_expected_content (_connection, _filename, _expected); \
} G_STMT_END
@ -310,12 +310,13 @@ _writer_new_connection_reread (NMConnection *connection,
con_verified = nmtst_connection_duplicate_and_normalize (connection);
success = writer_new_connection (con_verified,
ifcfg_dir,
&filename,
reread,
out_reread_same,
&error);
success = nms_ifcfg_rh_writer_write_connection (con_verified,
ifcfg_dir,
NULL,
&filename,
reread,
out_reread_same,
&error);
nmtst_assert_success (success, error);
g_assert (filename && filename[0]);
@ -384,12 +385,13 @@ _writer_new_connection_fail (NMConnection *connection,
connection_normalized = nmtst_connection_duplicate_and_normalize (connection);
success = writer_new_connection (connection_normalized,
ifcfg_dir,
&filename,
&reread,
NULL,
&local);
success = nms_ifcfg_rh_writer_write_connection (connection_normalized,
ifcfg_dir,
NULL,
&filename,
&reread,
NULL,
&local);
nmtst_assert_no_success (success, local);
g_assert (!filename);
g_assert (!reread);