Add fu_common_get_contents_stream() for future usage

This commit is contained in:
Richard Hughes 2021-12-15 13:30:34 +00:00
parent 52fd89f9ab
commit bcf000d7cc
3 changed files with 39 additions and 13 deletions

View File

@ -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 <glib-unix.h> 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 <glib-unix.h> is unavailable");
return NULL;
#endif
}
#ifdef HAVE_LIBARCHIVE

View File

@ -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;

View File

@ -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;