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 fflags,
int mflags): int mflags):
mapped_file (cruft::win32::handle (reinterpret_cast<HANDLE> (_get_osfhandle (src))), mapped_file (cruft::win32::handle (src.dup ()),
fflags, fflags,
mflags) mflags)
{ }; { };

View File

@ -62,7 +62,7 @@ namespace cruft {
mapped_file (const std::experimental::filesystem::path &path, mapped_file (const std::experimental::filesystem::path &path,
int fflags = O_RDONLY, int fflags = O_RDONLY,
int mflags = PROT_READ); int mflags = PROT_READ);
mapped_file (const cruft::posix::fd&, mapped_file (cruft::posix::fd const&,
int fflag = O_RDONLY, int fflag = O_RDONLY,
int mflags = PROT_READ); 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 () handle::~handle ()
{ {

View File

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