From 30aa1ec668cd49412ee39d8f163cd1ac45cc0694 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 4 Aug 2016 13:29:13 -0400 Subject: [PATCH] lib: Use libglnx file replace API more consistently We have a better API now, drop use of the internal helper, which also depended on libgsystem. This required bumping libglnx to pull in a fix. Closes: #429 Approved by: giuseppe --- libglnx | 2 +- src/libostree/ostree-repo-pull.c | 24 +++++--- src/libotutil/ot-gio-utils.c | 94 ++------------------------------ 3 files changed, 23 insertions(+), 97 deletions(-) diff --git a/libglnx b/libglnx index c2ba4d87..5ac0d702 160000 --- a/libglnx +++ b/libglnx @@ -1 +1 @@ -Subproject commit c2ba4d879956436c1349acb0aeafd6f885276c67 +Subproject commit 5ac0d702d70b00887f9329e47f4d5653e994531a diff --git a/src/libostree/ostree-repo-pull.c b/src/libostree/ostree-repo-pull.c index 7d9f61b0..712f17dc 100644 --- a/src/libostree/ostree-repo-pull.c +++ b/src/libostree/ostree-repo-pull.c @@ -2791,16 +2791,24 @@ ostree_repo_pull_with_options (OstreeRepo *self, if (pull_data->is_mirror && pull_data->summary_data) { - if (!ot_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary", - pull_data->summary_data, !pull_data->repo->disable_fsync, - cancellable, error)) + GLnxFileReplaceFlags replaceflag = + pull_data->repo->disable_fsync ? GLNX_FILE_REPLACE_NODATASYNC : 0; + gsize len; + const guint8 *buf = g_bytes_get_data (pull_data->summary_data, &len); + + if (!glnx_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary", + buf, len, replaceflag, + cancellable, error)) goto out; - if (pull_data->summary_data_sig && - !ot_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary.sig", - pull_data->summary_data_sig, !pull_data->repo->disable_fsync, - cancellable, error)) - goto out; + if (pull_data->summary_data_sig) + { + buf = g_bytes_get_data (pull_data->summary_data_sig, &len); + if (!glnx_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary.sig", + buf, len, replaceflag, + cancellable, error)) + goto out; + } } if (!ostree_repo_commit_transaction (pull_data->repo, NULL, cancellable, error)) diff --git a/src/libotutil/ot-gio-utils.c b/src/libotutil/ot-gio-utils.c index 4ff2feda..a7fe74cf 100644 --- a/src/libotutil/ot-gio-utils.c +++ b/src/libotutil/ot-gio-utils.c @@ -275,76 +275,6 @@ ot_gfile_load_contents_utf8_allow_noent (GFile *path, return ret; } -/** - * ot_file_replace_contents_at: - * - * Like g_file_replace_contents(), except using a fd-relative - * directory, and optionally enforces use of fdatasync(). - */ -gboolean -ot_file_replace_contents_at (int dfd, - const char *path, - GBytes *contents, - gboolean datasync, - GCancellable *cancellable, - GError **error) -{ - gboolean ret = FALSE; - int fd; - g_autofree char *tmpname = NULL; - g_autoptr(GOutputStream) stream = NULL; - g_autoptr(GInputStream) instream = NULL; - - if (!gs_file_open_in_tmpdir_at (dfd, 0644, - &tmpname, &stream, - cancellable, error)) - goto out; - - g_assert (G_IS_FILE_DESCRIPTOR_BASED (stream)); - fd = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (stream)); - - instream = g_memory_input_stream_new_from_bytes (contents); - - if (g_bytes_get_size (contents) > 0) - { - int r = posix_fallocate (fd, 0, g_bytes_get_size (contents)); - if (r != 0) - { - /* posix_fallocate is a weird deviation from errno standards */ - errno = r; - glnx_set_error_from_errno (error); - goto out; - } - } - - if (g_output_stream_splice (stream, instream, 0, - cancellable, error) < 0) - goto out; - - if (datasync && fdatasync (fd) != 0) - { - glnx_set_error_from_errno (error); - goto out; - } - - if (!g_output_stream_close (stream, cancellable, error)) - goto out; - - if (renameat (dfd, tmpname, dfd, path) == -1) - { - glnx_set_error_from_errno (error); - goto out; - } - - g_clear_pointer (&tmpname, g_free); - - ret = TRUE; - out: - if (tmpname) - (void) unlinkat (dfd, tmpname, 0); - return ret; -} - /** * ot_gfile_replace_contents_fsync: * @@ -356,25 +286,13 @@ ot_gfile_replace_contents_fsync (GFile *path, GCancellable *cancellable, GError **error) { - gboolean ret = FALSE; - glnx_fd_close int parent_dfd = -1; - const char *target_basename = glnx_basename (gs_file_get_path_cached (path)); - g_autoptr(GFile) parent = NULL; + gsize len; + const guint8*buf = g_bytes_get_data (contents, &len); - parent = g_file_get_parent (path); - - if (!glnx_opendirat (AT_FDCWD, gs_file_get_path_cached (parent), TRUE, - &parent_dfd, error)) - goto out; - - if (!ot_file_replace_contents_at (parent_dfd, target_basename, - contents, TRUE, - cancellable, error)) - goto out; - - ret = TRUE; - out: - return ret; + return glnx_file_replace_contents_at (AT_FDCWD, gs_file_get_path_cached (path), + buf, len, + GLNX_FILE_REPLACE_DATASYNC_NEW, + cancellable, error); } /**