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
This commit is contained in:
Colin Walters 2016-08-04 13:29:13 -04:00 committed by Atomic Bot
parent 1ce633f158
commit 30aa1ec668
3 changed files with 23 additions and 97 deletions

@ -1 +1 @@
Subproject commit c2ba4d879956436c1349acb0aeafd6f885276c67
Subproject commit 5ac0d702d70b00887f9329e47f4d5653e994531a

View File

@ -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))

View File

@ -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);
}
/**