From 80158b734343e50c59d3da22ba3a77a96da3203d Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 5 Apr 2016 11:07:23 +1000 Subject: [PATCH] io: remove our wrapper flag types --- io.cpp | 2 +- io.hpp | 2 ++ io_win32.cpp | 48 +++++++++++++++++++++++++----------------------- io_win32.hpp | 13 ++++++++++++- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/io.cpp b/io.cpp index 0a20de95..c239db5e 100644 --- a/io.cpp +++ b/io.cpp @@ -216,5 +216,5 @@ path_error::path_error (const boost::filesystem::path &_path): const char* path_error::path (void) const noexcept { - return m_path.c_str (); + return m_path.string ().c_str (); } diff --git a/io.hpp b/io.hpp index 06005bad..4241e6fa 100644 --- a/io.hpp +++ b/io.hpp @@ -21,6 +21,8 @@ #include "platform.hpp" #include "nocopy.hpp" +#include + #include #include #include diff --git a/io_win32.cpp b/io_win32.cpp index da825e45..7fa29d96 100644 --- a/io_win32.cpp +++ b/io_win32.cpp @@ -14,10 +14,10 @@ * Copyright 2014 Danny Robson */ -#include "io_win32.hpp" +#include "./io_win32.hpp" -#include "debug.hpp" -#include "except.hpp" +#include "./debug.hpp" +#include "./except.hpp" #include @@ -26,38 +26,40 @@ using util::detail::win32::mapped_file; //----------------------------------------------------------------------------- static DWORD -access_to_flags (util::access_t a) { - switch (a) { - case util::ACCESS_READ: - return GENERIC_READ; - - case util::ACCESS_WRITE: - return GENERIC_WRITE; - - default: - unreachable (); +fflags_to_native (int flags) { + switch (flags) { + case O_RDONLY: return GENERIC_READ; + case O_WRONLY: return GENERIC_WRITE; + case O_RDWR: return GENERIC_READ | GENERIC_WRITE; } + + unreachable (); } //----------------------------------------------------------------------------- mapped_file::mapped_file (const boost::filesystem::path &path, - access_t access): + int fflags, + int mflags): m_data (nullptr, UnmapViewOfFile) { - m_file.reset (CreateFile (path.string ().c_str (), - access_to_flags (access), - access == ACCESS_READ ? FILE_SHARE_READ : 0, - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, - 0)); + m_file.reset ( + CreateFile ( + path.string ().c_str (), + fflags_to_native (fflags), + fflags & O_RDONLY ? FILE_SHARE_READ : 0, + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, + 0 + ) + ); if (m_file == INVALID_HANDLE_VALUE) win32_error::throw_code (); m_mapping.reset (CreateFileMapping (m_file, nullptr, - access == ACCESS_READ ? PAGE_READONLY : PAGE_READWRITE, + access == O_RDONLY ? PAGE_READONLY : PAGE_READWRITE, 0, 0, nullptr)); @@ -65,7 +67,7 @@ mapped_file::mapped_file (const boost::filesystem::path &path, win32_error::throw_code (); auto view = MapViewOfFile (m_mapping, - access == ACCESS_READ ? FILE_MAP_READ : FILE_MAP_WRITE, + access == O_RDONLY ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0); if (view == nullptr) diff --git a/io_win32.hpp b/io_win32.hpp index cf9dc844..180fffe5 100644 --- a/io_win32.hpp +++ b/io_win32.hpp @@ -22,13 +22,24 @@ #include #include #include +#include +#include +#include namespace util { namespace detail { namespace win32 { + enum { + PROT_NONE = 0, + PROT_READ = 1 << 0, + PROT_EXEC = 1 << 1, + PROT_WRITE = 1 << 2 + }; + class mapped_file { public: mapped_file (const boost::filesystem::path &path, - access_t access = ACCESS_READ); + int fflags = O_RDONLY | O_BINARY, + int mflags = PROT_READ); mapped_file (const mapped_file&) = delete; mapped_file& operator= (const mapped_file&) = delete;