summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2013-08-14 20:38:51 -0400
committerColin Walters <walters@verbum.org>2013-08-14 20:44:45 -0400
commit4501b425953c3849112206c4a3f6f27cd3264936 (patch)
tree96bfcf6a67f89e382d61bc8941ea0d47578548ef
parent4150e6336881faec1a31f557567ca89c283b38c9 (diff)
downloadlibgsystem-4501b425953c3849112206c4a3f6f27cd3264936.tar.gz
fileutils: Add API to fstat() a stream
Will be used by ostree. Since libgsystem will return raw GUnix{Input,Output}Stream, we need a way to fstat() them. Maybe we should also have an API to synthesize a #GFileInfo, but that's hard without duplicating all the complex stuff in GIO. https://bugzilla.gnome.org/show_bug.cgi?id=706031
-rw-r--r--gsystem-file-utils.c39
-rw-r--r--gsystem-file-utils.h8
2 files changed, 47 insertions, 0 deletions
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
index 28c55d9..c734d2d 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/gfiledescriptorbased.h>
#include <gio/gunixoutputstream.h>
#include <glib-unix.h>
#include <limits.h>
@@ -130,6 +131,44 @@ gs_file_read_noatime (GFile *file,
}
/**
+ * gs_stream_fstat:
+ * @stream: A stream containing a Unix file descriptor
+ * @stbuf: Memory location to write stat buffer
+ * @cancellable:
+ * @error:
+ *
+ * Some streams created via libgsystem are #GUnixInputStream; these do
+ * not support e.g. g_file_input_stream_query_info(). This function
+ * allows dropping to the raw unix fstat() call for these types of
+ * streams, while still conveniently wrapped with the normal GLib
+ * handling of @cancellable and @error.
+ */
+gboolean
+gs_stream_fstat (GFileDescriptorBased *stream,
+ struct stat *stbuf,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gboolean ret = FALSE;
+ int fd;
+
+ if (g_cancellable_set_error_if_cancelled (cancellable, error))
+ goto out;
+
+ fd = g_file_descriptor_based_get_fd (stream);
+
+ if (fstat (fd, stbuf) == -1)
+ {
+ _set_error_from_errno (error);
+ goto out;
+ }
+
+ ret = TRUE;
+ out:
+ return ret;
+}
+
+/**
* gs_file_map_noatime: (skip)
* @file: a #GFile
* @cancellable: a #GCancellable
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
index 0d9994d..aae9944 100644
--- a/gsystem-file-utils.h
+++ b/gsystem-file-utils.h
@@ -22,6 +22,7 @@
#define __GSYSTEM_FILE_UTILS_H__
#include <gio/gio.h>
+#include <sys/stat.h>
G_BEGIN_DECLS
@@ -42,6 +43,13 @@ GMappedFile *gs_file_map_noatime (GFile *file,
GCancellable *cancellable,
GError **error);
+#ifndef __GI_SCANNER__
+gboolean gs_stream_fstat (GFileDescriptorBased *stream,
+ struct stat *out_stbuf,
+ GCancellable *cancellable,
+ GError **error);
+#endif
+
#if GLIB_CHECK_VERSION(2,34,0)
GBytes *gs_file_map_readonly (GFile *file,
GCancellable *cancellable,