summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2013-07-16 08:32:11 -0400
committerColin Walters <walters@verbum.org>2013-07-16 08:34:40 -0400
commitb5e773410cddad83b2a2aba8c7c89c59c3e32dbe (patch)
tree5e53dbfa1ec599556c5ba73b6501d9f3fc431a62
parente270f5379bd794391e9a8f1be7f9aa17f0d7dd97 (diff)
downloadlibgsystem-b5e773410cddad83b2a2aba8c7c89c59c3e32dbe.tar.gz
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
-rw-r--r--gsystem-file-utils.c39
-rw-r--r--gsystem-file-utils.h6
2 files changed, 45 insertions, 0 deletions
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 <glib/gstdio.h>
#include <gio/gunixinputstream.h>
+#include <gio/gunixoutputstream.h>
#include <glib-unix.h>
#include <limits.h>
@@ -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,