[ostree/wip/gs-dir-iter: 3/4] core: Add some helper functions to read all input from a fd



commit 95e3413ec4180d15454b40bf0ec7f557e62e55dc
Author: Colin Walters <walters verbum org>
Date:   Mon Dec 15 22:23:37 2014 -0500

    core: Add some helper functions to read all input from a fd
    
    Sadly...but can't quite go in GIO due to the Unix fd dependency.

 src/libotutil/ot-gio-utils.c |   37 +++++++++++++++++++++++++++++++++++++
 src/libotutil/ot-gio-utils.h |    4 ++++
 2 files changed, 41 insertions(+), 0 deletions(-)
---
diff --git a/src/libotutil/ot-gio-utils.c b/src/libotutil/ot-gio-utils.c
index 66d4377..1092f38 100644
--- a/src/libotutil/ot-gio-utils.c
+++ b/src/libotutil/ot-gio-utils.c
@@ -549,3 +549,40 @@ ot_gfile_atomic_symlink_swap (GFile          *path,
   if (parent_dfd != -1) (void) close (parent_dfd);
   return ret;
 }
+
+GBytes *
+ot_util_fd_readall_bytes (int fd, GCancellable *cancellable, GError **error)
+{
+  gs_unref_object GUnixInputStream *unixin = (GUnixInputStream*)g_unix_input_stream_new (fd, FALSE);
+  gs_unref_object GMemoryOutputStream *memout = (GMemoryOutputStream*)g_memory_output_stream_new_resizable 
();
+
+  if (g_output_stream_splice ((GOutputStream*)memout, (GInputStream*)unixin, 0, cancellable, error) < 0)
+    return NULL;
+  
+  return g_memory_output_stream_steal_as_bytes (memout);
+}
+
+char *
+ot_util_fd_readall_utf8 (int fd, GCancellable *cancellable, GError **error)
+{
+  GBytes *ret = ot_util_fd_readall_bytes (fd, cancellable, error);
+  const char *buf;
+  gsize len;
+
+  if (!ret)
+    return NULL;
+
+  buf = g_bytes_get_data (ret, &len);
+
+  if (!g_utf8_validate (buf, len, NULL))
+    {
+      g_bytes_unref (ret);
+      g_set_error (error,
+                   G_IO_ERROR,
+                   G_IO_ERROR_INVALID_DATA,
+                   "Invalid UTF-8");
+      return NULL;
+    }
+
+  return g_bytes_unref_to_data (ret, &len);
+}
diff --git a/src/libotutil/ot-gio-utils.h b/src/libotutil/ot-gio-utils.h
index d7c3945..5c6c20b 100644
--- a/src/libotutil/ot-gio-utils.h
+++ b/src/libotutil/ot-gio-utils.h
@@ -93,5 +93,9 @@ gboolean ot_util_fsync_directory (GFile         *dir,
                                   GCancellable  *cancellable,
                                   GError       **error);
 
+GBytes *ot_util_fd_readall_bytes (int fd, GCancellable *cancellable, GError **error);
+
+char * ot_util_fd_readall_utf8 (int fd, GCancellable *cancellable, GError **error);
+
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]