diff --git a/Makefile b/Makefile index 6e973aa..f941220 100644 --- a/Makefile +++ b/Makefile @@ -174,6 +174,16 @@ $(ISO_IMAGE): $(KERNEL_ELF) limine.conf limine-setup fi \ done + mkdir -p $(ISO_DIR)/docs + @for f in $$(find docs -name '*.md'); do \ + if [ -f "$$f" ]; then \ + dir=$$(dirname "$$f"); \ + mkdir -p $(ISO_DIR)/"$$dir"; \ + cp "$$f" $(ISO_DIR)/"$$dir"/; \ + echo " module_path: boot():/$$f" >> $(ISO_DIR)/limine.conf; \ + fi \ + done + cp limine/limine-bios.sys $(ISO_DIR)/ cp limine/limine-bios-cd.bin $(ISO_DIR)/ cp limine/limine-uefi-cd.bin $(ISO_DIR)/ diff --git a/src/core/main.c b/src/core/main.c index 37477e2..99798fb 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -93,6 +93,32 @@ void serial_write_hex(uint64_t n) { } // Kernel Entry Point + +static void fat32_mkdir_recursive(const char *path) { + char temp[256]; + int i = 0; + + // Skip initial slash + if (path[0] == '/') { + temp[0] = '/'; + i = 1; + } + + while (path[i] && i < 255) { + temp[i] = path[i]; + if (path[i] == '/') { + temp[i] = '\0'; + fat32_mkdir(temp); + temp[i] = '/'; + } + i++; + } + if (i > 0 && temp[i-1] != '/') { + temp[i] = '\0'; + fat32_mkdir(temp); + } +} + void kmain(void) { init_serial(); serial_write("\n[DEBUG] Entering kmain...\n"); @@ -158,6 +184,7 @@ void kmain(void) { fat32_mkdir("/Library/images/gif"); fat32_mkdir("/Library/Fonts"); fat32_mkdir("/Library/DOOM"); + fat32_mkdir("/docs"); if (module_request.response == NULL) { serial_write("[DEBUG] ERROR: Limine Module Response is NULL!\n"); @@ -172,6 +199,17 @@ void kmain(void) { if (fs_starts_with(clean_path, "boot():")) clean_path += 7; else if (fs_starts_with(clean_path, "boot:///")) clean_path += 8; + char dir_path[256]; + int last_slash = -1; + for (int j = 0; clean_path[j]; j++) { + if (clean_path[j] == '/') last_slash = j; + } + if (last_slash > 0) { + for (int j = 0; j < last_slash; j++) dir_path[j] = clean_path[j]; + dir_path[last_slash] = '\0'; + fat32_mkdir_recursive(dir_path); + } + FAT32_FileHandle *fh = fat32_open(clean_path, "w"); if (fh && fh->valid) { fat32_write(fh, mod->address, mod->size); diff --git a/src/wm/explorer.c b/src/wm/explorer.c index 06abbaf..f297f97 100644 --- a/src/wm/explorer.c +++ b/src/wm/explorer.c @@ -314,10 +314,18 @@ static void dialog_confirm_create_folder(Window *win) { bool explorer_delete_permanently(const char *path) { if (fat32_is_directory(path)) { - FAT32_FileInfo *entries = (FAT32_FileInfo*)kmalloc(64 * sizeof(FAT32_FileInfo)); + int capacity = 64; + FAT32_FileInfo *entries = (FAT32_FileInfo*)kmalloc(capacity * sizeof(FAT32_FileInfo)); if (!entries) return false; - int count = fat32_list_directory(path, entries, 64); + int count = fat32_list_directory(path, entries, capacity); + while (count == capacity) { + capacity *= 2; + FAT32_FileInfo *new_entries = (FAT32_FileInfo*)krealloc(entries, capacity * sizeof(FAT32_FileInfo)); + if (!new_entries) { kfree(entries); return false; } + entries = new_entries; + count = fat32_list_directory(path, entries, capacity); + } for (int i = 0; i < count; i++) { if (explorer_strcmp(entries[i].name, ".") == 0 || explorer_strcmp(entries[i].name, "..") == 0) continue; @@ -420,10 +428,18 @@ bool explorer_clipboard_has_content(void) { static bool explorer_copy_recursive(const char *src_path, const char *dest_path) { if (fat32_is_directory(src_path)) { if (!fat32_mkdir(dest_path)) return false; - FAT32_FileInfo *files = (FAT32_FileInfo*)kmalloc(64 * sizeof(FAT32_FileInfo)); + int capacity = 64; + FAT32_FileInfo *files = (FAT32_FileInfo*)kmalloc(capacity * sizeof(FAT32_FileInfo)); if (!files) return false; - int count = fat32_list_directory(src_path, files, 64); + int count = fat32_list_directory(src_path, files, capacity); + while (count == capacity) { + capacity *= 2; + FAT32_FileInfo *new_files = (FAT32_FileInfo*)krealloc(files, capacity * sizeof(FAT32_FileInfo)); + if (!new_files) { kfree(files); return false; } + files = new_files; + count = fat32_list_directory(src_path, files, capacity); + } for (int i = 0; i < count; i++) { if (explorer_strcmp(files[i].name, ".") == 0 || explorer_strcmp(files[i].name, "..") == 0) continue; @@ -646,13 +662,29 @@ static void explorer_load_directory(Window *win, const char *path) { state->item_count = 0; - FAT32_FileInfo *entries = (FAT32_FileInfo*)kmalloc(EXPLORER_MAX_FILES * sizeof(FAT32_FileInfo)); + int capacity = EXPLORER_INITIAL_CAPACITY; + FAT32_FileInfo *entries = (FAT32_FileInfo*)kmalloc(capacity * sizeof(FAT32_FileInfo)); if (!entries) return; - int count = fat32_list_directory(path, entries, EXPLORER_MAX_FILES); + int count = fat32_list_directory(path, entries, capacity); + while (count == capacity) { + capacity *= 2; + FAT32_FileInfo *new_entries = (FAT32_FileInfo*)krealloc(entries, capacity * sizeof(FAT32_FileInfo)); + if (!new_entries) { kfree(entries); return; } + entries = new_entries; + count = fat32_list_directory(path, entries, capacity); + } + + if (state->items_capacity < count) { + int new_cap = count < EXPLORER_INITIAL_CAPACITY ? EXPLORER_INITIAL_CAPACITY : count; + ExplorerItem *new_items = (ExplorerItem*)krealloc(state->items, new_cap * sizeof(ExplorerItem)); + if (!new_items) { kfree(entries); return; } + state->items = new_items; + state->items_capacity = new_cap; + } int temp_count = 0; - for (int i = 0; i < count && temp_count < EXPLORER_MAX_FILES; i++) { + for (int i = 0; i < count; i++) { if (explorer_strcmp(entries[i].name, ".color") == 0) { continue; } @@ -1820,6 +1852,8 @@ Window* explorer_create_window(const char *path) { win->handle_right_click = explorer_handle_right_click; win->data = state; + state->items = NULL; + state->items_capacity = 0; state->selected_item = -1; state->last_clicked_item = -1; state->explorer_scroll_row = 0; @@ -1854,6 +1888,8 @@ void explorer_init(void) { win_explorer.handle_right_click = explorer_handle_right_click; win_explorer.data = state; + state->items = NULL; + state->items_capacity = 0; state->selected_item = -1; state->last_clicked_item = -1; state->explorer_scroll_row = 0; diff --git a/src/wm/explorer.h b/src/wm/explorer.h index 0d7d19c..b462e6c 100644 --- a/src/wm/explorer.h +++ b/src/wm/explorer.h @@ -15,7 +15,7 @@ extern Window win_cmd; extern Window win_notepad; extern Window win_markdown; -#define EXPLORER_MAX_FILES 64 +#define EXPLORER_INITIAL_CAPACITY 256 #define DIALOG_INPUT_MAX 256 typedef struct { @@ -26,7 +26,8 @@ typedef struct { } ExplorerItem; typedef struct { - ExplorerItem items[EXPLORER_MAX_FILES]; + ExplorerItem *items; + int items_capacity; int item_count; int selected_item; char current_path[FAT32_MAX_PATH];