From bcf000d7cc40f406a9a64dcdf963299cd909131d Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Wed, 15 Dec 2021 13:30:34 +0000 Subject: [PATCH] Add fu_common_get_contents_stream() for future usage --- libfwupdplugin/fu-common.c | 47 ++++++++++++++++++++++++---------- libfwupdplugin/fu-common.h | 4 +++ libfwupdplugin/fwupdplugin.map | 1 + 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/libfwupdplugin/fu-common.c b/libfwupdplugin/fu-common.c index dd1920e93..899440380 100644 --- a/libfwupdplugin/fu-common.c +++ b/libfwupdplugin/fu-common.c @@ -281,14 +281,45 @@ GBytes * fu_common_get_contents_fd(gint fd, gsize count, GError **error) { #ifdef HAVE_GIO_UNIX - guint8 tmp[0x8000] = {0x0}; - g_autoptr(GByteArray) buf = g_byte_array_new(); - g_autoptr(GError) error_local = NULL; g_autoptr(GInputStream) stream = NULL; g_return_val_if_fail(fd > 0, NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); + /* read the entire fd to a data blob */ + stream = g_unix_input_stream_new(fd, TRUE); + return fu_common_get_contents_stream(stream, count, error); +#else + g_set_error_literal(error, + FWUPD_ERROR, + FWUPD_ERROR_NOT_SUPPORTED, + "Not supported as is unavailable"); + return NULL; +#endif +} + +/** + * fu_common_get_contents_stream: + * @stream: input stream + * @count: the maximum number of bytes to read + * @error: (nullable): optional return location for an error + * + * Reads a blob from a specific input stream. + * + * Returns: (transfer full): a #GBytes, or %NULL + * + * Since: 1.7.4 + **/ +GBytes * +fu_common_get_contents_stream(GInputStream *stream, gsize count, GError **error) +{ + guint8 tmp[0x8000] = {0x0}; + g_autoptr(GByteArray) buf = g_byte_array_new(); + g_autoptr(GError) error_local = NULL; + + g_return_val_if_fail(G_IS_INPUT_STREAM(stream), NULL); + g_return_val_if_fail(error == NULL || *error == NULL, NULL); + /* this is invalid */ if (count == 0) { g_set_error_literal(error, @@ -298,9 +329,6 @@ fu_common_get_contents_fd(gint fd, gsize count, GError **error) return NULL; } - /* read the entire fd to a data blob */ - stream = g_unix_input_stream_new(fd, TRUE); - /* read from stream in 32kB chunks */ while (TRUE) { gssize sz; @@ -326,13 +354,6 @@ fu_common_get_contents_fd(gint fd, gsize count, GError **error) } } return g_byte_array_free_to_bytes(g_steal_pointer(&buf)); -#else - g_set_error_literal(error, - FWUPD_ERROR, - FWUPD_ERROR_NOT_SUPPORTED, - "Not supported as is unavailable"); - return NULL; -#endif } #ifdef HAVE_LIBARCHIVE diff --git a/libfwupdplugin/fu-common.h b/libfwupdplugin/fu-common.h index c82cef79e..2dbd1641c 100644 --- a/libfwupdplugin/fu-common.h +++ b/libfwupdplugin/fu-common.h @@ -201,6 +201,10 @@ fu_common_set_contents_bytes(const gchar *filename, GBytes * fu_common_get_contents_bytes(const gchar *filename, GError **error) G_GNUC_WARN_UNUSED_RESULT; GBytes * +fu_common_get_contents_stream(GInputStream *stream, + gsize count, + GError **error) G_GNUC_WARN_UNUSED_RESULT; +GBytes * fu_common_get_contents_fd(gint fd, gsize count, GError **error) G_GNUC_WARN_UNUSED_RESULT; gboolean fu_common_extract_archive(GBytes *blob, const gchar *dir, GError **error) G_GNUC_WARN_UNUSED_RESULT; diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 77a1297c8..1e14bbe7c 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -980,6 +980,7 @@ LIBFWUPDPLUGIN_1.7.3 { LIBFWUPDPLUGIN_1.7.4 { global: + fu_common_get_contents_stream; fu_memmem_safe; local: *; } LIBFWUPDPLUGIN_1.7.3;