mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Added a RAM filesystem.
This commit is contained in:
parent
ae423eaeef
commit
013e11ca5e
9 changed files with 394 additions and 5 deletions
|
@ -48,6 +48,10 @@ namespace Maxsi
|
|||
const int ENODEV = 13;
|
||||
const int EWOULDBLOCK = 14;
|
||||
const int EBADF = 15;
|
||||
const int EOVERFLOW = 16;
|
||||
const int ENOENT = 17;
|
||||
const int ENOSPC = 18;
|
||||
const int EEXIST = 19;
|
||||
|
||||
extern int _errornumber;
|
||||
|
||||
|
|
|
@ -73,6 +73,7 @@ io.o \
|
|||
pipe.o \
|
||||
filesystem.o \
|
||||
mount.o \
|
||||
fs/ramfs.o \
|
||||
../libmaxsi/libmaxsi-sortix.a
|
||||
|
||||
JSOBJS:=$(subst .o,-js.o,$(OBJS))
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace Sortix
|
|||
static const unsigned STREAM = 0;
|
||||
static const unsigned BUFFER = 1;
|
||||
static const unsigned VGABUFFER = 2;
|
||||
static const unsigned FILESYSTEM = 3;
|
||||
|
||||
public:
|
||||
Device();
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
******************************************************************************/
|
||||
|
||||
#include "platform.h"
|
||||
#include <libmaxsi/memory.h>
|
||||
#include "syscall.h"
|
||||
#include "process.h"
|
||||
#include "filesystem.h"
|
||||
|
@ -47,7 +48,7 @@ namespace Sortix
|
|||
if ( !dev ) { return -1; /* TODO: errno */ }
|
||||
int fd = process->descriptors.Allocate(dev);
|
||||
if ( fd < 0 ) { dev->Unref(); }
|
||||
return fd;
|
||||
return fd;
|
||||
}
|
||||
|
||||
void Init()
|
||||
|
@ -55,6 +56,78 @@ namespace Sortix
|
|||
Syscall::Register(SYSCALL_OPEN, (void*) SysOpen);
|
||||
}
|
||||
}
|
||||
|
||||
DevFileWrapper::DevFileWrapper(DevBuffer* buffer, int flags)
|
||||
{
|
||||
this->buffer = buffer;
|
||||
this->flags = flags;
|
||||
this->offset = 0;
|
||||
this->buffer->Refer();
|
||||
}
|
||||
|
||||
DevFileWrapper::~DevFileWrapper()
|
||||
{
|
||||
buffer->Unref();
|
||||
}
|
||||
|
||||
size_t DevFileWrapper::BlockSize()
|
||||
{
|
||||
return buffer->BlockSize();
|
||||
}
|
||||
|
||||
uintmax_t DevFileWrapper::Size()
|
||||
{
|
||||
return buffer->Size();
|
||||
}
|
||||
|
||||
uintmax_t DevFileWrapper::Position()
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
bool DevFileWrapper::Seek(uintmax_t position)
|
||||
{
|
||||
if ( !buffer->Seek(position) ) { return false; }
|
||||
offset = position;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DevFileWrapper::Resize(uintmax_t size)
|
||||
{
|
||||
return buffer->Resize(size);
|
||||
}
|
||||
|
||||
ssize_t DevFileWrapper::Read(byte* dest, size_t count)
|
||||
{
|
||||
// TODO: Enforce read permission!
|
||||
if ( !buffer->Seek(offset) ) { return -1; }
|
||||
ssize_t result = buffer->Read(dest, count);
|
||||
if ( result < 0 ) { return result; }
|
||||
offset += result;
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t DevFileWrapper::Write(const byte* src, size_t count)
|
||||
{
|
||||
// TODO: Enforce write permission!
|
||||
if ( !buffer->Seek(offset) ) { return -1; }
|
||||
ssize_t result = buffer->Write(src, count);
|
||||
if ( result < 0 ) { return result; }
|
||||
offset += result;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DevFileWrapper::IsReadable()
|
||||
{
|
||||
// TODO: Enforce read permission!
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DevFileWrapper::IsWritable()
|
||||
{
|
||||
// TODO: Enforce write permission!
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#define SORTIX_FILESYSTEM_H
|
||||
|
||||
#include "device.h"
|
||||
#include "stream.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
|
@ -55,6 +56,32 @@ namespace Sortix
|
|||
public:
|
||||
virtual Device* Open(const char* path, int flags, mode_t mode) = 0;
|
||||
|
||||
public:
|
||||
virtual bool IsType(unsigned type) { return type == Device::FILESYSTEM; }
|
||||
|
||||
};
|
||||
|
||||
class DevFileWrapper : public DevBuffer
|
||||
{
|
||||
public:
|
||||
DevFileWrapper(DevBuffer* buffer, int flags);
|
||||
virtual ~DevFileWrapper();
|
||||
|
||||
private:
|
||||
DevBuffer* buffer;
|
||||
int flags;
|
||||
uintmax_t offset;
|
||||
|
||||
public:
|
||||
virtual ssize_t Read(byte* dest, size_t count);
|
||||
virtual ssize_t Write(const byte* src, size_t count);
|
||||
virtual bool IsReadable();
|
||||
virtual bool IsWritable();
|
||||
virtual size_t BlockSize();
|
||||
virtual uintmax_t Size();
|
||||
virtual uintmax_t Position();
|
||||
virtual bool Seek(uintmax_t position);
|
||||
virtual bool Resize(uintmax_t size);
|
||||
};
|
||||
|
||||
namespace FileSystem
|
||||
|
|
225
sortix/fs/ramfs.cpp
Normal file
225
sortix/fs/ramfs.cpp
Normal file
|
@ -0,0 +1,225 @@
|
|||
/******************************************************************************
|
||||
|
||||
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/>.
|
||||
|
||||
fs/ramfs.h
|
||||
A filesystem stored entirely in RAM.
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
#include "../platform.h"
|
||||
#include <libmaxsi/error.h>
|
||||
#include <libmaxsi/string.h>
|
||||
#include <libmaxsi/memory.h>
|
||||
#include "../filesystem.h"
|
||||
#include "../stream.h"
|
||||
#include "ramfs.h"
|
||||
|
||||
using namespace Maxsi;
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
class DevRAMFSFile : public DevBuffer
|
||||
{
|
||||
public:
|
||||
typedef DevBuffer BaseClass;
|
||||
|
||||
public:
|
||||
// Transfers ownership of name.
|
||||
DevRAMFSFile(char* name);
|
||||
virtual ~DevRAMFSFile();
|
||||
|
||||
public:
|
||||
char* name;
|
||||
|
||||
private:
|
||||
size_t offset;
|
||||
byte* buffer;
|
||||
size_t buffersize;
|
||||
|
||||
public:
|
||||
virtual size_t BlockSize();
|
||||
virtual uintmax_t Size();
|
||||
virtual uintmax_t Position();
|
||||
virtual bool Seek(uintmax_t position);
|
||||
virtual bool Resize(uintmax_t size);
|
||||
virtual ssize_t Read(byte* dest, size_t count);
|
||||
virtual ssize_t Write(const byte* src, size_t count);
|
||||
virtual bool IsReadable();
|
||||
virtual bool IsWritable();
|
||||
|
||||
};
|
||||
|
||||
DevRAMFSFile::DevRAMFSFile(char* name)
|
||||
{
|
||||
this->name = name;
|
||||
buffer = NULL;
|
||||
buffersize = 0;
|
||||
}
|
||||
|
||||
DevRAMFSFile::~DevRAMFSFile()
|
||||
{
|
||||
delete[] name;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
size_t DevRAMFSFile::BlockSize()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
uintmax_t DevRAMFSFile::Size()
|
||||
{
|
||||
return buffersize;
|
||||
}
|
||||
|
||||
uintmax_t DevRAMFSFile::Position()
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
bool DevRAMFSFile::Seek(uintmax_t position)
|
||||
{
|
||||
if ( SIZE_MAX < position ) { Error::Set(Error::EOVERFLOW); return false; }
|
||||
offset = position;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DevRAMFSFile::Resize(uintmax_t size)
|
||||
{
|
||||
if ( SIZE_MAX < size ) { Error::Set(Error::EOVERFLOW); return false; }
|
||||
byte* newbuffer = new byte[size];
|
||||
if ( !newbuffer ) { Error::Set(Error::ENOSPC); return false; }
|
||||
size_t sharedmemsize = ( size < buffersize ) ? size : buffersize;
|
||||
Memory::Copy(newbuffer, buffer, sharedmemsize);
|
||||
delete[] buffer;
|
||||
buffer = newbuffer;
|
||||
buffersize = size;
|
||||
return true;
|
||||
}
|
||||
|
||||
ssize_t DevRAMFSFile::Read(byte* dest, size_t count)
|
||||
{
|
||||
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; }
|
||||
size_t available = count;
|
||||
if ( buffersize < offset + count ) { available = buffersize - offset; }
|
||||
if ( available == 0 ) { return 0; }
|
||||
Memory::Copy(dest, buffer + offset, available);
|
||||
offset += available;
|
||||
return available;
|
||||
}
|
||||
|
||||
ssize_t DevRAMFSFile::Write(const byte* src, size_t count)
|
||||
{
|
||||
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; }
|
||||
if ( buffersize < offset + count )
|
||||
{
|
||||
uintmax_t newsize = (uintmax_t) offset + (uintmax_t) count;
|
||||
if ( !Resize(newsize) ) { return -1; }
|
||||
}
|
||||
|
||||
Memory::Copy(buffer + offset, src, count);
|
||||
offset += count;
|
||||
return count;
|
||||
}
|
||||
|
||||
bool DevRAMFSFile::IsReadable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DevRAMFSFile::IsWritable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
DevRAMFS::DevRAMFS()
|
||||
{
|
||||
files = NULL;
|
||||
}
|
||||
|
||||
DevRAMFS::~DevRAMFS()
|
||||
{
|
||||
if ( files )
|
||||
{
|
||||
while ( !files->Empty() ) { delete files->Remove(0); }
|
||||
delete files;
|
||||
}
|
||||
}
|
||||
|
||||
int CompareFiles(DevRAMFSFile* file1, DevRAMFSFile* file2)
|
||||
{
|
||||
return String::Compare(file1->name, file2->name);
|
||||
}
|
||||
|
||||
int LookupFile(DevRAMFSFile* file, const char* name)
|
||||
{
|
||||
return String::Compare(file->name, name);
|
||||
}
|
||||
|
||||
Device* DevRAMFS::Open(const char* path, int flags, mode_t mode)
|
||||
{
|
||||
DevBuffer* file = OpenFile(path, flags, mode);
|
||||
if ( !file ) { return NULL; }
|
||||
Device* wrapper = new DevFileWrapper(file, flags);
|
||||
if ( !wrapper ) { Error::Set(Error::ENOSPC); return NULL; }
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
DevBuffer* DevRAMFS::OpenFile(const char* path, int flags, mode_t mode)
|
||||
{
|
||||
if ( *path++ != '/' ) { return NULL; }
|
||||
|
||||
if ( files )
|
||||
{
|
||||
size_t fileindex = files->Search(LookupFile, path);
|
||||
if ( fileindex != SIZE_MAX ) { return files->Get(fileindex); }
|
||||
}
|
||||
|
||||
return CreateFile(path, flags, mode);
|
||||
}
|
||||
|
||||
DevBuffer* DevRAMFS::CreateFile(const char* path, int flags, mode_t mode)
|
||||
{
|
||||
if ( !(flags & O_CREAT) ) { Error::Set(Error::ENOENT); return NULL; }
|
||||
|
||||
if ( !files )
|
||||
{
|
||||
files = new SortedList<DevRAMFSFile*>(CompareFiles);
|
||||
if ( !files) { Error::Set(Error::ENOSPC); return NULL; }
|
||||
}
|
||||
|
||||
if ( files->Search(LookupFile, path) != SIZE_MAX )
|
||||
{
|
||||
Error::Set(Error::EEXIST);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* newpath = String::Clone(path);
|
||||
if ( !newpath ) { Error::Set(Error::ENOSPC); return NULL; }
|
||||
|
||||
DevRAMFSFile* file = new DevRAMFSFile(newpath);
|
||||
if ( !file ) { delete[] newpath; Error::Set(Error::ENOSPC); return NULL; }
|
||||
if ( !files->Add(file) ) { delete file; Error::Set(Error::ENOSPC); return NULL; }
|
||||
|
||||
file->Refer();
|
||||
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
55
sortix/fs/ramfs.h
Normal file
55
sortix/fs/ramfs.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
/******************************************************************************
|
||||
|
||||
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/>.
|
||||
|
||||
fs/ramfs.h
|
||||
A filesystem stored entirely in RAM.
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef SORTIX_FS_RAMFS_H
|
||||
#define SORTIX_FS_RAMFS_H
|
||||
|
||||
#include <libmaxsi/sortedlist.h>
|
||||
#include "../filesystem.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
class DevRAMFSFile;
|
||||
|
||||
class DevRAMFS : public DevFileSystem
|
||||
{
|
||||
public:
|
||||
DevRAMFS();
|
||||
virtual ~DevRAMFS();
|
||||
|
||||
public:
|
||||
virtual Device* Open(const char* path, int flags, mode_t mode);
|
||||
|
||||
private:
|
||||
Maxsi::SortedList<DevRAMFSFile*>* files;
|
||||
|
||||
private:
|
||||
virtual DevBuffer* OpenFile(const char* path, int flags, mode_t mode);
|
||||
virtual DevBuffer* CreateFile(const char* path, int flags, mode_t mode);
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -23,22 +23,25 @@
|
|||
******************************************************************************/
|
||||
|
||||
#include "platform.h"
|
||||
#include <libmaxsi/memory.h>
|
||||
#include "mount.h"
|
||||
#include "fs/ramfs.h"
|
||||
|
||||
namespace Sortix
|
||||
{
|
||||
namespace Mount
|
||||
{
|
||||
DevRAMFS* fs;
|
||||
|
||||
DevFileSystem* WhichFileSystem(const char* path, size_t* pathoffset)
|
||||
{
|
||||
// TODO: Support some file systems!
|
||||
*pathoffset = 0;
|
||||
return NULL;
|
||||
return fs;
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
|
||||
fs = new DevRAMFS();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace Sortix
|
|||
class DevBuffer : public DevStream
|
||||
{
|
||||
public:
|
||||
typedef Device BaseClass;
|
||||
typedef DevStream BaseClass;
|
||||
|
||||
public:
|
||||
virtual bool IsType(unsigned type)
|
||||
|
|
Loading…
Reference in a new issue