From bed0cd45e6536010d01563a2bbd0e02a8501de0b Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Sun, 17 Dec 2023 00:47:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20IMFS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/limine.cfg | 3 + modules/imfs/build.sh | 17 +++++ modules/imfs/main.c | 158 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 modules/imfs/build.sh create mode 100644 modules/imfs/main.c diff --git a/configs/limine.cfg b/configs/limine.cfg index 8f40147..24afca9 100644 --- a/configs/limine.cfg +++ b/configs/limine.cfg @@ -36,5 +36,8 @@ TERM_WALLPAPER=boot:///boot.jpg MODULE_PATH=boot:///mod/tga.ko MODULE_CMDLINE=[MOD]tga.ko + MODULE_PATH=boot:///mod/imfs.ko + MODULE_CMDLINE=[MOD]imfs.ko + MODULE_PATH=boot:///boot.tga MODULE_CMDLINE=[BOOTIMG] \ No newline at end of file diff --git a/modules/imfs/build.sh b/modules/imfs/build.sh new file mode 100644 index 0000000..a151d04 --- /dev/null +++ b/modules/imfs/build.sh @@ -0,0 +1,17 @@ +#/bin/sh +echo "Название: IMFS" +echo "Лицензия: CC BY-NC 4.0" + +CC="gcc" +ARCH_FLAGS="-ffreestanding -O0 -g -fPIC -static -nostdlib " + +if [ -d "../../sdk" ]; then + CC="../../sdk/bin/x86_64-elf-gcc" +fi + + +$CC $ARCH_FLAGS -I../../modlib -finput-charset=UTF-8 -fexec-charset=cp1251 -c main.c -o imfs.o +$CC $ARCH_FLAGS -T ../link.ld -Wl,--entry=init,--build-id=none imfs.o -o imfs.ko + +cp imfs.ko ../bin/ +echo "Сборка завершена, файл: imfs.ko" diff --git a/modules/imfs/main.c b/modules/imfs/main.c new file mode 100644 index 0000000..49d3657 --- /dev/null +++ b/modules/imfs/main.c @@ -0,0 +1,158 @@ +#include + +#define MAX_FILE_NAME_LEN 256 +#define MAX_FILE_TYPE_LEN 16 + +typedef unsigned char folder_type_t; + +typedef struct file_t { + char *data; + size_t size; + char name[MAX_FILE_NAME_LEN]; + char type[MAX_FILE_TYPE_LEN]; + struct file_t *next; +} file_t; + +typedef struct folder_t { + char name[MAX_FILE_NAME_LEN]; + struct folder_t *parent; + struct folder_t *child; + struct folder_t *next; + file_t *files; +} folder_t; + +folder_t *root_folder = NULL; + +folder_t *create_folder(char *name, folder_t *parent) { + folder_t *folder = alloc(sizeof(folder_t)); + strcpy(folder->name, name); + folder->parent = parent; + folder->child = NULL; + folder->next = NULL; + folder->files = NULL; + + if (parent == NULL) { + root_folder = folder; + } else { + if (parent->child == NULL) { + parent->child = folder; + } else { + folder_t *current_folder = parent->child; + while (current_folder->next != NULL) { current_folder = current_folder->next; } + current_folder->next = folder; + } + } + + return folder; +} + +file_t *create_file(char *name, char *type, folder_t *parent) { + file_t *file = alloc(sizeof(file_t)); + + file->data = NULL; + file->size = 0; + strcpy(file->name, name); + strcpy(file->type, type); + file->next = NULL; + + if (parent->files == NULL) { + parent->files = file; + } else { + file_t *current_file = parent->files; + while (current_file->next != NULL) { current_file = current_file->next; } + current_file->next = file; + } + + return file; +} + +void write_file(file_t *file, void *data, size_t size) { + file->data = realloc(file->data, file->size + size); + memcpy(file->data + file->size, data, size); + file->size += size; +} + +void read_file(file_t *file, char *buffer, size_t size) { + memcpy(buffer, file->data, size); +} + +void delete_file(file_t *file) { + if (file->data != NULL) { free(file->data); } + if (file != NULL) { free(file); } +} + +void delete_folder(folder_t *folder) { + if (folder == NULL) { return; } + + while (folder->files != NULL) { + file_t *file = folder->files; + folder->files = file->next; + delete_file(file); + } + + while (folder->child != NULL) { + folder_t *child_folder = folder->child; + folder->child = child_folder->next; + delete_folder(child_folder); + } + + if (folder->parent == NULL) { + root_folder = NULL; + } else { + folder_t *parent_folder = folder->parent; + if (parent_folder->child == folder) { + parent_folder->child = folder->next; + } else { + folder_t *current_folder = parent_folder->child; + while (current_folder->next != folder) { current_folder = current_folder->next; } + current_folder->next = folder->next; + } + } + + free(folder); +} + +void print_folder_contents(folder_t *folder, size_t depth) { + for (size_t i = 0; i < depth; i++) { fb_printf("\t"); } + fb_printf("- %s/\n", folder->name); + + file_t *file = folder->files; + + while (file != NULL) { + for (size_t i = 0; i < depth + 1; i++) { fb_printf("\t"); } + fb_printf("- %s.%s\n", file->name, file->type); + file = file->next; + } + + folder_t *child_folder = folder->child; + + while (child_folder != NULL) { + print_folder_contents(child_folder, depth + 1); + child_folder = child_folder->next; + } +} + +module_info_t __attribute__((section(".minit"))) init(env_t *env) { + init_env(env); + create_folder("", NULL); + + folder_t *cache_f = create_folder("cache", root_folder); + folder_t *docs_f = create_folder("docs", root_folder); + + create_file("readme", "text", root_folder); + + print_folder_contents(root_folder, 0); + + return (module_info_t){ + .name = (char *)"[FS][IMFS]", + .message = (char *)"IMFS (in memory filesystem) - файловая система работающая исключительно в ОЗУ.", + .type = 0, + .data_size = 0, + .data = (void *)0, + .err_code = 0, + .module_id = 0, + .irq = 0, + .irq_handler = 0, + .get_func = 0 + }; +}