commit e184cc4fe544273d5490c314ee6724916deeba0a
parent 0f107aba69354e4aa04e63e2df180f18b56a33eb
Author: Jacki <jacki@thejackimonster.de>
Date: Wed, 17 Apr 2024 16:14:04 +0200
Abstract file path generation from hash
Signed-off-by: Jacki <jacki@thejackimonster.de>
Diffstat:
4 files changed, 59 insertions(+), 43 deletions(-)
diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c
@@ -723,6 +723,25 @@ handle_get_directory (const struct GNUNET_CHAT_Handle *handle)
return handle->current->directory;
}
+char*
+handle_create_file_path (const struct GNUNET_CHAT_Handle *handle,
+ const struct GNUNET_HashCode *hash)
+{
+ GNUNET_assert((handle) && (hash));
+
+ const char *directory = handle_get_directory(handle);
+
+ if (!directory)
+ return NULL;
+
+ char *filename;
+ util_get_filename (
+ directory, "files", hash, &filename
+ );
+
+ return filename;
+}
+
int
handle_update (struct GNUNET_CHAT_Handle *handle)
{
diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h
@@ -286,6 +286,19 @@ const char*
handle_get_directory (const struct GNUNET_CHAT_Handle *handle);
/**
+ * Returns an allocated string providing the full path to
+ * a file stored by a chat <i>handle</i> with a given
+ * <i>hash</i>.
+ *
+ * @param[in] handle Chat handle
+ * @param[in] hash File hash
+ * @return File path
+ */
+char*
+handle_create_file_path (const struct GNUNET_CHAT_Handle *handle,
+ const struct GNUNET_HashCode *hash);
+
+/**
* Updates the used private key by creating a new identity
* using the same identifier as currently in use, replacing
* the old identity.
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
@@ -254,16 +254,13 @@ notify_handle_fs_progress(void* cls,
);
file->unindex = NULL;
- const char *directory = handle_get_directory(chat);
+ char *filename = handle_create_file_path(
+ chat, &(file->hash)
+ );
- if (!directory)
+ if (!filename)
break;
- char *filename;
- util_get_filename (
- directory, "files", &(file->hash), &filename
- );
-
if (GNUNET_YES == GNUNET_DISK_file_test_read(filename))
remove(filename);
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -31,6 +31,7 @@
#include <gnunet/gnunet_scheduler_lib.h>
#include <gnunet/gnunet_time_lib.h>
#include <libgen.h>
+#include <string.h>
#include <strings.h>
#define _(String) ((const char*) String)
@@ -1638,9 +1639,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);
+ char *filename = handle_create_file_path(
+ context->handle, &hash
+ );
- if (!directory)
+ if (!filename)
return NULL;
struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get(
@@ -1648,11 +1651,6 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context,
&hash
);
- char *filename;
- util_get_filename (
- directory, "files", &hash, &filename
- );
-
if (file)
goto file_binding;
@@ -2201,16 +2199,13 @@ GNUNET_CHAT_file_get_local_size (const struct GNUNET_CHAT_File *file)
if (!file)
return 0;
- const char *directory = handle_get_directory(file->handle);
+ char *filename = handle_create_file_path(
+ file->handle, &(file->hash)
+ );
- if (!directory)
+ if (!filename)
return 0;
- char *filename;
- util_get_filename (
- directory, "files", &(file->hash), &filename
- );
-
uint64_t size;
if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
size = 0;
@@ -2243,16 +2238,13 @@ GNUNET_CHAT_file_open_preview (struct GNUNET_CHAT_File *file)
if (file->preview)
return file->preview;
- const char *directory = handle_get_directory(file->handle);
+ char *filename = handle_create_file_path(
+ file->handle, &(file->hash)
+ );
- if (!directory)
+ if (!filename)
return NULL;
- char *filename;
- util_get_filename (
- directory, "files", &(file->hash), &filename
- );
-
if (GNUNET_YES != GNUNET_DISK_file_test(filename))
goto free_filename;
@@ -2347,18 +2339,15 @@ GNUNET_CHAT_file_start_download (struct GNUNET_CHAT_File *file,
return GNUNET_OK;
}
- const char *directory = handle_get_directory(file->handle);
+ char *filename = handle_create_file_path(
+ file->handle, &(file->hash)
+ );
- if (!directory)
+ if (!filename)
return GNUNET_SYSERR;
const uint64_t size = GNUNET_FS_uri_chk_get_file_size(file->uri);
- char *filename;
- util_get_filename (
- directory, "files", &(file->hash), &filename
- );
-
uint64_t offset;
if (GNUNET_OK != GNUNET_DISK_file_size(filename, &offset,
GNUNET_NO, GNUNET_YES))
@@ -2369,7 +2358,7 @@ GNUNET_CHAT_file_start_download (struct GNUNET_CHAT_File *file,
if (callback)
callback(cls, file, size, size);
- return GNUNET_OK;
+ goto free_filename;
}
file_bind_downlaod(file, callback, cls);
@@ -2393,6 +2382,7 @@ GNUNET_CHAT_file_start_download (struct GNUNET_CHAT_File *file,
if (file->download)
file->status |= GNUNET_CHAT_FILE_STATUS_DOWNLOAD;
+free_filename:
GNUNET_free(filename);
return GNUNET_OK;
}
@@ -2472,16 +2462,13 @@ GNUNET_CHAT_file_unindex (struct GNUNET_CHAT_File *file,
if (file->unindex)
return GNUNET_OK;
- const char *directory = handle_get_directory(file->handle);
+ char *filename = handle_create_file_path(
+ file->handle, &(file->hash)
+ );
- if (!directory)
+ if (!filename)
return GNUNET_SYSERR;
- char *filename;
- util_get_filename (
- directory, "files", &(file->hash), &filename
- );
-
file->unindex = GNUNET_FS_unindex_start(
file->handle->fs, filename, file
);