commit 3190e88262a22b847b6f2c73ebf01ceae726c153
parent 6e68e9f9b50685a715e6ed73253de14c5ba0995b
Author: Jacki <jacki@thejackimonster.de>
Date: Fri, 19 Apr 2024 17:06:39 +0200
Implement function to iterate through all files
Signed-off-by: Jacki <jacki@thejackimonster.de>
Diffstat:
3 files changed, 77 insertions(+), 0 deletions(-)
diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h
@@ -248,6 +248,19 @@ typedef void
const struct GNUNET_CHAT_Uri *uri);
/**
+ * Iterator over chat files of a specific chat handle.
+ *
+ * @param[in,out] cls Closure from #GNUNET_CHAT_iterate_files
+ * @param[in,out] handle Chat handle
+ * @param[in,out] file Chat file
+ * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise.
+ */
+typedef enum GNUNET_GenericReturnValue
+(*GNUNET_CHAT_FileCallback) (void *cls,
+ struct GNUNET_CHAT_Handle *handle,
+ struct GNUNET_CHAT_File *file);
+
+/**
* Iterator over chat contacts of a specific chat handle.
*
* @param[in,out] cls Closure from #GNUNET_CHAT_iterate_contacts
@@ -714,6 +727,20 @@ GNUNET_CHAT_upload_file (struct GNUNET_CHAT_Handle *handle,
void *cls);
/**
+ * Iterates through the files of a given chat <i>handle</i> with a selected
+ * callback and custom closure.
+ *
+ * @param[in,out] handle Chat handle
+ * @param[in] callback Callback for file iteration (optional)
+ * @param[in,out] cls Closure for file iteration (optional)
+ * @return Amount of files iterated or #GNUNET_SYSERR on failure
+ */
+int
+GNUNET_CHAT_iterate_files (struct GNUNET_CHAT_Handle *handle,
+ GNUNET_CHAT_FileCallback callback,
+ void *cls);
+
+/**
* Sets a custom <i>user pointer</i> to a given chat <i>handle</i> so it can
* be accessed in all handle related callbacks.
*
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -899,6 +899,29 @@ file_binding:
}
+int
+GNUNET_CHAT_iterate_files (struct GNUNET_CHAT_Handle *handle,
+ GNUNET_CHAT_FileCallback callback,
+ void *cls)
+{
+ GNUNET_CHAT_VERSION_ASSERT();
+
+ if ((!handle) || (handle->destruction))
+ return GNUNET_SYSERR;
+
+ struct GNUNET_CHAT_IterateFiles it;
+ it.handle = handle;
+ it.cb = callback;
+ it.cls = cls;
+
+ return GNUNET_CONTAINER_multihashmap_iterate(
+ handle->files,
+ it_iterate_files,
+ &it
+ );
+}
+
+
void
GNUNET_CHAT_set_user_pointer (struct GNUNET_CHAT_Handle *handle,
void *user_pointer)
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c
@@ -125,6 +125,33 @@ drop_lookup:
GNUNET_free(lookups);
}
+struct GNUNET_CHAT_IterateFiles
+{
+ struct GNUNET_CHAT_Handle *handle;
+ GNUNET_CHAT_FileCallback cb;
+ void *cls;
+};
+
+enum GNUNET_GenericReturnValue
+it_iterate_files (void *cls,
+ GNUNET_UNUSED const struct GNUNET_HashCode *key,
+ void *value)
+{
+ GNUNET_assert((cls) && (key));
+
+ struct GNUNET_CHAT_IterateFiles *it = cls;
+
+ if (!(it->cb))
+ return GNUNET_YES;
+
+ struct GNUNET_CHAT_File *file = (struct GNUNET_CHAT_File*) value;
+
+ if (!file)
+ return GNUNET_YES;
+
+ return it->cb(it->cls, it->handle, file);
+}
+
struct GNUNET_CHAT_HandleIterateContacts
{
struct GNUNET_CHAT_Handle *handle;