libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit aa8a1d1faeee94b92c3bb9094cbaa1c1e63f1e38
parent a7308b22bf24131c5a0bf715e4f79e375e4bcfa2
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Thu,  6 Jan 2022 23:41:30 +0100

Updated file uploading to allow setting user pointer before upload

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>

Diffstat:
Minclude/gnunet_chat_lib.h | 6+++---
Msrc/gnunet_chat_handle_intern.c | 11+++++++++--
Msrc/gnunet_chat_lib.c | 41+++++++++++++++++++++++++++++++----------
Msrc/gnunet_chat_lib_intern.c | 22+++++++++++++++++-----
4 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -624,9 +624,9 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, * @param[in] path Local file path * @param[in] callback Callback for file uploading (optional) * @param[in,out] cls Closure for file uploading (optional) - * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure + * @return The file handle on success, NULL on failure */ -int +struct GNUNET_CHAT_File* GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, const char *path, GNUNET_CHAT_FileUploadCallback callback, diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -418,7 +418,14 @@ on_handle_message (void *cls, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST ); - struct GNUNET_CHAT_File *file = file_create_from_message( + struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( + context->handle->files, &(msg->body.file.hash) + ); + + if (file) + break; + + file = file_create_from_message( context->handle, &(msg->body.file) ); diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -611,32 +611,40 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, } -int +struct GNUNET_CHAT_File* GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, const char *path, GNUNET_CHAT_FileUploadCallback callback, void *cls) { if ((!context) || (!path) || (!(context->room))) - return GNUNET_SYSERR; + return NULL; if (!(context->handle->directory)) - return GNUNET_SYSERR; + return NULL; struct GNUNET_HashCode hash; if (GNUNET_OK != util_hash_file(path, &hash)) - return GNUNET_SYSERR; + return NULL; + + struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( + context->handle->files, + &hash + ); char *filename; util_get_filename ( context->handle->directory, "files", &hash, &filename ); + if (file) + goto file_upload; + if ((GNUNET_OK != GNUNET_DISK_directory_create_for_file(filename)) || (GNUNET_OK != GNUNET_DISK_file_copy(path, filename))) { GNUNET_free(filename); - return GNUNET_SYSERR; + return NULL; } struct GNUNET_CRYPTO_SymmetricSessionKey key; @@ -645,17 +653,30 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, if (GNUNET_OK != util_encrypt_file(filename, &key)) { GNUNET_free(filename); - return GNUNET_SYSERR; + return NULL; } char* p = GNUNET_strdup(path); - struct GNUNET_CHAT_File *file = file_create_from_disk( - context->handle, basename(p), &hash, &key + file = file_create_from_disk( + context->handle, + basename(p), + &hash, + &key ); GNUNET_free(p); + if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( + context->handle->files, &hash, file, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) + { + file_destroy(file); + GNUNET_free(filename); + return NULL; + } + +file_upload: file_bind_upload(file, callback, cls); struct GNUNET_FS_BlockOptions bo; @@ -685,7 +706,7 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, ); GNUNET_free(filename); - return GNUNET_OK; + return file; } diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -156,17 +156,29 @@ struct GNUNET_CHAT_ContextIterateFiles int it_context_iterate_files (void *cls, - GNUNET_UNUSED const struct GNUNET_HashCode *key, - void *value) + const struct GNUNET_HashCode *key, + GNUNET_UNUSED void *value) { - GNUNET_assert((cls) && (value)); + GNUNET_assert((cls) && (key)); struct GNUNET_CHAT_ContextIterateFiles *it = cls; if (!(it->cb)) return GNUNET_YES; - struct GNUNET_CHAT_File *file = value; + struct GNUNET_CHAT_Message *message = GNUNET_CONTAINER_multihashmap_get( + it->context->messages, key + ); + + if (!message) + return GNUNET_YES; + + struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( + it->context->handle->files, &(message->hash) + ); + + if (!file) + return GNUNET_YES; return it->cb(it->cls, it->context, file); }