[libgsystem] fileutils: Add API to fstat() a stream
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgsystem] fileutils: Add API to fstat() a stream
- Date: Thu, 15 Aug 2013 01:50:21 +0000 (UTC)
commit 4501b425953c3849112206c4a3f6f27cd3264936
Author: Colin Walters <walters verbum org>
Date: Wed Aug 14 20:38:51 2013 -0400
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
gsystem-file-utils.c | 39 +++++++++++++++++++++++++++++++++++++++
gsystem-file-utils.h | 8 ++++++++
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,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]