win32/handle: take ownership of any supplied fd

CloseHandle, close, and memory mapping interact poorly so it's a little safer
to dup the fd before taking ownership of it.
This commit is contained in:
Danny Robson 2018-08-22 21:34:02 +10:00
parent 1b5dfe437a
commit 058a86ba05
4 changed files with 14 additions and 3 deletions

View File

@ -135,10 +135,10 @@ mapped_file::mapped_file (
//-----------------------------------------------------------------------------
mapped_file::mapped_file (const cruft::posix::fd &src,
mapped_file::mapped_file (cruft::posix::fd const &src,
int fflags,
int mflags):
mapped_file (cruft::win32::handle (reinterpret_cast<HANDLE> (_get_osfhandle (src))),
mapped_file (cruft::win32::handle (src.dup ()),
fflags,
mflags)
{ };

View File

@ -62,7 +62,7 @@ namespace cruft {
mapped_file (const std::experimental::filesystem::path &path,
int fflags = O_RDONLY,
int mflags = PROT_READ);
mapped_file (const cruft::posix::fd&,
mapped_file (cruft::posix::fd const&,
int fflag = O_RDONLY,
int mflags = PROT_READ);

View File

@ -25,6 +25,14 @@ handle::handle (handle &&rhs):
}
//-----------------------------------------------------------------------------
handle::handle (posix::fd &&rhs):
m_native (reinterpret_cast<HANDLE> (_get_osfhandle (rhs.release ())))
{
win32::error::try_value (m_native);
}
//-----------------------------------------------------------------------------
handle::~handle ()
{

View File

@ -8,12 +8,15 @@
#include <windows.h>
#include "../posix/fd.hpp"
namespace cruft::win32 {
struct handle {
handle ();
explicit handle (HANDLE&&);
handle (handle&&);
handle (const handle&) = delete;
handle (posix::fd&&);
~handle ();
operator HANDLE& (void) &;