From 3d162e772db80f6664a78583268150d2e1d1d29e Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 3 May 2016 17:20:43 -0400 Subject: fdio: Add glnx_stream_fstat Migrated from libgsystem's `gs_stream_fstat()`. It's a small function but I end up using it in OSTree a fair bit. --- glnx-fdio.c | 28 ++++++++++++++++++++++++++++ glnx-fdio.h | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/glnx-fdio.c b/glnx-fdio.c index 466cbc4..62371d0 100644 --- a/glnx-fdio.c +++ b/glnx-fdio.c @@ -748,3 +748,31 @@ glnx_file_replace_contents_with_perms_at (int dfd, out: return ret; } + +/** + * glnx_stream_fstat: + * @stream: A stream containing a Unix file descriptor + * @stbuf: Memory location to write stat buffer + * @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 @error. + */ +gboolean +glnx_stream_fstat (GFileDescriptorBased *stream, + struct stat *stbuf, + GError **error) +{ + int fd = g_file_descriptor_based_get_fd (stream); + + if (fstat (fd, stbuf) == -1) + { + glnx_set_prefix_error_from_errno (error, "%s", "fstat"); + return FALSE; + } + + return TRUE; +} diff --git a/glnx-fdio.h b/glnx-fdio.h index c0fd4e4..3ca1a66 100644 --- a/glnx-fdio.h +++ b/glnx-fdio.h @@ -21,6 +21,7 @@ #pragma once #include +#include #include #include #include @@ -121,4 +122,9 @@ glnx_file_copy_at (int src_dfd, GCancellable *cancellable, GError **error); +gboolean +glnx_stream_fstat (GFileDescriptorBased *stream, + struct stat *stbuf, + GError **error); + G_END_DECLS -- cgit v1.2.1