2011-11-18 11:49:31 -05: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/>.
|
|
|
|
|
|
|
|
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"
|
2011-11-20 18:02:53 -05:00
|
|
|
#include "../directory.h"
|
2011-11-18 11:49:31 -05:00
|
|
|
#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;
|
2011-11-26 14:56:45 -05:00
|
|
|
size_t bufferused;
|
2011-11-18 11:49:31 -05:00
|
|
|
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;
|
2011-11-26 14:56:45 -05:00
|
|
|
bufferused = 0;
|
2011-11-18 11:49:31 -05:00
|
|
|
buffersize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DevRAMFSFile::~DevRAMFSFile()
|
|
|
|
{
|
|
|
|
delete[] name;
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DevRAMFSFile::BlockSize()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uintmax_t DevRAMFSFile::Size()
|
|
|
|
{
|
2011-11-26 14:56:45 -05:00
|
|
|
return bufferused;
|
2011-11-18 11:49:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uintmax_t DevRAMFSFile::Position()
|
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DevRAMFSFile::Seek(uintmax_t position)
|
|
|
|
{
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( SIZE_MAX < position ) { Error::Set(EOVERFLOW); return false; }
|
2011-11-18 11:49:31 -05:00
|
|
|
offset = position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DevRAMFSFile::Resize(uintmax_t size)
|
|
|
|
{
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( SIZE_MAX < size ) { Error::Set(EOVERFLOW); return false; }
|
2011-11-18 11:49:31 -05:00
|
|
|
byte* newbuffer = new byte[size];
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !newbuffer ) { Error::Set(ENOSPC); return false; }
|
2011-11-26 14:56:45 -05:00
|
|
|
size_t sharedmemsize = ( size < bufferused ) ? size : bufferused;
|
2011-11-18 11:49:31 -05:00
|
|
|
Memory::Copy(newbuffer, buffer, sharedmemsize);
|
|
|
|
delete[] buffer;
|
|
|
|
buffer = newbuffer;
|
2011-11-26 14:56:45 -05:00
|
|
|
bufferused = sharedmemsize;
|
2011-11-18 11:49:31 -05:00
|
|
|
buffersize = size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t DevRAMFSFile::Read(byte* dest, size_t count)
|
|
|
|
{
|
|
|
|
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; }
|
|
|
|
size_t available = count;
|
2011-11-26 14:56:45 -05:00
|
|
|
if ( bufferused < offset + count ) { available = bufferused - offset; }
|
2011-11-18 11:49:31 -05:00
|
|
|
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;
|
2011-11-26 14:25:25 -05:00
|
|
|
if ( newsize < buffersize * 2 ) { newsize = buffersize * 2; }
|
2011-11-18 11:49:31 -05:00
|
|
|
if ( !Resize(newsize) ) { return -1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
Memory::Copy(buffer + offset, src, count);
|
|
|
|
offset += count;
|
2011-11-26 14:56:45 -05:00
|
|
|
if ( bufferused < offset ) { bufferused = offset; }
|
2011-11-18 11:49:31 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-20 18:02:53 -05:00
|
|
|
class DevRAMFSDir : public DevDirectory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Device DevDirectory;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DevRAMFSDir(DevRAMFS* fs);
|
|
|
|
virtual ~DevRAMFSDir();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DevRAMFS* fs;
|
|
|
|
size_t position;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void Rewind();
|
|
|
|
virtual int Read(sortix_dirent* dirent, size_t available);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
DevRAMFSDir::DevRAMFSDir(DevRAMFS* fs)
|
|
|
|
{
|
|
|
|
position = 0;
|
|
|
|
this->fs = fs;
|
|
|
|
fs->Refer();
|
|
|
|
}
|
|
|
|
|
|
|
|
DevRAMFSDir::~DevRAMFSDir()
|
|
|
|
{
|
|
|
|
fs->Unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevRAMFSDir::Rewind()
|
|
|
|
{
|
|
|
|
position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DevRAMFSDir::Read(sortix_dirent* dirent, size_t available)
|
|
|
|
{
|
|
|
|
if ( available <= sizeof(sortix_dirent) ) { return -1; }
|
|
|
|
if ( fs->GetNumFiles() <= position )
|
|
|
|
{
|
|
|
|
dirent->d_namelen = 0;
|
|
|
|
dirent->d_name[0] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name = fs->GetFilename(position);
|
|
|
|
if ( !name ) { return -1; }
|
|
|
|
size_t namelen = String::Length(name);
|
|
|
|
size_t needed = sizeof(sortix_dirent) + namelen + 1;
|
|
|
|
|
|
|
|
if ( available < needed )
|
|
|
|
{
|
|
|
|
dirent->d_namelen = needed;
|
2011-11-22 11:26:47 -05:00
|
|
|
Error::Set(EINVAL);
|
2011-11-20 18:02:53 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Memory::Copy(dirent->d_name, name, namelen + 1);
|
|
|
|
dirent->d_namelen = namelen;
|
|
|
|
position++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-18 11:49:31 -05:00
|
|
|
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)
|
|
|
|
{
|
2011-11-22 09:33:47 -05:00
|
|
|
if ( path[0] == 0 || (path[0] == '/' && path[1] == 0) )
|
2011-11-20 18:02:53 -05:00
|
|
|
{
|
2011-11-22 09:33:47 -05:00
|
|
|
if ( (flags & O_LOWERFLAGS) == O_SEARCH )
|
|
|
|
{
|
|
|
|
return new DevRAMFSDir(this);
|
|
|
|
}
|
|
|
|
|
2011-11-22 11:26:47 -05:00
|
|
|
Error::Set(EISDIR);
|
2011-11-20 18:02:53 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-23 08:14:59 -05:00
|
|
|
if ( (flags & O_LOWERFLAGS) == O_SEARCH ) { Error::Set(ENOTDIR); return NULL; }
|
|
|
|
|
2011-11-24 09:38:45 -05:00
|
|
|
if ( *path++ != '/' ) { Error::Set(ENOENT); return NULL; }
|
2011-11-23 11:51:18 -05:00
|
|
|
|
|
|
|
size_t pathlen = String::Length(path);
|
|
|
|
for ( size_t i = 0; i < pathlen; i++ )
|
|
|
|
{
|
|
|
|
if ( path[i] == '/' ) { Error::Set(ENOENT); return NULL; }
|
|
|
|
}
|
|
|
|
|
2011-11-18 11:49:31 -05:00
|
|
|
DevBuffer* file = OpenFile(path, flags, mode);
|
|
|
|
if ( !file ) { return NULL; }
|
|
|
|
Device* wrapper = new DevFileWrapper(file, flags);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !wrapper ) { Error::Set(ENOSPC); return NULL; }
|
2011-11-18 11:49:31 -05:00
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
DevBuffer* DevRAMFS::OpenFile(const char* path, int flags, mode_t mode)
|
|
|
|
{
|
2011-11-20 16:47:39 -05:00
|
|
|
// Hack to prevent / from being a filename.
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( path == 0 ) { Error::Set(ENOENT); return NULL; }
|
2011-11-18 11:49:31 -05:00
|
|
|
|
|
|
|
if ( files )
|
|
|
|
{
|
|
|
|
size_t fileindex = files->Search(LookupFile, path);
|
2011-11-20 18:12:20 -05:00
|
|
|
if ( fileindex != SIZE_MAX )
|
|
|
|
{
|
|
|
|
DevRAMFSFile* file = files->Get(fileindex);
|
|
|
|
if ( flags & O_TRUNC ) { file->Resize(0); }
|
|
|
|
return file;
|
|
|
|
}
|
2011-11-18 11:49:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return CreateFile(path, flags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
DevBuffer* DevRAMFS::CreateFile(const char* path, int flags, mode_t mode)
|
|
|
|
{
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !(flags & O_CREAT) ) { Error::Set(ENOENT); return NULL; }
|
2011-11-18 11:49:31 -05:00
|
|
|
|
|
|
|
if ( !files )
|
|
|
|
{
|
|
|
|
files = new SortedList<DevRAMFSFile*>(CompareFiles);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !files) { Error::Set(ENOSPC); return NULL; }
|
2011-11-18 11:49:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( files->Search(LookupFile, path) != SIZE_MAX )
|
|
|
|
{
|
2011-11-22 11:26:47 -05:00
|
|
|
Error::Set(EEXIST);
|
2011-11-18 11:49:31 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* newpath = String::Clone(path);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !newpath ) { Error::Set(ENOSPC); return NULL; }
|
2011-11-18 11:49:31 -05:00
|
|
|
|
|
|
|
DevRAMFSFile* file = new DevRAMFSFile(newpath);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !file ) { delete[] newpath; Error::Set(ENOSPC); return NULL; }
|
|
|
|
if ( !files->Add(file) ) { delete file; Error::Set(ENOSPC); return NULL; }
|
2011-11-18 11:49:31 -05:00
|
|
|
|
|
|
|
file->Refer();
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
2011-11-20 18:02:53 -05:00
|
|
|
|
2011-11-21 12:49:55 -05:00
|
|
|
bool DevRAMFS::Unlink(const char* path)
|
|
|
|
{
|
|
|
|
if ( *path == '\0' || ( *path++ == '/' && *path == '\0' ) )
|
|
|
|
{
|
2011-11-22 11:26:47 -05:00
|
|
|
Error::Set(EISDIR);
|
2011-11-21 12:49:55 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-23 11:51:18 -05:00
|
|
|
if ( !files ) { Error::Set(ENOENT); return false; }
|
2011-11-21 12:49:55 -05:00
|
|
|
size_t index = files->Search(LookupFile, path);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( index == SIZE_MAX ) { Error::Set(ENOENT); return false; }
|
2011-11-21 12:49:55 -05:00
|
|
|
|
|
|
|
Device* dev = files->Remove(index);
|
|
|
|
ASSERT(dev);
|
|
|
|
dev->Unref();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-20 18:02:53 -05:00
|
|
|
size_t DevRAMFS::GetNumFiles()
|
|
|
|
{
|
|
|
|
if ( !files ) { return 0; }
|
|
|
|
return files->Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* DevRAMFS::GetFilename(size_t index)
|
|
|
|
{
|
|
|
|
if ( !files ) { return NULL; }
|
|
|
|
if ( files->Length() <= index ) { return NULL; }
|
|
|
|
DevRAMFSFile* file = files->Get(index);
|
|
|
|
return file->name;
|
|
|
|
}
|
2011-11-18 11:49:31 -05:00
|
|
|
}
|
|
|
|
|