From 4c1103881ad8516a60943d34383aeb819fdc13a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Tue, 29 Jun 2021 15:56:36 +0200 Subject: [PATCH] refactor: split local file access --- file-browser/include/local_file_utils.h | 11 ++++ file-browser/src/local_file_access.c | 80 +--------------------- file-browser/src/local_file_utils.c | 88 +++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 79 deletions(-) diff --git a/file-browser/include/local_file_utils.h b/file-browser/include/local_file_utils.h index c17af9392f15b26803acfdb5ad8ada03e61b2e3b..43e30cee71252c6add7ee6efdfbb83e06bbd6be4 100644 --- a/file-browser/include/local_file_utils.h +++ b/file-browser/include/local_file_utils.h @@ -1,5 +1,6 @@ #include "file_access.h" #include +#include "dirent.h" int file_delete(const char *path); int directory_delete(const char *path); @@ -8,3 +9,13 @@ size_t file_get_full_path_memory_size(fileaccess_state_t state, directory_t *directory, file_t *file); bool file_get_full_path(fileaccess_state_t state, directory_t *directory, file_t *file, char *dest); + +file_operation_error_t file_get_information(void *malloced, + uint64_t *file_offset, + uint64_t *names_offset, + fileaccess_state_t state, + file_t file); + +file_operation_error_t directory_list(fileaccess_state_t state, void *malloced, + char *show_path, uint32_t files_count, + DIR *dirptr, directory_t *directory); diff --git a/file-browser/src/local_file_access.c b/file-browser/src/local_file_access.c index b0931413b5e177a3668e434aa55f7d57a791fea2..256baa4078bbe61f22aa4dd84449a383e0a99efa 100644 --- a/file-browser/src/local_file_access.c +++ b/file-browser/src/local_file_access.c @@ -40,44 +40,6 @@ static uint64_t directory_get_needed_bytes(char *name, uint32_t *dirs_count, DIR return size; } -static file_operation_error_t file_get_information(void *malloced, - uint64_t *file_offset, - uint64_t *names_offset, - fileaccess_state_t state, - file_t file) { - size_t name_len = strlen(file.name); - - char full_path[file_get_full_path_memory_size(state, file.directory, &file)]; - file_get_full_path(state, file.directory, &file, full_path); - - // load info - struct stat stats; - int status = stat(full_path, &stats); - - if (status == -1) { - file.size = 0; - file.gid = 0; - file.uid = 0; - file.permissions = 0; - file.modify_time = 0; - } else { - file.size = stats.st_size; - file.gid = stats.st_gid; - file.uid = stats.st_uid; - file.permissions = stats.st_mode; - file.modify_time = stats.st_mtim.tv_sec; - } - - file_t *stored = malloced + *file_offset; - *stored = file; - *file_offset += sizeof(file_t); - - strcpy(malloced + *names_offset, file.name); - stored->name = malloced + *names_offset; - *names_offset += name_len + 1; - - return FILOPER_SUCCESS; -} static int compare_files(const void *a, const void *b) { const file_t *file_a = (const file_t*)a; @@ -104,8 +66,6 @@ directory_or_error_t local_fileaccess_directory_list(fileaccess_state_t state, uint32_t files_count = 0; uint64_t size = directory_get_needed_bytes(show_path, &files_count, dirptr); - uint64_t files_offset = sizeof(directory_t) + strlen(show_path) + 1; - uint64_t names_offset = files_count * sizeof(file_t) + files_offset; directory_t *directory = malloc(size); void *malloced = directory; @@ -115,45 +75,7 @@ directory_or_error_t local_fileaccess_directory_list(fileaccess_state_t state, return ret; } - directory->path = malloced + sizeof(directory_t); - directory->files = malloced + files_offset; - directory->files_count = 0; - strcpy(directory->path, show_path); - - struct dirent * dir; - errno = 0; - while ((dir = readdir(dirptr)) != NULL) { - file_t file; - file.directory = directory; - file.name = dir->d_name; - - switch(dir->d_type) { - case DT_DIR: - file.type = FT_FOLDER; - break; - case DT_REG: - file.type = FT_FILE; - break; - case DT_UNKNOWN: - file.type = FT_UNKNOWN; - break; - default: - file.type = FT_OTHER; - break; - } - - ret.payload.error = file_get_information(malloced, &files_offset, - &names_offset, state, file); - errno = 0; - if (ret.payload.error != FILOPER_SUCCESS) { - ret.error = true; - return ret; - } - - directory->files_count++; - } - - closedir(dirptr); + directory_list(state, malloced, show_path, files_count, dirptr, directory); if (errno != 0) { free(malloced); diff --git a/file-browser/src/local_file_utils.c b/file-browser/src/local_file_utils.c index 21b2430806d10eade5d1e15dcb18c8ccd2f53ead..11172ebbe4a14a22bbe10066ac48640d51c20ea2 100644 --- a/file-browser/src/local_file_utils.c +++ b/file-browser/src/local_file_utils.c @@ -6,6 +6,7 @@ #include #include #include +#include "dirent.h" static int nfw_callback(const char *fpath, const struct stat *sb, int typeflag); @@ -62,3 +63,90 @@ static int nfw_callback(const char *fpath, const struct stat *sb, } } + +file_operation_error_t file_get_information(void *malloced, + uint64_t *file_offset, + uint64_t *names_offset, + fileaccess_state_t state, + file_t file) { + size_t name_len = strlen(file.name); + + char full_path[file_get_full_path_memory_size(state, file.directory, &file)]; + file_get_full_path(state, file.directory, &file, full_path); + + // load info + struct stat stats; + int status = stat(full_path, &stats); + + if (status == -1) { + file.size = 0; + file.gid = 0; + file.uid = 0; + file.permissions = 0; + file.modify_time = 0; + } else { + file.size = stats.st_size; + file.gid = stats.st_gid; + file.uid = stats.st_uid; + file.permissions = stats.st_mode; + file.modify_time = stats.st_mtim.tv_sec; + } + + file_t *stored = malloced + *file_offset; + *stored = file; + *file_offset += sizeof(file_t); + + strcpy(malloced + *names_offset, file.name); + stored->name = malloced + *names_offset; + *names_offset += name_len + 1; + + return FILOPER_SUCCESS; +} + +file_operation_error_t directory_list(fileaccess_state_t state, void *malloced, + char *show_path, uint32_t files_count, + DIR *dirptr, directory_t *directory) { + uint64_t files_offset = sizeof(directory_t) + strlen(show_path) + 1; + uint64_t names_offset = files_count * sizeof(file_t) + files_offset; + + file_operation_error_t error = FILOPER_SUCCESS; + directory->path = malloced + sizeof(directory_t); + directory->files = malloced + files_offset; + directory->files_count = 0; + strcpy(directory->path, show_path); + + struct dirent *dir; + errno = 0; + while ((dir = readdir(dirptr)) != NULL) { + file_t file; + file.directory = directory; + file.name = dir->d_name; + + switch (dir->d_type) { + case DT_DIR: + file.type = FT_FOLDER; + break; + case DT_REG: + file.type = FT_FILE; + break; + case DT_UNKNOWN: + file.type = FT_UNKNOWN; + break; + default: + file.type = FT_OTHER; + break; + } + + error = file_get_information(malloced, &files_offset, + &names_offset, state, file); + errno = 0; + if (error != FILOPER_SUCCESS) { + return error; + } + + directory->files_count++; + } + + closedir(dirptr); + return error; +}