From 90788e842f36175ab31e594d004314efae39a7ad Mon Sep 17 00:00:00 2001 From: Daniel Hammer Date: Wed, 27 May 2026 18:06:27 +0200 Subject: [PATCH] fix: fix VFS Delete op for Ramdisk driver --- kernel/src/Fs/Ramdisk.cpp | 42 +++++++++++++------ .../src/desktop/apps/filemanager/actions.cpp | 17 ++++---- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/kernel/src/Fs/Ramdisk.cpp b/kernel/src/Fs/Ramdisk.cpp index 18129f6..167153b 100644 --- a/kernel/src/Fs/Ramdisk.cpp +++ b/kernel/src/Fs/Ramdisk.cpp @@ -374,24 +374,40 @@ namespace Fs::Ramdisk { int Delete(const char* path) { if (path == nullptr) return -1; - if (path[0] == '/') path++; - for (int i = 0; i < fileCount; i++) { - if (StrEqual(fileTable[i].name, path)) { - // Free heap-allocated data - if (fileTable[i].heapAllocated && fileTable[i].data) { - Memory::g_heap->Free(fileTable[i].data); - } + int idx = FindEntryByPath(path); + if (idx < 0) return -1; - // Shift remaining entries down - for (int j = i; j < fileCount - 1; j++) { - fileTable[j] = fileTable[j + 1]; + // For directories, refuse if any other entry lives under this prefix. + if (fileTable[idx].isDirectory) { + char normalized[MaxNameLen]; + int normLen = NormalizePath(path, normalized, MaxNameLen); + if (normLen < 0) return -1; + if (normLen + 1 >= MaxNameLen) return -1; + normalized[normLen] = '/'; + normalized[normLen + 1] = '\0'; + + for (int i = 0; i < fileCount; i++) { + if (i == idx) continue; + const char* name = fileTable[i].name; + if (name[0] == '/') name++; + bool prefixMatch = true; + for (int k = 0; k <= normLen; k++) { + if (name[k] != normalized[k]) { prefixMatch = false; break; } } - fileCount--; - return 0; + if (prefixMatch && name[normLen + 1] != '\0') return -1; } } - return -1; // not found + + if (fileTable[idx].heapAllocated && fileTable[idx].data) { + Memory::g_heap->Free(fileTable[idx].data); + } + + for (int j = idx; j < fileCount - 1; j++) { + fileTable[j] = fileTable[j + 1]; + } + fileCount--; + return 0; } int Mkdir(const char* path) { diff --git a/programs/src/desktop/apps/filemanager/actions.cpp b/programs/src/desktop/apps/filemanager/actions.cpp index 35608e1..d6bea98 100644 --- a/programs/src/desktop/apps/filemanager/actions.cpp +++ b/programs/src/desktop/apps/filemanager/actions.cpp @@ -108,14 +108,15 @@ void filemanager_finish_rename(FileManagerState* fm) { filemanager_build_fullpath(new_path, 512, fm->current_path, fm->rename_buf); - if (fm->is_dir[fm->rename_idx]) { - // Directory rename: copy recursively then delete old - if (filemanager_copy_dir_recursive(old_path, new_path)) - filemanager_delete_recursive(old_path); - } else { - // File rename: copy then delete old - if (filemanager_copy_file(old_path, new_path)) - montauk::fdelete(old_path); + if (montauk::frename(old_path, new_path) != 0) { + // Fall back to copy + delete for drivers that don't support rename + if (fm->is_dir[fm->rename_idx]) { + if (filemanager_copy_dir_recursive(old_path, new_path)) + filemanager_delete_recursive(old_path); + } else { + if (filemanager_copy_file(old_path, new_path)) + montauk::fdelete(old_path); + } } filemanager_read_dir(fm);