fix: fix VFS Delete op for Ramdisk driver
This commit is contained in:
+29
-13
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user