From a9aa5ba543570543a2fb60ecd0bf7faf622341ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 23 Jun 2021 15:04:01 +0200 Subject: [PATCH] refactor: make fileaccess state from union --- file-browser/include/file_access.h | 19 ++++++++++++++++++- file-browser/src/local_file_access.c | 20 ++++++++++---------- file-browser/src/local_file_connectors.c | 12 ++++++------ file-browser/src/local_file_utils.c | 6 +++--- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/file-browser/include/file_access.h b/file-browser/include/file_access.h index ec8613ccb924e66b09ceab2900b181241f4590bd..35244810d0db73616b05c997cda19b3f9aa726ae 100644 --- a/file-browser/include/file_access.h +++ b/file-browser/include/file_access.h @@ -70,9 +70,26 @@ typedef struct { typedef struct fileaccess_t fileaccess_t; +typedef struct { + char *path; +} local_fileaccess_state_t; + +typedef struct { + char *path; // ORDER MATTERS! + char *mount_device; + bool mounted; + file_operation_error_t error; +} extern_fileaccess_state_t; + +typedef local_fileaccess_state_t temp_fileaccess_state_t; + typedef struct { const fileaccess_t *fileaccess; - void *state; + union { + local_fileaccess_state_t local; + local_fileaccess_state_t temp; + extern_fileaccess_state_t exter; + } payload; } fileaccess_state_t; typedef fileaccess_state_t (*init_state_fn)(void *data); diff --git a/file-browser/src/local_file_access.c b/file-browser/src/local_file_access.c index 0027ebf8c7f241d9a801ae89f64cad9982b7148f..29ebcf5ce75f8df9a937ecef1ea2a802b773341e 100644 --- a/file-browser/src/local_file_access.c +++ b/file-browser/src/local_file_access.c @@ -14,7 +14,7 @@ #define ROOT "/" fileaccess_state_t local_fileaccess_init_state(void *data) { - fileaccess_state_t state = {.state = ROOT, .fileaccess = &local_file_access}; + fileaccess_state_t state = {.payload.local.path = ROOT, .fileaccess = &local_file_access}; return state; } @@ -67,8 +67,8 @@ static file_operation_error_t file_get_information(void **malloced, directory_or_error_t local_fileaccess_directory_list(fileaccess_state_t state, char *path) { directory_or_error_t ret; - char full_path[path_join_memory_size(state.state, path)]; - path_join((char *)state.state, path, full_path); + char full_path[path_join_memory_size(state.payload.local.path, path)]; + path_join((char *)state.payload.local.path, path, full_path); uint64_t malloc_offset = sizeof(directory_t) + strlen(path) + 1; uint64_t bytes_malloced = sizeof(directory_t) + strlen(path) + 1; @@ -138,14 +138,14 @@ directory_or_error_t local_fileaccess_directory_list(fileaccess_state_t state, } directory_or_error_t local_fileaccess_root_list(fileaccess_state_t state) { - return local_fileaccess_directory_list(state, (char *)state.state); + return local_fileaccess_directory_list(state, (char *)state.payload.local.path); } directory_or_error_t local_fileaccess_directory_create(fileaccess_state_t state, char *path) { directory_or_error_t ret; - char full_path[path_join_memory_size(state.state, path)]; - path_join((char *)state.state, path, full_path); + char full_path[path_join_memory_size(state.payload.local.path, path)]; + path_join((char *)state.payload.local.path, path, full_path); int status = mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); @@ -162,8 +162,8 @@ directory_or_error_t local_fileaccess_directory_create(fileaccess_state_t state, file_operation_error_t local_fileaccess_directory_delete(fileaccess_state_t state, char *path) { file_operation_error_t error = FILOPER_SUCCESS; - char full_path[path_join_memory_size(state.state, path)]; - path_join((char *)state.state, path, full_path); + char full_path[path_join_memory_size(state.payload.local.path, path)]; + path_join((char *)state.payload.local.path, path, full_path); int status = directory_delete(full_path); if (status != 0) { @@ -228,8 +228,8 @@ executing_file_or_error_t local_fileaccess_file_execute(fileaccess_state_t state file_operation_error_t local_fileaccess_file_delete(fileaccess_state_t state, char *path) { file_operation_error_t error = FILOPER_SUCCESS; - char full_path[path_join_memory_size(state.state, path)]; - path_join((char *)state.state, path, full_path); + char full_path[path_join_memory_size(state.payload.local.path, path)]; + path_join((char *)state.payload.local.path, path, full_path); int status = remove(path); diff --git a/file-browser/src/local_file_connectors.c b/file-browser/src/local_file_connectors.c index 5a4e216ad885cd52deab3e24d25d19064c193591..af7dd290e28f6509daefee20314c5d3e64abbeb5 100644 --- a/file-browser/src/local_file_connectors.c +++ b/file-browser/src/local_file_connectors.c @@ -14,8 +14,8 @@ file_operation_error_t local_connector_file_copy( file)]; file_get_full_path(first_state, file->directory, file, src_path); - char dst_path[path_join_memory_size(second_state.state, destination)]; - path_join(second_state.state, destination, dst_path); + char dst_path[path_join_memory_size(second_state.payload.local.path, destination)]; + path_join(second_state.payload.local.path, destination, dst_path); // TODO: copy folders @@ -38,7 +38,7 @@ file_operation_error_t local_connector_file_copy( fwrite(buffer, sizeof(char), in, dst_file); } - file_operation_error_t error; + file_operation_error_t error = FILOPER_SUCCESS; if (!feof(src_file) && ferror(src_file)) { error = file_operation_error_from_errno(ferror(src_file)); } else if (ferror(dst_file)) { @@ -47,7 +47,7 @@ file_operation_error_t local_connector_file_copy( fclose(src_file); fclose(dst_file); - return FILOPER_SUCCESS; + return error; } file_operation_error_t local_connector_file_move( @@ -56,8 +56,8 @@ file_operation_error_t local_connector_file_move( file_operation_error_t error = FILOPER_SUCCESS; char src_path[file_get_full_path_memory_size(first_state, file->directory, file)]; - char dst_path[path_join_memory_size(second_state.state, destination)]; - path_join(second_state.state, destination, dst_path); + char dst_path[path_join_memory_size(second_state.payload.local.path, destination)]; + path_join(second_state.payload.local.path, destination, dst_path); int status = rename(src_path, dst_path); if (status == 0) { diff --git a/file-browser/src/local_file_utils.c b/file-browser/src/local_file_utils.c index ee81a275bf4029f8d35b94978e81ba9cd2c9b0db..8545e760549f68d0e5354ed6c7d616cc44cacb67 100644 --- a/file-browser/src/local_file_utils.c +++ b/file-browser/src/local_file_utils.c @@ -10,13 +10,13 @@ static int nfw_callback(const char *fpath, const struct stat *sb, int typeflag); size_t file_get_full_path_memory_size(fileaccess_state_t state, directory_t *directory, file_t *file) { - return strlen(file->name) + strlen(state.state) + strlen(file->name) + 3; + return strlen(file->name) + strlen(state.payload.local.path) + strlen(file->name) + 3; } bool file_get_full_path(fileaccess_state_t state, directory_t *directory, file_t *file, char *out) { - char base_path[path_join_memory_size(state.state, directory->path)]; - if (!path_join((char*)state.state, directory->path, base_path)) { + char base_path[path_join_memory_size(state.payload.local.path, directory->path)]; + if (!path_join((char*)state.payload.local.path, directory->path, base_path)) { return false; }