commit b88148bf5c4e693b36148e45221dfd3e7c5d7e24
parent 4110727b40a1c2ac4b5517a9e6304cd956e1ba7c
Author: Jacki <jacki@thejackimonster.de>
Date: Mon, 15 Jul 2024 19:47:50 +0200
Add function to feed a message's data directly to a file descriptor
Signed-off-by: Jacki <jacki@thejackimonster.de>
Diffstat:
2 files changed, 50 insertions(+), 3 deletions(-)
diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h
@@ -1577,8 +1577,8 @@ GNUNET_CHAT_message_iterate_tags (const struct GNUNET_CHAT_Message *message,
void *cls);
/**
- * Returns the amount of data from a given <i>message</i> if its kind
- * is #GNUNET_CHAT_KIND_DATA, otherwise it returns NULL.
+ * Returns the amount of data from a given chat <i>message</i> if its
+ * kind is #GNUNET_CHAT_KIND_DATA, otherwise it returns zero.
*
* @param[in] message Message
* @return The amount of data or zero
@@ -1587,7 +1587,7 @@ uint64_t
GNUNET_CHAT_message_available (const struct GNUNET_CHAT_Message *message);
/**
- * Reads the data from a given <i>message</i> if its kind is
+ * Reads the data from a given chat <i>message</i> if its kind is
* #GNUNET_CHAT_KIND_DATA into a specific <i>data</i> buffer
* up to a selected <i>size</i>.
*
@@ -1603,6 +1603,20 @@ GNUNET_CHAT_message_read (const struct GNUNET_CHAT_Message *message,
uint64_t size);
/**
+ * Feeds the data from a given chat <i>message</i> if its kind is
+ * #GNUNET_CHAT_KIND_DATA into a specific file descriptor (of pipe for
+ * example).
+ *
+ * @param[in] message Message
+ * @param[in] fd File descriptor
+ * @return #GNUNET_OK on success, #GNUNET_NO if there's not enough data
+ * to read, otherwise #GNUNET_SYSERR on failure
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CHAT_message_feed (const struct GNUNET_CHAT_Message *message,
+ int fd);
+
+/**
* Returns the name of a given <i>file</i> handle.
*
* @param[in] file File handle
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -32,10 +32,12 @@
#include <gnunet/gnunet_scheduler_lib.h>
#include <gnunet/gnunet_time_lib.h>
#include <gnunet/gnunet_util_lib.h>
+
#include <libgen.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
+#include <unistd.h>
#define _(String) ((const char*) String)
@@ -2407,6 +2409,37 @@ GNUNET_CHAT_message_read (const struct GNUNET_CHAT_Message *message,
}
+enum GNUNET_GenericReturnValue
+GNUNET_CHAT_message_feed (const struct GNUNET_CHAT_Message *message,
+ int fd)
+{
+ GNUNET_CHAT_VERSION_ASSERT();
+
+ if ((!message) || (GNUNET_YES != message_has_msg(message)) ||
+ (fd == -1))
+ return GNUNET_SYSERR;
+
+ if (GNUNET_MESSENGER_KIND_TALK != message->msg->header.kind)
+ return GNUNET_SYSERR;
+
+ if (!(message->msg->body.talk.length))
+ return GNUNET_NO;
+
+ const ssize_t written = write(
+ fd,
+ message->msg->body.talk.data,
+ message->msg->body.talk.length
+ );
+
+ if (-1 == written)
+ return GNUNET_SYSERR;
+ else if (written != message->msg->body.talk.length)
+ return GNUNET_NO;
+ else
+ return GNUNET_OK;
+}
+
+
const char*
GNUNET_CHAT_file_get_name (const struct GNUNET_CHAT_File *file)
{