From 190989646bf43abd9d2b8329d7478117ba3b66c8 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 20 Nov 2011 16:25:32 +0100 Subject: [PATCH] Added an filesystem able to browse the initrd. --- libmaxsi/c/hsrc/fcntl.h | 31 ++++---- libmaxsi/hsrc/error.h | 1 + sortix/Makefile | 1 + sortix/fs/initfs.cpp | 163 ++++++++++++++++++++++++++++++++++++++++ sortix/fs/initfs.h | 48 ++++++++++++ 5 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 sortix/fs/initfs.cpp create mode 100644 sortix/fs/initfs.h diff --git a/libmaxsi/c/hsrc/fcntl.h b/libmaxsi/c/hsrc/fcntl.h index 04d032f4..d100fd28 100644 --- a/libmaxsi/c/hsrc/fcntl.h +++ b/libmaxsi/c/hsrc/fcntl.h @@ -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) diff --git a/libmaxsi/hsrc/error.h b/libmaxsi/hsrc/error.h index ae081d2d..211c66b6 100644 --- a/libmaxsi/hsrc/error.h +++ b/libmaxsi/hsrc/error.h @@ -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; diff --git a/sortix/Makefile b/sortix/Makefile index 62159637..b9a2547d 100644 --- a/sortix/Makefile +++ b/sortix/Makefile @@ -73,6 +73,7 @@ io.o \ pipe.o \ filesystem.o \ mount.o \ +fs/initfs.o \ fs/ramfs.o \ ../libmaxsi/libmaxsi-sortix.a diff --git a/sortix/fs/initfs.cpp b/sortix/fs/initfs.cpp new file mode 100644 index 00000000..4ae1b4df --- /dev/null +++ b/sortix/fs/initfs.cpp @@ -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 . + + fs/initfs.h + Provides access to the initial ramdisk. + +******************************************************************************/ + +#include "../platform.h" +#include +#include +#include +#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; + } +} + diff --git a/sortix/fs/initfs.h b/sortix/fs/initfs.h new file mode 100644 index 00000000..cc4d701c --- /dev/null +++ b/sortix/fs/initfs.h @@ -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 . + + fs/initfs.h + Provides access to the initial ramdisk. + +******************************************************************************/ + +#ifndef SORTIX_FS_INITFS_H +#define SORTIX_FS_INITFS_H + +#include +#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 +