From 00f257023101b903c4a021a5a5c9276b053c9453 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Tue, 28 Oct 2025 11:16:06 +0100 Subject: [PATCH] Progress --- TinyDFS.c | 33 ++++++++++++++++++++++++++++----- TinyDFS.h | 20 +++++++++++++++----- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/TinyDFS.c b/TinyDFS.c index 37ffcc0..960b0d1 100644 --- a/TinyDFS.c +++ b/TinyDFS.c @@ -4487,6 +4487,12 @@ int tinydfs_submit_write(TinyDFS *tdfs, char *path, int path_len, int off, void return 0; } +void tinydfs_result_free(TinyDFS_Result *result) +{ + if (result->type == TINYDFS_RESULT_LIST_SUCCESS) + free(result->entities); +} + static void process_event_for_create(TinyDFS *tdfs, int opidx, int request_tag, ByteView msg) { @@ -4497,7 +4503,7 @@ static void process_event_for_create(TinyDFS *tdfs, BinaryReader reader = { msg.ptr, msg.len, 0 }; - // version; + // version if (!binary_read(&reader, NULL, sizeof(uint16_t))) { tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_CREATE_ERROR }; return; @@ -4617,37 +4623,54 @@ static void process_event_for_list(TinyDFS *tdfs, return; } + TinyDFS_Entity *entities = malloc(item_count * sizeof(TinyDFS_Entity)); + if (entities == NULL) { + tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; + return; + } + // Parse each list item for (uint32_t i = 0; i < item_count; i++) { uint8_t is_dir; if (!binary_read(&reader, &is_dir, sizeof(is_dir))) { tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; + free(entities); return; } uint16_t name_len; if (!binary_read(&reader, &name_len, sizeof(name_len))) { tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; + free(entities); return; } - // Skip the name data + char *name = reader.src + reader.cur; if (!binary_read(&reader, NULL, name_len)) { tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; + free(entities); return; } - // Note: In a full implementation, the list items would be stored somewhere - // accessible to the user. For now, we just validate the message format. + entities[i].is_dir = is_dir; + + if (name_len > sizeof(entities[i].name)-1) { + tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; + free(entities); + return; + } + memcpy(entities[i].name, name, name_len); + entities[i].name[name_len] = '\0'; } // Check there is nothing else to read if (binary_read(&reader, NULL, 1)) { tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; + free(entities); return; } - tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_SUCCESS }; + tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_SUCCESS, item_count, entities }; } static void process_event_for_read(TinyDFS *tdfs, diff --git a/TinyDFS.h b/TinyDFS.h index 5373706..1c223d4 100644 --- a/TinyDFS.h +++ b/TinyDFS.h @@ -6,6 +6,11 @@ typedef struct TinyDFS TinyDFS; +typedef struct { + char name[128]; // TODO: Implement a proper name length + bool is_dir; +} TinyDFS_Entity; + typedef enum { TINYDFS_RESULT_EMPTY, TINYDFS_RESULT_CREATE_ERROR, @@ -21,16 +26,21 @@ typedef enum { } TinyDFS_ResultType; typedef struct { + TinyDFS_ResultType type; + + int num_entities; + TinyDFS_Entity *entities; } TinyDFS_Result; TinyDFS *tinydfs_init(char *addr, uint16_t port); void tinydfs_free(TinyDFS *tdfs); void tinydfs_wait(TinyDFS *tdfs, int opidx, TinyDFS_Result *result, int timeout); -int tinydfs_submit_create (TinyDFS *tdfs, char *path, int path_len, bool is_dir, unsigned int chunk_size); -int tinydfs_submit_delete (TinyDFS *tdfs, char *path, int path_len); -int tinydfs_submit_list (TinyDFS *tdfs, char *path, int path_len); -int tinydfs_submit_read (TinyDFS *tdfs, char *path, int path_len, int off, void *dst, int len); -int tinydfs_submit_write (TinyDFS *tdfs, char *path, int path_len, int off, void *src, int len); +int tinydfs_submit_create (TinyDFS *tdfs, char *path, int path_len, bool is_dir, unsigned int chunk_size); +int tinydfs_submit_delete (TinyDFS *tdfs, char *path, int path_len); +int tinydfs_submit_list (TinyDFS *tdfs, char *path, int path_len); +int tinydfs_submit_read (TinyDFS *tdfs, char *path, int path_len, int off, void *dst, int len); +int tinydfs_submit_write (TinyDFS *tdfs, char *path, int path_len, int off, void *src, int len); +void tinydfs_result_free(TinyDFS_Result *result); #endif // TINYDFS_INCLUDED