diff options
author | Thomas Haller <thaller@redhat.com> | 2018-12-21 09:39:01 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-02-19 16:19:51 +0100 |
commit | c29d6138ddc1a0f3f684d50b1ad5ce7c0bf15cf4 (patch) | |
tree | 1e80d83ca3d4d1cefeff0116392c023704551150 | |
parent | d028d3bd98560a17ee845cbdd5a7b6be4f14e030 (diff) | |
download | NetworkManager-th/keyfile-cleanup.tar.gz |
keyfile: cleanup _internal_write_connection()th/keyfile-cleanup
- use gs_free instead of explicit free().
- use nm_streq*() instead of strcmp().
- move deletion of existing file after we successfully wrote
the new file.
- add parameter existing_path_readonly, to avoid to overwrite or
delete the existing path (if it exists). This is still mostly unused,
but will be necessary when we have read-only directories.
-rw-r--r-- | src/settings/plugins/keyfile/nms-keyfile-writer.c | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/src/settings/plugins/keyfile/nms-keyfile-writer.c b/src/settings/plugins/keyfile/nms-keyfile-writer.c index 2536378613..8c75d8c799 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-writer.c +++ b/src/settings/plugins/keyfile/nms-keyfile-writer.c @@ -175,15 +175,16 @@ _internal_write_connection (NMConnection *connection, uid_t owner_uid, pid_t owner_grp, const char *existing_path, + gboolean existing_path_read_only, gboolean force_rename, char **out_path, NMConnection **out_reread, gboolean *out_reread_same, GError **error) { - gs_unref_keyfile GKeyFile *key_file = NULL; - gs_free char *data = NULL; - gsize len; + gs_unref_keyfile GKeyFile *kf_file = NULL; + gs_free char *kf_content_buf = NULL; + gsize kf_content_len; gs_free char *path = NULL; const char *id; WriteInfo info = { 0 }; @@ -195,6 +196,7 @@ _internal_write_connection (NMConnection *connection, g_return_val_if_fail (keyfile_dir && keyfile_dir[0] == '/', FALSE); rename = force_rename + || existing_path_read_only || ( existing_path && !nm_utils_file_is_in_path (existing_path, keyfile_dir)); @@ -213,11 +215,11 @@ _internal_write_connection (NMConnection *connection, info.keyfile_dir = keyfile_dir; - key_file = nm_keyfile_write (connection, _handler_write, &info, error); - if (!key_file) + kf_file = nm_keyfile_write (connection, _handler_write, &info, error); + if (!kf_file) return FALSE; - data = g_key_file_to_data (key_file, &len, error); - if (!data) + kf_content_buf = g_key_file_to_data (kf_file, &kf_content_len, error); + if (!kf_content_buf) return FALSE; if (!g_file_test (keyfile_dir, G_FILE_TEST_IS_DIR)) @@ -226,13 +228,14 @@ _internal_write_connection (NMConnection *connection, /* If we have existing file path, use it. Else generate one from * connection's ID. */ - if (existing_path != NULL && !rename) { + if ( existing_path + && !rename) path = g_strdup (existing_path); - } else { - char *filename_escaped = nm_keyfile_utils_create_filename (id, with_extension); + else { + gs_free char *filename_escaped = NULL; + filename_escaped = nm_keyfile_utils_create_filename (id, with_extension); path = g_build_filename (keyfile_dir, filename_escaped, NULL); - g_free (filename_escaped); } /* If a file with this path already exists (but isn't the existing path @@ -242,13 +245,15 @@ _internal_write_connection (NMConnection *connection, * there's a race here, but there's not a lot we can do about it, and * we shouldn't get more than one connection with the same UUID either. */ - if (g_strcmp0 (path, existing_path) != 0 && g_file_test (path, G_FILE_TEST_EXISTS)) { + if ( !nm_streq0 (path, existing_path) + && g_file_test (path, G_FILE_TEST_EXISTS)) { guint i; gboolean name_found = FALSE; /* A keyfile with this connection's ID already exists. Pick another name. */ for (i = 0; i < 100; i++) { - char *filename, *filename_escaped; + gs_free char *filename_escaped = NULL; + gs_free char *filename = NULL; if (i == 0) filename = g_strdup_printf ("%s-%s", id, nm_connection_get_uuid (connection)); @@ -259,15 +264,15 @@ _internal_write_connection (NMConnection *connection, g_free (path); path = g_strdup_printf ("%s/%s", keyfile_dir, filename_escaped); - g_free (filename); - g_free (filename_escaped); - if (g_strcmp0 (path, existing_path) == 0 || !g_file_test (path, G_FILE_TEST_EXISTS)) { + + if ( nm_streq0 (path, existing_path) + || !g_file_test (path, G_FILE_TEST_EXISTS)) { name_found = TRUE; break; } } if (!name_found) { - if (existing_path == NULL) { + if (existing_path_read_only || !existing_path) { /* this really should not happen, we tried hard to find an unused name... bail out. */ g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "could not find suitable keyfile file name (%s already used)", path); @@ -280,13 +285,7 @@ _internal_write_connection (NMConnection *connection, } } - /* In case of updating the connection and changing the file path, - * we need to remove the old one, not to end up with two connections. - */ - if (existing_path != NULL && strcmp (path, existing_path) != 0) - unlink (existing_path); - - nm_utils_file_set_contents (path, data, len, 0600, &local_err); + nm_utils_file_set_contents (path, kf_content_buf, kf_content_len, 0600, &local_err); if (local_err) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "error writing to file '%s': %s", @@ -304,12 +303,19 @@ _internal_write_connection (NMConnection *connection, return FALSE; } - if (out_reread || out_reread_same) - { + /* In case of updating the connection and changing the file path, + * we need to remove the old one, not to end up with two connections. + */ + if ( existing_path + && !existing_path_read_only + && !nm_streq (path, existing_path)) + unlink (existing_path); + + if (out_reread || out_reread_same) { gs_unref_object NMConnection *reread = NULL; gboolean reread_same = FALSE; - reread = nms_keyfile_reader_from_keyfile (key_file, path, NULL, profile_dir, FALSE, NULL); + reread = nms_keyfile_reader_from_keyfile (kf_file, path, NULL, profile_dir, FALSE, NULL); nm_assert (NM_IS_CONNECTION (reread)); @@ -364,6 +370,7 @@ nms_keyfile_writer_connection (NMConnection *connection, 0, 0, existing_path, + FALSE, force_rename, out_path, out_reread, @@ -389,6 +396,7 @@ nms_keyfile_writer_test_connection (NMConnection *connection, owner_grp, NULL, FALSE, + FALSE, out_path, out_reread, out_reread_same, |