From 494636b8eb85700bc3ee1f800ee5cf5957f3c1e9 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 27 May 2012 14:31:09 +0200 Subject: [PATCH] Added some filesystem utility classes. --- sortix/Makefile | 1 + sortix/fs/util.cpp | 145 +++++++++++++++++++++++++++++++++++++++++++++ sortix/fs/util.h | 78 ++++++++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 sortix/fs/util.cpp create mode 100644 sortix/fs/util.h diff --git a/sortix/Makefile b/sortix/Makefile index 22dfb61e..ebc7f505 100644 --- a/sortix/Makefile +++ b/sortix/Makefile @@ -130,6 +130,7 @@ filesystem.o \ directory.o \ mount.o \ signal.o \ +fs/util.o \ fs/devfs.o \ fs/initfs.o \ fs/ramfs.o \ diff --git a/sortix/fs/util.cpp b/sortix/fs/util.cpp new file mode 100644 index 00000000..96921faa --- /dev/null +++ b/sortix/fs/util.cpp @@ -0,0 +1,145 @@ +/******************************************************************************* + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + + 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/util.cpp + Utility classes for kernel filesystems. + +*******************************************************************************/ + +#include +#include +#include +#include +#include "util.h" + +using namespace Maxsi; + +namespace Sortix { + +DevStringBuffer::DevStringBuffer(char* str) +{ + this->str = str; + strlength = String::Length(str); + off = 0; +} + +DevStringBuffer::~DevStringBuffer() +{ + delete[] str; +} + +size_t DevStringBuffer::BlockSize() +{ + return 1; +} + +uintmax_t DevStringBuffer::Size() +{ + return strlength; +} + +uintmax_t DevStringBuffer::Position() +{ + return off; +} + +bool DevStringBuffer::Seek(uintmax_t position) +{ + if ( strlength <= position ) { Error::Set(EINVAL); return false; } + off = position; + return true; +} + +bool DevStringBuffer::Resize(uintmax_t size) +{ + if ( size != strlength ) { Error::Set(EBADF); } + return false; +} + +ssize_t DevStringBuffer::Read(byte* dest, size_t count) +{ + size_t available = strlength - off; + if ( available < count ) { count = available; } + Memory::Copy(dest, str + off, count); + off += count; + return count; +} + +ssize_t DevStringBuffer::Write(const byte* /*src*/, size_t /*count*/) +{ + Error::Set(EBADF); + return -1; +} + +bool DevStringBuffer::IsReadable() +{ + return true; +} + +bool DevStringBuffer::IsWritable() +{ + return false; +} + + +DevLineCommand::DevLineCommand(bool (*handler)(void*, const char*), void* user) +{ + this->handler = handler; + this->user = user; + this->handled = false; + this->sofar = 0; +} + +DevLineCommand::~DevLineCommand() +{ +} + +ssize_t DevLineCommand::Read(byte* /*dest*/, size_t /*count*/) +{ + Error::Set(EBADF); + return -1; +} + +ssize_t DevLineCommand::Write(const byte* src, size_t count) +{ + if ( handled ) { Error::Set(EINVAL); return -1; } + size_t available = CMDMAX - sofar; + if ( !available && count ) { Error::Set(ENOSPC); return -1; } + if ( available < count ) { count = available; } + Memory::Copy(cmd + sofar, src, count); + cmd[sofar += count] = 0; + size_t newlinepos = String::Reject(cmd, "\n"); + if ( !cmd[newlinepos] ) { return count; } + cmd[newlinepos] = 0; + if ( !handler(user, cmd) ) { return -1; } + return count; +} + +bool DevLineCommand::IsReadable() +{ + return false; +} + +bool DevLineCommand::IsWritable() +{ + return true; +} + +} // namespace Sortix + diff --git a/sortix/fs/util.h b/sortix/fs/util.h new file mode 100644 index 00000000..cede9947 --- /dev/null +++ b/sortix/fs/util.h @@ -0,0 +1,78 @@ +/******************************************************************************* + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + + 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/util.h + Utility classes for kernel filesystems. + +*******************************************************************************/ + +#ifndef SORTIX_FS_UTIL_H +#define SORTIX_FS_UTIL_H + +#include "../stream.h" + +namespace Sortix { + +class DevStringBuffer : public DevBuffer +{ +public: + DevStringBuffer(char* str); + virtual ~DevStringBuffer(); + +private: + char* str; + size_t strlength; + size_t off; + +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(); + +}; + +class DevLineCommand : public DevStream +{ +public: + DevLineCommand(bool (*handler)(void*, const char*), void* user); + virtual ~DevLineCommand(); + 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(); + +private: + bool (*handler)(void*, const char*); + void* user; + size_t sofar; + static const size_t CMDMAX = 255; + char cmd[CMDMAX+1]; + bool handled; + +}; + +} // namespace Sortix + +#endif