1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/sortix/mount.cpp

142 lines
3.2 KiB
C++
Raw Normal View History

2011-08-05 08:25:00 -04:00
/******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along
with Sortix. If not, see <http://www.gnu.org/licenses/>.
mount.cpp
Handles system wide mount points and initialization of new file systems.
******************************************************************************/
#include <sortix/kernel/platform.h>
2011-11-18 11:49:31 -05:00
#include <libmaxsi/memory.h>
2011-11-20 11:07:34 -05:00
#include <libmaxsi/string.h>
#include <sortix/kernel/panic.h>
#include "mount.h"
2011-11-18 11:49:31 -05:00
#include "fs/ramfs.h"
2011-11-20 10:25:55 -05:00
#include "fs/initfs.h"
#include "fs/devfs.h"
2011-08-05 08:25:00 -04:00
2011-11-20 11:07:34 -05:00
using namespace Maxsi;
2011-08-05 08:25:00 -04:00
namespace Sortix
{
class MountPoint
{
public:
MountPoint(DevFileSystem* fs, char* path);
~MountPoint();
public:
MountPoint* prev;
MountPoint* next;
public:
DevFileSystem* fs;
char* path;
};
MountPoint::MountPoint(DevFileSystem* fs, char* path)
{
this->path = path;
this->fs = fs;
this->fs->Refer();
prev = NULL;
next = NULL;
}
MountPoint::~MountPoint()
{
fs->Unref();
delete[] path;
}
namespace Mount
2011-08-05 08:25:00 -04:00
{
MountPoint* root;
2011-11-18 11:49:31 -05:00
2011-11-20 11:07:34 -05:00
bool MatchesMountPath(const char* path, const char* mount)
{
size_t mountlen = String::Length(mount);
if ( !String::StartsWith(path, mount) ) { return false; }
int c = path[mountlen];
return c == '/' || c == '\0';
}
DevFileSystem* WhichFileSystem(const char* path, size_t* pathoffset)
{
DevFileSystem* result = NULL;
*pathoffset = 0;
for ( MountPoint* tmp = root; tmp; tmp = tmp->next )
2011-11-20 11:07:34 -05:00
{
if ( MatchesMountPath(path, tmp->path) )
{
result = tmp->fs;
*pathoffset = String::Length(tmp->path);
}
2011-11-20 11:07:34 -05:00
}
return result;
2011-11-20 11:07:34 -05:00
}
bool Register(DevFileSystem* fs, const char* path)
2011-08-05 08:25:00 -04:00
{
char* newpath = String::Clone(path);
if ( !newpath ) { return false; }
MountPoint* mp = new MountPoint(fs, newpath);
if ( !mp ) { delete[] newpath; return false; }
if ( !root ) { root = mp; return true; }
if ( String::Compare(path, root->path) < 0 )
2011-11-20 10:25:55 -05:00
{
mp->next = root;
root->prev = mp;
root = mp;
return false;
2011-11-20 10:25:55 -05:00
}
for ( MountPoint* tmp = root; tmp; tmp = tmp->next )
{
if ( tmp->next == NULL || String::Compare(path, tmp->next->path) < 0 )
{
mp->next = tmp->next;
tmp->next = mp;
return true;
}
}
return false; // Shouldn't happen.
2011-08-05 08:25:00 -04:00
}
void Init()
2011-08-05 08:25:00 -04:00
{
root = NULL;
DevFileSystem* rootfs = new DevRAMFS();
if ( !rootfs || !Register(rootfs, "") ) { Panic("Unable to allocate rootfs"); }
DevFileSystem* initfs = new DevInitFS();
if ( !initfs || !Register(initfs, "/bin") ) { Panic("Unable to allocate initfs"); }
DevFileSystem* devfs = new DevDevFS();
if ( !devfs || !Register(devfs, "/dev") ) { Panic("Unable to allocate devfs"); }
2011-08-05 08:25:00 -04:00
}
}
}