From b5e773410cddad83b2a2aba8c7c89c59c3e32dbe Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 16 Jul 2013 08:32:11 -0400 Subject: gs_file_create: New API This allows ostree core to drop an invocation of chmod() after creating the file. See https://bugzilla.gnome.org/show_bug.cgi?id=699959 --- gsystem-file-utils.c | 39 +++++++++++++++++++++++++++++++++++++++ gsystem-file-utils.h | 6 ++++++ 2 files changed, 45 insertions(+) diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c index bc34b7c..2f57efa 100644 --- a/gsystem-file-utils.c +++ b/gsystem-file-utils.c @@ -32,6 +32,7 @@ #include "gsystem-glib-compat.h" #include #include +#include #include #include @@ -250,6 +251,44 @@ gs_file_sync_data (GFile *file, return ret; } +/** + * gs_file_create: + * @file: Path to non-existent file + * @mode: Unix access permissions + * @out_stream: (out) (transfer full) (allow-none): Newly created output, or %NULL + * @cancellable: a #GCancellable + * @error: a #GError + * + * Like g_file_create(), except this function allows specifying the + * access mode. This allows atomically creating private files. + */ +gboolean +gs_file_create (GFile *file, + int mode, + GOutputStream **out_stream, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + int fd; + GOutputStream *ret_stream = NULL; + + fd = open_nointr (gs_file_get_path_cached (file), O_WRONLY | O_CREAT | O_EXCL, mode); + if (fd < 0) + { + _set_error_from_errno (error); + goto out; + } + + ret_stream = g_unix_output_stream_new (fd, TRUE); + + ret = TRUE; + gs_transfer_out_value (out_stream, &ret_stream); + out: + g_clear_object (&ret_stream); + return ret; +} + static const char * get_default_tmp_prefix (void) { diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h index 3fa01b6..b553d7f 100644 --- a/gsystem-file-utils.h +++ b/gsystem-file-utils.h @@ -52,6 +52,12 @@ gboolean gs_file_sync_data (GFile *file, GCancellable *cancellable, GError **error); +gboolean gs_file_create (GFile *file, + int mode, + GOutputStream **out_stream, + GCancellable *cancellable, + GError **error); + gboolean gs_file_linkcopy (GFile *src, GFile *dest, GFileCopyFlags flags, -- cgit v1.2.1