Added an filesystem able to browse the initrd.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-20 16:25:32 +01:00
parent 446616b3aa
commit 190989646b
5 changed files with 230 additions and 14 deletions

View File

@ -42,20 +42,23 @@ __BEGIN_DECLS
@include(SEEK_END.h)
/* TODO: Keep these aligned with those in the Sortix kernel */
#define O_ACCMODE 0003
#define O_RDONLY 00
#define O_WRONLY 01
#define O_RDWR 02
#define O_CREAT (1<<5)
#define O_EXCL 0200
#define O_NOCTTY 0400
#define O_TRUNC 01000
#define O_APPEND 02000
#define O_NONBLOCK 04000
#define O_NDELAY O_NONBLOCK
#define O_SYNC 04010000
#define O_FSYNC O_SYNC
#define O_ASYNC 020000
#define O_RDONLY 1
#define O_WRONLY 2
#define O_RDWR 3
#define O_EXEC 4
#define O_SEARCH 5
#define O_APPEND (1<<3)
#define O_CLOEXEC (1<<4)
#define O_CREAT (1<<5)
#define O_DIRECTORY (1<<6)
#define O_DSYNC (1<<6)
#define O_EXCL (1<<7)
#define O_NOCTTY (1<<8)
#define O_NOFOLLOW (1<<9)
#define O_RSYNC (1<<11)
#define O_SYNC (1<<12)
#define O_TRUNC (1<<13)
#define O_TTY_INIT (1<<13)
@include(mode_t.h)
@include(mode_t_values.h)

View File

@ -52,6 +52,7 @@ namespace Maxsi
const int ENOENT = 17;
const int ENOSPC = 18;
const int EEXIST = 19;
const int EROFS = 20;
extern int _errornumber;

View File

@ -73,6 +73,7 @@ io.o \
pipe.o \
filesystem.o \
mount.o \
fs/initfs.o \
fs/ramfs.o \
../libmaxsi/libmaxsi-sortix.a

163
sortix/fs/initfs.cpp Normal file
View File

@ -0,0 +1,163 @@
/******************************************************************************
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/initfs.h
Provides access to the initial ramdisk.
******************************************************************************/
#include "../platform.h"
#include <libmaxsi/error.h>
#include <libmaxsi/string.h>
#include <libmaxsi/memory.h>
#include "../filesystem.h"
#include "../stream.h"
#include "initfs.h"
#include "../initrd.h"
using namespace Maxsi;
namespace Sortix
{
class DevInitFSFile : public DevBuffer
{
public:
typedef DevBuffer BaseClass;
public:
// Transfers ownership of name.
DevInitFSFile(char* name, const byte* buffer, size_t buffersize);
virtual ~DevInitFSFile();
public:
char* name;
private:
size_t offset;
const 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();
};
DevInitFSFile::DevInitFSFile(char* name, const byte* buffer, size_t buffersize)
{
this->name = name;
this->buffer = buffer;
this->buffersize = buffersize;
}
DevInitFSFile::~DevInitFSFile()
{
delete[] name;
}
size_t DevInitFSFile::BlockSize()
{
return 1;
}
uintmax_t DevInitFSFile::Size()
{
return buffersize;
}
uintmax_t DevInitFSFile::Position()
{
return offset;
}
bool DevInitFSFile::Seek(uintmax_t position)
{
if ( SIZE_MAX < position ) { Error::Set(Error::EOVERFLOW); return false; }
offset = position;
return true;
}
bool DevInitFSFile::Resize(uintmax_t /*size*/)
{
Error::Set(Error::EBADF);
return false;
}
ssize_t DevInitFSFile::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 DevInitFSFile::Write(const byte* /*src*/, size_t /*count*/)
{
Error::Set(Error::EBADF);
return false;
}
bool DevInitFSFile::IsReadable()
{
return true;
}
bool DevInitFSFile::IsWritable()
{
return false;
}
DevInitFS::DevInitFS()
{
}
DevInitFS::~DevInitFS()
{
}
Device* DevInitFS::Open(const char* path, int flags, mode_t mode)
{
if ( *path++ != '/' ) { return NULL; }
if ( (flags & O_LOWERFLAGS) != O_RDONLY ) { Error::Set(Error::EROFS); return NULL; }
size_t buffersize;
const byte* buffer = InitRD::Open(path, &buffersize);
if ( !buffer ) { Error::Set(Error::ENOENT); return NULL; }
char* newpath = String::Clone(path);
if ( !newpath ) { Error::Set(Error::ENOSPC); return NULL; }
Device* result = new DevInitFSFile(newpath, buffer, buffersize);
if ( !result ) { delete[] newpath; Error::Set(Error::ENOSPC); return NULL; }
return result;
}
}

48
sortix/fs/initfs.h Normal file
View File

@ -0,0 +1,48 @@
/******************************************************************************
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/initfs.h
Provides access to the initial ramdisk.
******************************************************************************/
#ifndef SORTIX_FS_INITFS_H
#define SORTIX_FS_INITFS_H
#include <libmaxsi/sortedlist.h>
#include "../filesystem.h"
namespace Sortix
{
class DevInitFSFile;
class DevInitFS : public DevFileSystem
{
public:
DevInitFS();
virtual ~DevInitFS();
public:
virtual Device* Open(const char* path, int flags, mode_t mode);
};
}
#endif