This commit is contained in:
2025-10-28 11:16:06 +01:00
parent 223bbbefb2
commit 00f2570231
2 changed files with 43 additions and 10 deletions
+28 -5
View File
@@ -4487,6 +4487,12 @@ int tinydfs_submit_write(TinyDFS *tdfs, char *path, int path_len, int off, void
return 0; 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, static void process_event_for_create(TinyDFS *tdfs,
int opidx, int request_tag, ByteView msg) 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 }; BinaryReader reader = { msg.ptr, msg.len, 0 };
// version; // version
if (!binary_read(&reader, NULL, sizeof(uint16_t))) { if (!binary_read(&reader, NULL, sizeof(uint16_t))) {
tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_CREATE_ERROR }; tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_CREATE_ERROR };
return; return;
@@ -4617,37 +4623,54 @@ static void process_event_for_list(TinyDFS *tdfs,
return; 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 // Parse each list item
for (uint32_t i = 0; i < item_count; i++) { for (uint32_t i = 0; i < item_count; i++) {
uint8_t is_dir; uint8_t is_dir;
if (!binary_read(&reader, &is_dir, sizeof(is_dir))) { if (!binary_read(&reader, &is_dir, sizeof(is_dir))) {
tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR };
free(entities);
return; return;
} }
uint16_t name_len; uint16_t name_len;
if (!binary_read(&reader, &name_len, sizeof(name_len))) { if (!binary_read(&reader, &name_len, sizeof(name_len))) {
tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR };
free(entities);
return; return;
} }
// Skip the name data char *name = reader.src + reader.cur;
if (!binary_read(&reader, NULL, name_len)) { if (!binary_read(&reader, NULL, name_len)) {
tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR };
free(entities);
return; return;
} }
// Note: In a full implementation, the list items would be stored somewhere entities[i].is_dir = is_dir;
// accessible to the user. For now, we just validate the message format.
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 // Check there is nothing else to read
if (binary_read(&reader, NULL, 1)) { if (binary_read(&reader, NULL, 1)) {
tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR }; tdfs->operations[opidx].result = (TinyDFS_Result) { TINYDFS_RESULT_LIST_ERROR };
free(entities);
return; 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, static void process_event_for_read(TinyDFS *tdfs,
+15 -5
View File
@@ -6,6 +6,11 @@
typedef struct TinyDFS TinyDFS; typedef struct TinyDFS TinyDFS;
typedef struct {
char name[128]; // TODO: Implement a proper name length
bool is_dir;
} TinyDFS_Entity;
typedef enum { typedef enum {
TINYDFS_RESULT_EMPTY, TINYDFS_RESULT_EMPTY,
TINYDFS_RESULT_CREATE_ERROR, TINYDFS_RESULT_CREATE_ERROR,
@@ -21,16 +26,21 @@ typedef enum {
} TinyDFS_ResultType; } TinyDFS_ResultType;
typedef struct { typedef struct {
TinyDFS_ResultType type; TinyDFS_ResultType type;
int num_entities;
TinyDFS_Entity *entities;
} TinyDFS_Result; } TinyDFS_Result;
TinyDFS *tinydfs_init(char *addr, uint16_t port); TinyDFS *tinydfs_init(char *addr, uint16_t port);
void tinydfs_free(TinyDFS *tdfs); void tinydfs_free(TinyDFS *tdfs);
void tinydfs_wait(TinyDFS *tdfs, int opidx, TinyDFS_Result *result, int timeout); 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_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_delete (TinyDFS *tdfs, char *path, int path_len);
int tinydfs_submit_list (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_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_write (TinyDFS *tdfs, char *path, int path_len, int off, void *src, int len);
void tinydfs_result_free(TinyDFS_Result *result);
#endif // TINYDFS_INCLUDED #endif // TINYDFS_INCLUDED