libgnunetchat

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

commit 2d86ca47e55589718307872999b8cb362b818ba0
parent 349ec7b8d1452f3361b962ec8b845fc85acf052e
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Thu, 17 Feb 2022 13:38:19 +0100

Implemented account specific directories for configurations and files

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

Diffstat:
Msrc/gnunet_chat_account.c | 28++++++++++++++++++++++++++++
Msrc/gnunet_chat_account.h | 5+++++
Msrc/gnunet_chat_context.c | 8++++----
Msrc/gnunet_chat_group.c | 4++--
Msrc/gnunet_chat_handle.c | 14++++++++++++++
Msrc/gnunet_chat_handle.h | 3+++
Msrc/gnunet_chat_handle_intern.c | 15+++++++++------
Msrc/gnunet_chat_lib.c | 35++++++++++++++++++++++++++++++-----
8 files changed, 95 insertions(+), 17 deletions(-)

diff --git a/src/gnunet_chat_account.c b/src/gnunet_chat_account.c @@ -34,6 +34,7 @@ account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_CHAT_Account *account = GNUNET_new(struct GNUNET_CHAT_Account); account->ego = ego; + account->directory = NULL; account->name = NULL; util_set_name_field(name, &(account->name)); @@ -44,6 +45,30 @@ account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego, } void +account_update_directory (struct GNUNET_CHAT_Account *account, + const char *base_directory) +{ + GNUNET_assert((account) && (base_directory)); + + if (account->directory) + GNUNET_free(account->directory); + + struct GNUNET_IDENTITY_PublicKey key; + GNUNET_IDENTITY_ego_get_public_key(account->ego, &key); + + char *key_string = GNUNET_IDENTITY_public_key_to_string(&key); + + if (!key_string) + { + account->directory = NULL; + return; + } + + util_get_dirname(base_directory, key_string, &(account->directory)); + GNUNET_free(key_string); +} + +void account_destroy(struct GNUNET_CHAT_Account *account) { GNUNET_assert(account); @@ -51,5 +76,8 @@ account_destroy(struct GNUNET_CHAT_Account *account) if (account->name) GNUNET_free(account->name); + if (account->directory) + GNUNET_free(account->directory); + GNUNET_free(account); } diff --git a/src/gnunet_chat_account.h b/src/gnunet_chat_account.h @@ -32,6 +32,7 @@ struct GNUNET_CHAT_Account { struct GNUNET_IDENTITY_Ego *ego; + char *directory; char *name; void *user_pointer; @@ -42,6 +43,10 @@ account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego, const char *name); void +account_update_directory (struct GNUNET_CHAT_Account *account, + const char *base_directory); + +void account_destroy(struct GNUNET_CHAT_Account *account); #endif /* GNUNET_CHAT_ACCOUNT_H_ */ diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c @@ -169,7 +169,7 @@ context_load_config (struct GNUNET_CHAT_Context *context) (context->handle) && (context->room)); - const char *directory = context->handle->directory; + const char *directory = handle_get_directory(context->handle); if (!directory) return; @@ -228,7 +228,7 @@ context_save_config (const struct GNUNET_CHAT_Context *context) (context->handle) && (context->room)); - const char *directory = context->handle->directory; + const char *directory = handle_get_directory(context->handle); if (!directory) return; @@ -309,13 +309,13 @@ context_scan_configs (struct GNUNET_CHAT_Handle *handle) { GNUNET_assert((handle) && (handle->messenger)); - const char *directory = handle->directory; + const char *directory = handle_get_directory(handle); if (!directory) return; char* dirname; - util_get_dirname(handle->directory, "chats", &dirname); + util_get_dirname(directory, "chats", &dirname); if (GNUNET_YES != GNUNET_DISK_directory_test(dirname, GNUNET_YES)) goto free_dirname; diff --git a/src/gnunet_chat_group.c b/src/gnunet_chat_group.c @@ -104,7 +104,7 @@ group_load_config (struct GNUNET_CHAT_Group *group) { GNUNET_assert((group) && (group->handle)); - const char *directory = group->handle->directory; + const char *directory = handle_get_directory(group->handle); if ((!directory) || (!(group->context))) return; @@ -145,7 +145,7 @@ group_save_config (const struct GNUNET_CHAT_Group *group) { GNUNET_assert((group) && (group->handle)); - const char *directory = group->handle->directory; + const char *directory = handle_get_directory(group->handle); if ((!directory) || (!(group->context))) return; diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -260,6 +260,20 @@ handle_disconnect (struct GNUNET_CHAT_Handle *handle) handle_update_key(handle); } +const char* +handle_get_directory (const struct GNUNET_CHAT_Handle *handle) +{ + GNUNET_assert(handle); + + if (!(handle->directory)) + return NULL; + + if (!(handle->current)) + return handle->directory; + else + return handle->current->directory; +} + void handle_send_internal_message (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Context *context, diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -106,6 +106,9 @@ handle_connect (struct GNUNET_CHAT_Handle *handle, void handle_disconnect (struct GNUNET_CHAT_Handle *handle); +const char* +handle_get_directory (const struct GNUNET_CHAT_Handle *handle); + void handle_send_internal_message (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Context *context, diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c @@ -207,10 +207,7 @@ on_handle_gnunet_identity(void *cls, struct GNUNET_CHAT_Handle* handle = cls; if ((!name) || (!ego)) - { - handle_send_internal_message(handle, NULL, GNUNET_CHAT_FLAG_REFRESH, NULL); - return; - } + goto send_refresh; struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head; @@ -223,13 +220,13 @@ on_handle_gnunet_identity(void *cls, (0 == strcmp(accounts->account->name, name))) { accounts->account->ego = ego; - return; + goto send_refresh; } if (ego == accounts->account->ego) { util_set_name_field(name, &(accounts->account->name)); - return; + goto send_refresh; } skip_account: @@ -239,11 +236,17 @@ skip_account: accounts = GNUNET_new(struct GNUNET_CHAT_InternalAccounts); accounts->account = account_create_from_ego(ego, name); + if (handle->directory) + account_update_directory(accounts->account, handle->directory); + GNUNET_CONTAINER_DLL_insert_tail( handle->accounts_head, handle->accounts_tail, accounts ); + +send_refresh: + handle_send_internal_message(handle, NULL, GNUNET_CHAT_FLAG_REFRESH, NULL); } int diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -759,6 +759,11 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, if (GNUNET_OK != util_hash_file(path, &hash)) return NULL; + const char *directory = handle_get_directory(context->handle); + + if (!directory) + return NULL; + struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( context->handle->files, &hash @@ -766,7 +771,7 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, char *filename; util_get_filename ( - context->handle->directory, "files", &hash, &filename + directory, "files", &hash, &filename ); if (file) @@ -1128,9 +1133,14 @@ GNUNET_CHAT_file_get_local_size (const struct GNUNET_CHAT_File *file) if (!file) return 0; + const char *directory = handle_get_directory(file->handle); + + if (!directory) + return 0; + char *filename; util_get_filename ( - file->handle->directory, "files", &(file->hash), &filename + directory, "files", &(file->hash), &filename ); uint64_t size; @@ -1161,9 +1171,14 @@ GNUNET_CHAT_file_open_preview (struct GNUNET_CHAT_File *file) if (file->preview) return file->preview; + const char *directory = handle_get_directory(file->handle); + + if (!directory) + return NULL; + char *filename; util_get_filename ( - file->handle->directory, "files", &(file->hash), &filename + directory, "files", &(file->hash), &filename ); if (GNUNET_YES != GNUNET_DISK_file_test(filename)) @@ -1251,11 +1266,16 @@ GNUNET_CHAT_file_start_download (struct GNUNET_CHAT_File *file, return GNUNET_OK; } + const char *directory = handle_get_directory(file->handle); + + if (!directory) + return GNUNET_SYSERR; + const uint64_t size = GNUNET_FS_uri_chk_get_file_size(file->uri); char *filename; util_get_filename ( - file->handle->directory, "files", &(file->hash), &filename + directory, "files", &(file->hash), &filename ); uint64_t offset; @@ -1362,9 +1382,14 @@ GNUNET_CHAT_file_unindex (struct GNUNET_CHAT_File *file, if (file->unindex) return GNUNET_OK; + const char *directory = handle_get_directory(file->handle); + + if (!directory) + return GNUNET_SYSERR; + char *filename; util_get_filename ( - file->handle->directory, "files", &(file->hash), &filename + directory, "files", &(file->hash), &filename ); file->unindex = GNUNET_FS_unindex_start(