hos/kernel/fs/vfs.cpp

89 lines
1.7 KiB
C++

// vfs.cpp
// Virtual file system subsystem for HOS
// Author: Josh Holtrop
// Date: 05/10/05
// Modified: 05/10/05
#define _HOS_CPP_ _HOS_CPP_
extern "C" {
#include "hos_defines.h"
#include "display/kout.h"
}
#include "vfs.h"
#include "fs/FileSystem.h"
#include "fs/VFSMount.h"
#include "fs/ext2/ext2.h"
#include "lang/vector.h"
#include "lang/string.h"
#include "devices.h"
inode_num_t rootInode;
vector<VFSMount> *mountPoints;
vector<FSHandle> *fses;
int vfs_init()
{
mountPoints = new vector<VFSMount>;
fses = new vector<FSHandle>;
ext2_init();
return 0;
}
int vfs_register(char *fs, FileSystem *(*mount_func)(device_t))
{
string fsName(fs);
if (vfs_get_mount_func(fsName))
return -1;
FSHandle fsh = {fsName, mount_func};
fses->add(fsh);
return 0;
}
int vfs_mount(device_t device, char *fsType, char *mountPoint)
{
string mountPt(mountPoint);
string fsName(fsType);
if (!vfs_get_mount_func(fsName))
return -501;
if (mountPt == "/")
{
if (mountPoints->size())
{
kprintf("/ must be the first filesystem mounted!\n");
return -1;
}
FileSystem *fs = vfs_attempt_mount(device, fsType);
if (fs == NULL)
return -2;
rootInode = fs->getRootInodeNumber();
mountPoints->add(VFSMount(device, fs, mountPt, 0));
return 0;
}
else
{
// TODO: mount a second filesystem
}
return 500;
}
FileSystem *vfs_attempt_mount(device_t device, char *fsType)
{
mount_func_t mount_func;
if (( mount_func = vfs_get_mount_func(string(fsType)) ))
return mount_func(device);
else
return NULL;
}
mount_func_t vfs_get_mount_func(const string & fsName)
{
for (unsigned int i = 0; i < fses->size(); i++)
{
if (fsName == (*fses)[i].name)
return (*fses)[i].mount_func;
}
return NULL;
}