M file-browser/include/local_file_utils.h => file-browser/include/local_file_utils.h +11 -0
@@ 1,5 1,6 @@
#include "file_access.h"
#include <stdbool.h>
+#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);
M file-browser/src/local_file_access.c => file-browser/src/local_file_access.c +1 -79
@@ 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);
M file-browser/src/local_file_utils.c => file-browser/src/local_file_utils.c +88 -0
@@ 6,6 6,7 @@
#include <ftw.h>
#include <string.h>
#include <stdio.h>
+#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;
+}