From e53674e01533524b573a9b955ae7945c052be7ff Mon Sep 17 00:00:00 2001 From: ksherlock Date: Wed, 19 May 2010 16:06:42 +0000 Subject: [PATCH] git-svn-id: https://profuse.googlecode.com/svn/branches/v2@224 aa027e90-d47c-11dd-86d7-074df07e0730 --- File/File.cpp | 12 ++++++++++++ File/File.h | 1 + File/MappedFile.cpp | 10 +++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/File/File.cpp b/File/File.cpp index 4442b1b..7ce27f0 100644 --- a/File/File.cpp +++ b/File/File.cpp @@ -36,6 +36,18 @@ File::File(const char *name, int flags) throw POSIXException( __METHOD__ ": open", errno); } + +File::File(const char *name, bool readOnly) +{ +#undef __METHOD__ +#define __METHOD__ "File::File" + + _fd = ::open(name, readOnly ? O_RDONLY : O_RDWR); + if (_fd < 0) + throw POSIXException( __METHOD__ ": open", errno); +} + + File::~File() { close(); diff --git a/File/File.h b/File/File.h index 58609c5..6278666 100644 --- a/File/File.h +++ b/File/File.h @@ -13,6 +13,7 @@ class File { File(File &); File(int fd); File(const char *name, int flags); + File(const char *name, bool readOnly); ~File(); bool isValid() const diff --git a/File/MappedFile.cpp b/File/MappedFile.cpp index babaac5..0ffd087 100644 --- a/File/MappedFile.cpp +++ b/File/MappedFile.cpp @@ -45,7 +45,7 @@ MappedFile::MappedFile(const char *name, bool readOnly) _address = MAP_FAILED; _readOnly = readOnly; - init(f, readOnly, -1); + init(f, readOnly, 0); } @@ -64,13 +64,15 @@ void MappedFile::init(const File &f, bool readOnly, size_t size) #define __METHOD__ "MappedFile::init" struct stat st; + int prot = readOnly ? PROT_READ : PROT_READ | PROT_WRITE; + int flags = MAP_FILE | MAP_SHARED; // close enough if (f.fd() < 0) throw POSIXException( __METHOD__, EBADF); - if (size <= 0) + if (!size) { if (::fstat(f.fd(), &st) != 0) throw POSIXException(__METHOD__ ": fstat", errno); @@ -82,9 +84,7 @@ void MappedFile::init(const File &f, bool readOnly, size_t size) } _length = size; - _address = ::mmap(0, _length, - readOnly ? PROT_READ : PROT_READ | PROT_WRITE, - MAP_FILE | MAP_SHARED, f.fd(), 0); + _address = ::mmap(0, _length, prot, flags, f.fd(), 0); if (_address == MAP_FAILED) throw POSIXException(__METHOD__ ": mmap", errno);