io: require access flags for mapped_file

This commit is contained in:
Danny Robson 2014-07-29 02:20:02 +10:00
parent 4ea033c5a3
commit 503f5e2eff
2 changed files with 50 additions and 15 deletions

52
io.cpp
View File

@ -37,10 +37,28 @@
using namespace std; using namespace std;
using namespace util; using namespace util;
//----------------------------------------------------------------------------
static int
access_to_cflags (access_t a) {
int flags = 0;
if ((a & ACCESS_READWRITE) == ACCESS_READWRITE) {
flags = O_RDWR;
} else if (a & ACCESS_READ) {
flags = O_RDONLY;
} else if (a & ACCESS_WRITE) {
flags = O_WRONLY;
}
a = static_cast<access_t> (a & ~ACCESS_READWRITE);
return flags;
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
std::unique_ptr<char []> std::unique_ptr<char []>
util::slurp (const boost::filesystem::path& path) { util::slurp (const boost::filesystem::path& path) {
fd_ref fd(path, O_RDONLY); fd_ref fd(path, ACCESS_READ);
// Calculate the total file size // Calculate the total file size
off_t size = lseek (fd, 0, SEEK_END); off_t size = lseek (fd, 0, SEEK_END);
@ -79,7 +97,7 @@ util::write (const boost::filesystem::path &path, const char *data, size_t len)
CHECK_SOFT (len > 0); CHECK_SOFT (len > 0);
CHECK_HARD (data); CHECK_HARD (data);
fd_ref fd (path, O_WRONLY); fd_ref fd (path, ACCESS_WRITE);
const char *cursor = data; const char *cursor = data;
size_t remaining = len; size_t remaining = len;
@ -102,11 +120,11 @@ fd_ref::fd_ref (int _fd):
} }
fd_ref::fd_ref (const boost::filesystem::path &path, int flags): fd_ref::fd_ref (const boost::filesystem::path &path, access_t access):
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
fd (open (path.native ().c_str (), flags | O_BINARY)) fd (open (path.native ().c_str (), access_to_cflags (access) | O_BINARY))
#else #else
fd (open (path.native ().c_str (), flags)) fd (open (path.native ().c_str (), access_to_cflags (access)))
#endif #endif
{ {
if (fd < 0) if (fd < 0)
@ -184,9 +202,9 @@ util::set_cwd (const boost::filesystem::path &path) {
#include <sys/mman.h> #include <sys/mman.h>
mapped_file::mapped_file (const boost::filesystem::path &_path): mapped_file::mapped_file (const boost::filesystem::path &_path, access_t _access):
m_fd (_path, O_RDWR) m_fd (_path, _access)
{ load_fd (); } { load_fd (_access); }
mapped_file::~mapped_file () { mapped_file::~mapped_file () {
@ -195,14 +213,28 @@ mapped_file::~mapped_file () {
} }
int
mapped_file::access_to_flags (access_t a) {
int flags = 0;
if (a & ACCESS_READ)
flags |= PROT_READ;
if (a & ACCESS_WRITE)
flags |= PROT_WRITE;
return flags;
}
void void
mapped_file::load_fd (void) { mapped_file::load_fd (access_t access) {
struct stat meta; struct stat meta;
if (fstat (m_fd, &meta) < 0) if (fstat (m_fd, &meta) < 0)
throw errno_error (); throw errno_error ();
m_size = (size_t)meta.st_size; m_size = (size_t)meta.st_size;
m_data = (uint8_t *)mmap (NULL, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0); m_data = (uint8_t *)mmap (NULL, m_size, access_to_flags (access), MAP_SHARED, m_fd, 0);
if (m_data == MAP_FAILED) if (m_data == MAP_FAILED)
throw errno_error (); throw errno_error ();
} }

11
io.hpp
View File

@ -54,7 +54,7 @@ namespace util {
int fd; int fd;
explicit fd_ref (int _fd); explicit fd_ref (int _fd);
explicit fd_ref (const boost::filesystem::path&, int flags); explicit fd_ref (const boost::filesystem::path&, access_t access);
~fd_ref (); ~fd_ref ();
operator int (void) const; operator int (void) const;
@ -108,15 +108,18 @@ namespace util {
#if defined(HAVE_MMAP) #if defined(HAVE_MMAP)
/// Wraps a mechanism to map a file into memory. Read only. /// Wraps a mechanism to map a file into memory. Read only.
class mapped_file { class mapped_file {
protected: private:
fd_ref m_fd; fd_ref m_fd;
uint8_t *m_data; uint8_t *m_data;
size_t m_size; size_t m_size;
void load_fd (void); void load_fd (access_t);
protected:
int access_to_flags (access_t);
public: public:
mapped_file (const boost::filesystem::path &path); mapped_file (const boost::filesystem::path &path, access_t access = ACCESS_READWRITE);
mapped_file (const mapped_file&) = delete; mapped_file (const mapped_file&) = delete;
mapped_file& operator= (const mapped_file&) = delete; mapped_file& operator= (const mapped_file&) = delete;