summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2016-05-03 17:20:43 -0400
committerColin Walters <walters@verbum.org>2016-05-03 17:23:17 -0400
commit3d162e772db80f6664a78583268150d2e1d1d29e (patch)
treef74bfea103ad76a39dc72433c68de4d683cc17b1
parent85c9dd5c073a8c0d74c4baa2e4a94f5535984e62 (diff)
downloadlibglnx-3d162e772db80f6664a78583268150d2e1d1d29e.tar.gz
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.
-rw-r--r--glnx-fdio.c28
-rw-r--r--glnx-fdio.h6
2 files changed, 34 insertions, 0 deletions
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 <glnx-backport-autocleanups.h>
+#include <gio/gfiledescriptorbased.h>
#include <limits.h>
#include <dirent.h>
#include <sys/stat.h>
@@ -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