fix(kernel/vfs): support multiple roots

This commit is contained in:
2025-04-08 05:04:04 +00:00
parent a1b58bacd8
commit 3315d79742
10 changed files with 118 additions and 39 deletions

View File

@ -175,12 +175,13 @@ namespace vfs
return nullptr;
}
FileNode *Virtual::CacheLookup(const char *Path)
FileNode *Virtual::CacheLookup(FileNode *Parent, const char *Path)
{
debug("Cache lookup for \"%s\"", Path);
FileNode *rootNode = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0);
if (Parent == nullptr)
Parent = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0);
FileNode *ret = CacheRecursiveSearch(rootNode, Path, false);
FileNode *ret = CacheRecursiveSearch(Parent, Path, false);
if (ret)
return ret;

View File

@ -137,14 +137,22 @@ namespace vfs
{
char *path = strdup(Path);
char *lastSlash = strrchr(path, '/');
if (lastSlash == path)
lastSlash++;
*lastSlash = '\0';
if (lastSlash)
{
if (lastSlash == path)
lastSlash++;
*lastSlash = '\0';
}
FileNode *parentNode = this->GetByPath(path, Parent);
if (parentNode == nullptr && Parent != nullptr)
parentNode = Parent;
free(path);
lastSlash = strrchr(Path, '/');
lastSlash++;
if (lastSlash)
lastSlash++;
else
lastSlash = (char *)Path;
return this->CreateCacheNode(parentNode, Node, lastSlash, Node->Mode);
}
@ -160,8 +168,13 @@ namespace vfs
FileNode *Virtual::GetByPath(const char *Path, FileNode *Parent)
{
debug("GetByPath: %s", Path);
if (Parent == nullptr || this->PathIsAbsolute(Path))
Parent = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0);
if (Parent == nullptr)
{
if (fs->PathIsRelative(Path))
Parent = thisProcess ? thisProcess->CWD : thisProcess->Info.RootNode;
else
Parent = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0);
}
if (strcmp(Path, ".") == 0)
return Parent;
@ -214,6 +227,7 @@ namespace vfs
auto it = DeviceMap.find(__Parent->Node->Device);
if (unlikely(it == DeviceMap.end()))
ReturnLogError(nullptr, "Device %d not found", __Parent->Node->Device);
debug("found fs %s", it->second.fsi->Name);
if (it->second.fsi->Ops.Lookup == NULL)
ReturnLogError(nullptr, "Lookup not supported for %d", it->first);
@ -321,7 +335,7 @@ namespace vfs
bool Virtual::PathExists(const char *Path, FileNode *Parent)
{
FileNode *fn = this->CacheLookup(Path);
FileNode *fn = this->CacheLookup(Parent, Path);
if (fn)
return true;

View File

@ -33,7 +33,7 @@ namespace vfs
cwk_path_get_basename(Name, &basename, &length);
if (basename == NULL)
{
if (strcmp(Name, "/") == 0)
if (strcmp(Name, RootName.c_str()) == 0)
{
auto &it = Files.at(0);
*Result = &it->Node;
@ -90,6 +90,7 @@ namespace vfs
RAMFSInode *node = new RAMFSInode;
node->Name.assign(basename, length);
node->Mode = Mode;
node->Node = inode;
auto file = Files.insert(std::make_pair(NextInode, node));
assert(file.second == true);
@ -383,25 +384,15 @@ O2 int __ramfs_Destroy(FileSystemInfo *fsi)
return 0;
}
bool MountRAMFS(Inode *Parent, const char *Name, size_t Index)
bool MountRAMFS(FileNode *Parent, const char *Name, size_t Index)
{
vfs::RAMFS *ramfs = new vfs::RAMFS;
ramfs->DeviceID = fs->EarlyReserveDevice();
if (Parent == nullptr)
{
ramfs->Create(nullptr, Name, S_IFDIR | 0755, &Parent);
if (Parent == nullptr)
{
error("Failed to create root inode");
delete ramfs;
return false;
}
}
ramfs->RootName.assign(Name);
FileSystemInfo *fsi = new FileSystemInfo;
fsi->Name = "ramfs";
fsi->RootName = "/";
fsi->RootName = Name;
fsi->Flags = I_FLAG_ROOT | I_FLAG_MOUNTPOINT | I_FLAG_CACHE_KEEP;
fsi->SuperOps.DeleteInode = __ramfs_DestroyInode;
fsi->SuperOps.Destroy = __ramfs_Destroy;
@ -415,7 +406,12 @@ bool MountRAMFS(Inode *Parent, const char *Name, size_t Index)
fsi->Ops.Stat = __ramfs_Stat;
fsi->PrivateData = ramfs;
fs->LateRegisterFileSystem(ramfs->DeviceID, fsi, Parent);
fs->AddRootAt(Parent, Index);
Inode *root = nullptr;
ramfs->Create(nullptr, Name, S_IFDIR | 0755, &root);
fs->LateRegisterFileSystem(ramfs->DeviceID, fsi, root);
fs->AddRootAt(root, Index);
fs->Mount(Parent, root, Name);
return true;
}