From ea0d1c198be660445719f023920d227468a138f8 Mon Sep 17 00:00:00 2001 From: ksherlock Date: Sat, 19 Feb 2011 19:37:01 +0000 Subject: [PATCH] new branch to integrate BlockDevice, BlockCache git-svn-id: https://profuse.googlecode.com/svn/branches/profuse_interim@335 aa027e90-d47c-11dd-86d7-074df07e0730 --- ProFUSE/Exception.cpp | 148 +++++++++++++++++++++++++++++++++++++++ ProFUSE/Exception.h | 158 ++++++++++++++++++++++++++++++++++++++++++ ProFUSE/Lock.cpp | 28 ++++++++ ProFUSE/Lock.h | 33 +++++++++ ProFUSE/Makefile | 9 +++ ProFUSE/auto.h | 104 +++++++++++++++++++++++++++ 6 files changed, 480 insertions(+) create mode 100644 ProFUSE/Exception.cpp create mode 100644 ProFUSE/Exception.h create mode 100644 ProFUSE/Lock.cpp create mode 100644 ProFUSE/Lock.h create mode 100644 ProFUSE/Makefile create mode 100644 ProFUSE/auto.h diff --git a/ProFUSE/Exception.cpp b/ProFUSE/Exception.cpp new file mode 100644 index 0000000..a42b877 --- /dev/null +++ b/ProFUSE/Exception.cpp @@ -0,0 +1,148 @@ + +#include +#include + +using namespace ProFUSE; + +Exception::~Exception() throw() +{ +} + +const char *Exception::what() +{ + return _string.c_str(); +} + +const char *Exception::errorString() +{ + return ""; +} + + +const char *POSIXException::errorString() +{ + return strerror(error()); +} + +const char *ProDOSException::errorString() +{ + + + switch (error()) + { + case badSystemCall: + return "Bad System Call"; + case invalidPcount: + return "Invalid Parameter Count"; + case gsosActive: + return "GS/OS Active"; + case devNotFound: + return "Device Not Found"; + case invalidDevNum: + return "Invalid Device Number"; + case drvrBadReq: + return "Driver Bad Request"; + case drvrBadCode: + return "Driver Bad Code"; + case drvrBadParm: + return "Driver Bad Parameter"; + case drvrNotOpen: + return "Driver Not Open"; + case drvrPriorOpen: + return "Driver Prior Open"; + case irqTableFull: + return "IRQ Table Full"; + case drvrNoResrc: + return "Driver No Resource"; + case drvrIOError: + return "Driver IO Error"; + case drvrNoDevice: + return "Driver No Device"; + case drvrBusy: + return "Driver Busy"; + case drvrWrtProt: + return "Driver Write Protected"; + case drvrBadCount: + return "Driver Bad Count"; + case drvrBadBlock: + return "Driver Bad Block"; + case drvrDiskSwitch: + return "Driver Disk Switch"; + case drvrOffLine: + return "Driver Off Line"; + case badPathSyntax: + return "Bad Path Syntax"; + case invalidRefNum: + return "Invalid Ref Num"; + case pathNotFound: + return "Path Not Found"; + case volNotFound: + return "Volume Not Found"; + case fileNotFound: + return "File Not Found"; + case dupPathName: + return "Duplicate Path Name"; + case volumeFull: + return "Volume Full"; + case volDirFull: + return "Volume Directory Full"; + case badFileFormat: + return "Bad File Format"; + case badStoreType: + return "Bad Storage Type"; + case eofEncountered: + return "End of File"; + case outOfRange: + return "Out of Range"; + case invalidAccess: + return "Invalid Access"; + case buffTooSmall: + return "Buffer Too Small"; + case fileBusy: + return "File Busy"; + case dirError: + return "Directory Error"; + case unknownVol: + return "Unknown Volume"; + case paramRangeError: + return "Parameter Range Error"; + case outOfMem: + return "Out of Memory"; + case dupVolume: + return "Duplicate Volume"; + case notBlockDev: + return "Not a Block Device"; + case invalidLevel: + return "Invalid Level"; + case damagedBitMap: + return "Damaged Bit Map"; + case badPathNames: + return "Bad Path Names"; + case notSystemFile: + return "Not a System File"; + case osUnsupported: + return "OS Unsupported"; + case stackOverflow: + return "Stack Overflow"; + case dataUnavail: + return "Data Unavailable"; + case endOfDir: + return "End Of Directory"; + case invalidClass: + return "Invalid Class"; + case resForkNotFound: + return "Resource Fork Not Found"; + case invalidFSTID: + return "Invalid FST ID"; + case devNameErr: + return "Device Name Error"; + case resExistsErr: + return "Resource Exists Error"; + case resAddErr: + return "Resource Add Error"; + + default: + return ""; + } + return ""; +} \ No newline at end of file diff --git a/ProFUSE/Exception.h b/ProFUSE/Exception.h new file mode 100644 index 0000000..caa9964 --- /dev/null +++ b/ProFUSE/Exception.h @@ -0,0 +1,158 @@ +#ifndef __EXCEPTION_H__ +#define __EXCEPTION_H__ + +#include +#include + +namespace ProFUSE { + + +// ProDOS Errors +enum +{ + badSystemCall = 0x01, + invalidPcount = 0x04, + gsosActive = 0x07, + devNotFound = 0x10, + invalidDevNum = 0x11, + drvrBadReq = 0x20, + drvrBadCode = 0x21, + drvrBadParm = 0x22, + drvrNotOpen = 0x23, + drvrPriorOpen = 0x24, + irqTableFull = 0x25, + drvrNoResrc = 0x26, + drvrIOError = 0x27, + drvrNoDevice = 0x28, + drvrBusy = 0x29, + drvrWrtProt = 0x2b, + drvrBadCount = 0x2c, + drvrBadBlock = 0x2d, + drvrDiskSwitch = 0x2e, + drvrOffLine = 0x2f, + badPathSyntax = 0x40, + invalidRefNum = 0x43, + pathNotFound = 0x44, + volNotFound = 0x45, + fileNotFound = 0x46, + dupPathName = 0x47, + volumeFull = 0x48, + volDirFull = 0x49, + badFileFormat = 0x4a, + badStoreType = 0x4b, + eofEncountered = 0x4c, + outOfRange = 0x4d, + invalidAccess = 0x4e, + buffTooSmall = 0x4f, + fileBusy = 0x50, + dirError = 0x51, + unknownVol = 0x52, + paramRangeError = 0x53, + outOfMem = 0x54, + dupVolume = 0x57, + notBlockDev = 0x58, + invalidLevel = 0x59, + damagedBitMap = 0x5a, + badPathNames = 0x5b, + notSystemFile = 0x5c, + osUnsupported = 0x5d, + stackOverflow = 0x5f, + dataUnavail = 0x60, + endOfDir = 0x61, + invalidClass = 0x62, + resForkNotFound = 0x63, + invalidFSTID = 0x64, + devNameErr = 0x67, + resExistsErr = 0x70, + resAddErr = 0x71 +}; + +class Exception : public std::exception +{ +public: + Exception(const char *cp); + Exception(const std::string &str); + + + virtual ~Exception() throw (); + + virtual const char *what(); + virtual const char *errorString(); + + int error() const { return _error; } + +protected: + Exception(const char *cp, int error); + Exception(const std::string& string, int error); + +private: + int _error; + std::string _string; + +}; + +class POSIXException : public Exception { +public: + POSIXException(const char *cp, int error); + POSIXException(const std::string& string, int error); + + virtual const char *errorString(); +}; + +class ProDOSException : public Exception { +public: + ProDOSException(const char *cp, int error); + ProDOSException(const std::string& string, int error); + + virtual const char *errorString(); +}; + +inline Exception::Exception(const char *cp): + _error(0), + _string(cp) +{ +} + +inline Exception::Exception(const std::string& string): + _error(0), + _string(string) +{ +} + +inline Exception::Exception(const char *cp, int error): + _error(error), + _string(cp) +{ +} + +inline Exception::Exception(const std::string& string, int error): + _error(error), + _string(string) +{ +} + +inline POSIXException::POSIXException(const char *cp, int error) : + Exception(cp, error) +{ +} + +inline POSIXException::POSIXException(const std::string& string, int error) : + Exception(string, error) +{ +} + +inline ProDOSException::ProDOSException(const char *cp, int error) : + Exception(cp, error) +{ +} + +inline ProDOSException::ProDOSException(const std::string& string, int error) : + Exception(string, error) +{ +} + + + +} + +#endif \ No newline at end of file diff --git a/ProFUSE/Lock.cpp b/ProFUSE/Lock.cpp new file mode 100644 index 0000000..7f6130c --- /dev/null +++ b/ProFUSE/Lock.cpp @@ -0,0 +1,28 @@ +#include + +using namespace ProFUSE; + +Lock::Lock() +{ + pthread_mutex_init(&_mutex, NULL); +} + +Lock::~Lock() +{ + pthread_mutex_destroy(&_mutex); +} + +void Lock::lock() +{ + pthread_mutex_lock(&_mutex); +} + +void Lock::unlock() +{ + pthread_mutex_unlock(&_mutex); +} + +bool Lock::tryLock() +{ + return pthread_mutex_trylock(&_mutex) == 0; +} diff --git a/ProFUSE/Lock.h b/ProFUSE/Lock.h new file mode 100644 index 0000000..8e32a80 --- /dev/null +++ b/ProFUSE/Lock.h @@ -0,0 +1,33 @@ +#ifndef __LOCK_H__ +#define __LOCK_H__ + +#include + +namespace ProFUSE { + +class Lock { +public: + Lock(); + ~Lock(); + + void lock(); + void unlock(); + + bool tryLock(); + +private: + pthread_mutex_t _mutex; +}; + +class Locker { +public: + Locker(Lock& lock) : _lock(lock) { _lock.lock(); } + ~Locker() { _lock.unlock(); } +private: + Lock &_lock; +}; + + +} + +#endif diff --git a/ProFUSE/Makefile b/ProFUSE/Makefile new file mode 100644 index 0000000..657e89d --- /dev/null +++ b/ProFUSE/Makefile @@ -0,0 +1,9 @@ +CC = g++ +CPPFLAGS += -g -Wall -I../ + + +all : Exception.o Lock.o + +Exception.o : Exception.cpp Exception.h + +Lock.o : Lock.cpp Lock.h \ No newline at end of file diff --git a/ProFUSE/auto.h b/ProFUSE/auto.h new file mode 100644 index 0000000..e7c6876 --- /dev/null +++ b/ProFUSE/auto.h @@ -0,0 +1,104 @@ +#ifndef __AUTO_H__ +#define __AUTO_H__ + +#include + +namespace ProFUSE { + +template +class auto_array +{ +public: + auto_array() : _t(NULL) {} + auto_array(T *t) : _t(t) {} + ~auto_array() { if (_t) delete []_t; } + + + T* release() + { T *tmp = _t; _t = NULL; return tmp; } + + T* get() const { return _t; } + operator T*() const { return _t; } + T& operator[](int index) { return _t[index]; } + + void reset(T *t) + { + if (t == _t) return; + if (_t) delete[] _t; + _t = t; + } + +private: + T *_t; +}; + +// ::close +#if defined(O_CREAT) +class auto_fd +{ +public: + auto_fd(int fd = -1) : _fd(fd) { } + + ~auto_fd() { close(); } + + int release() + { int tmp = _fd; _fd = -1; return tmp; } + + int get() const { return _fd; } + operator int() const { return _fd; } + + void reset(int fd) + { + if (fd != _fd) + { + close(); + _fd = fd; + } + } + +private: + auto_fd& operator=(const auto_fd&); + + void close() + { + if (_fd >= 0) + { + ::close(_fd); + _fd = -1; + } + } + + int _fd; +}; +#endif + +// ::mmap, :munmap +#if defined(MAP_FAILED) +class auto_map +{ +public: + auto_map(void *addr, size_t size, int prot, int flags, int fd, off_t offset) + : + _size(size), + _map(::mmap(addr, size, prot, flags, fd, offset)) + { } + + ~auto_map() + { if (_map != MAP_FAILED) ::munmap(_map, _size); } + + void *release() + { void *tmp = _map; _map = MAP_FAILED; return tmp; } + + void *get() const { return _map; } + operator void *() const { return _map; } + +private: + size_t _size; + void *_map; +}; +#endif + +} + +#endif +