diff --git a/File/File.cpp b/File/File.cpp index c036633..4442b1b 100644 --- a/File/File.cpp +++ b/File/File.cpp @@ -1,8 +1,14 @@ -#include "File.h" - +#include #include -using namespace File; +#include +#include + + + +using ProFUSE::Exception; +using ProFUSE::POSIXException; + File::File() { @@ -22,9 +28,12 @@ File::File(File& f) File::File(const char *name, int flags) { + #undef __METHOD__ + #define __METHOD__ "File::File" + _fd = ::open(name, flags); if (_fd < 0) - throw ProFUSE::PosixException(errno); + throw POSIXException( __METHOD__ ": open", errno); } File::~File() @@ -32,12 +41,30 @@ File::~File() close(); } -File::close() +void File::close() { - int fd = _fd; - _fd = -1; + #undef __METHOD__ + #define __METHOD__ "File::close" + + if (_fd >= 0) + { + int fd = _fd; + _fd = -1; - if (fd >= 0 && ::close(fd) != 0) - throw ProFUSE::PosixException(errno); + if (::close(fd) != 0) + throw POSIXException(__METHOD__ ": close", errno); + } } + +void File::adopt(File &f) +{ + close(); + _fd = f._fd; + f._fd = -1; +} + +void File::swap(File &f) +{ + std::swap(_fd, f._fd); +} diff --git a/File/File.h b/File/File.h index 4c766d2..56763c5 100644 --- a/File/File.h +++ b/File/File.h @@ -1,17 +1,11 @@ #ifndef __FILE_H__ #define __FILE_H__ +#include #include #include -namespace File { - -enum Flags { - ReadOnly = O_RDONLY, - WriteOnly = O_WRONLY, - ReadWrite = O_RDWR -}; - + class File { public: @@ -24,10 +18,15 @@ class File { int fd() const { return _fd; } void close(); + + void adopt(File &f); + void swap(File &f); private: + + // could call dup() or something. + File& operator=(const File &f); int _fd; }; -} #endif diff --git a/File/MappedFile.cpp b/File/MappedFile.cpp index 8fc4351..a1f79f2 100644 --- a/File/MappedFile.cpp +++ b/File/MappedFile.cpp @@ -1,11 +1,14 @@ -#include "MappedFile.h" - +#include #include + #include -using namespace File; +#include +#include +using ProFUSE::POSIXException; + MappedFile::MappedFile() { _length = -1; @@ -21,20 +24,23 @@ MappedFile::MappedFile(MappedFile &mf) mf._length = -1; } -MappedFile::MappedFile(File f, int flags) +MappedFile::MappedFile(File f, bool readOnly) { + #undef __METHOD__ + #define __METHOD__ "MappedFile::MappedFile" + struct stat st; // close enough if (f.fd() < 0) - throw ProFUSE::PosixException(EBADF); + throw POSIXException( __METHOD__, EBADF); - if (::fstat(f.fd(), st) != 0) - throw ProFUSE::PosixException(errno); + if (::fstat(f.fd(), &st) != 0) + throw POSIXException(__METHOD__ ": fstat", errno); if (!S_ISREG(st.st_mode)) - throw ProFUSE::PosixException(ENODEV); + throw POSIXException(__METHOD__, ENODEV); _length = st.st_size; _address = ::mmap(0, _length, @@ -42,7 +48,7 @@ MappedFile::MappedFile(File f, int flags) MAP_FILE | MAP_SHARED, f.fd(), 0); if (_address == MAP_FAILED) - throw ProFUSE::PosixException(errno); + throw POSIXException(__METHOD__ ": mmap", errno); } MappedFile::~MappedFile() @@ -52,8 +58,11 @@ MappedFile::~MappedFile() -MappedFile::close() +void MappedFile::close() { + #undef __METHOD__ + #define __METHOD__ "MappedFile::close" + if (_address != MAP_FAILED) { void *address = _address; @@ -63,15 +72,34 @@ MappedFile::close() _length = -1; if (::munmap(address, length) != 0) - throw ProFUSE::PosixException(errno); + throw POSIXException(__METHOD__ ": munmap", errno); } } -MappedFile::sync() +void MappedFile::sync() { + #undef __METHOD__ + #define __METHOD__ "MappedFile::sync" + if (_address != MAP_FAILED) { if (::msync(_address, _length, MS_SYNC) != 0) - throw ProFUSE::PosixException(errno); + throw POSIXException(__METHOD__ ": msync", errno); } +} + +void MappedFile::adopt(MappedFile &mf) +{ + close(); + _address = mf._address; + _length = mf._length; + + mf._address = MAP_FAILED; + mf._length = -1; +} + +void MappedFile::swap(MappedFile &mf) +{ + std::swap(_address, mf._address); + std::swap(_length, mf._length); } \ No newline at end of file diff --git a/File/MappedFile.h b/File/MappedFile.h index 90852ec..bc8911c 100644 --- a/File/MappedFile.h +++ b/File/MappedFile.h @@ -3,15 +3,14 @@ #include -#include "File.h" +#include -namespace File { class MappedFile { public: MappedFile(); - MappedFile(File f, int flags); + MappedFile(File f, bool readOnly); MappedFile(MappedFile&); ~MappedFile(); @@ -22,12 +21,16 @@ class MappedFile { void *address() const { return _address; } size_t length() const { return _length; } + void swap(MappedFile &); + void adopt(MappedFile &); + private: + MappedFile& operator=(MappedFile &); + void *_address; size_t _length; }; -} #endif \ No newline at end of file