2019-12-21 13:16:18 +02:00
|
|
|
#![unstable(issue = "none", feature = "windows_handle")]
|
2016-02-12 10:29:25 -08:00
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::cmp;
|
2019-11-27 10:28:39 -08:00
|
|
|
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, Read};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::mem;
|
|
|
|
|
use crate::ops::Deref;
|
|
|
|
|
use crate::ptr;
|
|
|
|
|
use crate::sys::c;
|
|
|
|
|
use crate::sys::cvt;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
2015-05-12 11:03:49 -07:00
|
|
|
/// An owned container for `HANDLE` object, closing them on Drop.
|
|
|
|
|
///
|
|
|
|
|
/// All methods are inherited through a `Deref` impl to `RawHandle`
|
|
|
|
|
pub struct Handle(RawHandle);
|
2015-01-27 12:20:58 -08:00
|
|
|
|
2015-05-12 11:03:49 -07:00
|
|
|
/// A wrapper type for `HANDLE` objects to give them proper Send/Sync inference
|
|
|
|
|
/// as well as Rust-y methods.
|
|
|
|
|
///
|
|
|
|
|
/// This does **not** drop the handle when it goes out of scope, use `Handle`
|
|
|
|
|
/// instead for that.
|
|
|
|
|
#[derive(Copy, Clone)]
|
2015-11-02 16:23:22 -08:00
|
|
|
pub struct RawHandle(c::HANDLE);
|
2015-05-12 11:03:49 -07:00
|
|
|
|
|
|
|
|
unsafe impl Send for RawHandle {}
|
|
|
|
|
unsafe impl Sync for RawHandle {}
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
|
impl Handle {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn new(handle: c::HANDLE) -> Handle {
|
2015-05-12 11:03:49 -07:00
|
|
|
Handle(RawHandle::new(handle))
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
2015-02-06 09:42:57 -08:00
|
|
|
|
2016-02-12 10:29:25 -08:00
|
|
|
pub fn new_event(manual: bool, init: bool) -> io::Result<Handle> {
|
|
|
|
|
unsafe {
|
2019-11-27 10:28:39 -08:00
|
|
|
let event =
|
|
|
|
|
c::CreateEventW(ptr::null_mut(), manual as c::BOOL, init as c::BOOL, ptr::null());
|
|
|
|
|
if event.is_null() { Err(io::Error::last_os_error()) } else { Ok(Handle::new(event)) }
|
2016-02-12 10:29:25 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn into_raw(self) -> c::HANDLE {
|
2015-05-12 11:03:49 -07:00
|
|
|
let ret = self.raw();
|
2015-05-07 10:49:39 -07:00
|
|
|
mem::forget(self);
|
2019-09-05 13:30:30 +02:00
|
|
|
ret
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Deref for Handle {
|
|
|
|
|
type Target = RawHandle;
|
2019-11-27 10:28:39 -08:00
|
|
|
fn deref(&self) -> &RawHandle {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Handle {
|
|
|
|
|
fn drop(&mut self) {
|
2019-11-27 10:28:39 -08:00
|
|
|
unsafe {
|
|
|
|
|
let _ = c::CloseHandle(self.raw());
|
|
|
|
|
}
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RawHandle {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn new(handle: c::HANDLE) -> RawHandle {
|
2015-05-12 11:03:49 -07:00
|
|
|
RawHandle(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
pub fn raw(&self) -> c::HANDLE {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2015-02-06 09:42:57 -08:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-04-14 11:17:47 -07:00
|
|
|
let mut read = 0;
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
|
2015-04-14 11:17:47 -07:00
|
|
|
let res = cvt(unsafe {
|
2019-11-27 10:28:39 -08:00
|
|
|
c::ReadFile(self.0, buf.as_mut_ptr() as c::LPVOID, len, &mut read, ptr::null_mut())
|
2015-04-14 11:17:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
match res {
|
|
|
|
|
Ok(_) => Ok(read as usize),
|
|
|
|
|
|
|
|
|
|
// The special treatment of BrokenPipe is to deal with Windows
|
|
|
|
|
// pipe semantics, which yields this error when *reading* from
|
|
|
|
|
// a pipe after the other end has closed; we interpret that as
|
|
|
|
|
// EOF on the pipe.
|
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::BrokenPipe => Ok(0),
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
Err(e) => Err(e),
|
2015-04-14 11:17:47 -07:00
|
|
|
}
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
|
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 12:51:25 -07:00
|
|
|
crate::io::default_read_vectored(|buf| self.read(buf), bufs)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 01:11:33 +02:00
|
|
|
pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
|
|
|
|
|
let mut read = 0;
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
|
2016-08-16 01:11:33 +02:00
|
|
|
let res = unsafe {
|
|
|
|
|
let mut overlapped: c::OVERLAPPED = mem::zeroed();
|
|
|
|
|
overlapped.Offset = offset as u32;
|
|
|
|
|
overlapped.OffsetHigh = (offset >> 32) as u32;
|
2019-11-27 10:28:39 -08:00
|
|
|
cvt(c::ReadFile(self.0, buf.as_mut_ptr() as c::LPVOID, len, &mut read, &mut overlapped))
|
2016-08-16 01:11:33 +02:00
|
|
|
};
|
|
|
|
|
match res {
|
|
|
|
|
Ok(_) => Ok(read as usize),
|
|
|
|
|
Err(ref e) if e.raw_os_error() == Some(c::ERROR_HANDLE_EOF as i32) => Ok(0),
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
pub unsafe fn read_overlapped(
|
|
|
|
|
&self,
|
|
|
|
|
buf: &mut [u8],
|
|
|
|
|
overlapped: *mut c::OVERLAPPED,
|
|
|
|
|
) -> io::Result<Option<usize>> {
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
|
2016-02-12 10:29:25 -08:00
|
|
|
let mut amt = 0;
|
2020-03-27 21:55:15 +01:00
|
|
|
let res = cvt(c::ReadFile(self.0, buf.as_ptr() as c::LPVOID, len, &mut amt, overlapped));
|
2016-02-12 10:29:25 -08:00
|
|
|
match res {
|
|
|
|
|
Ok(_) => Ok(Some(amt as usize)),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
|
|
|
|
|
Ok(None)
|
|
|
|
|
} else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) {
|
|
|
|
|
Ok(Some(0))
|
|
|
|
|
} else {
|
|
|
|
|
Err(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
pub fn overlapped_result(
|
|
|
|
|
&self,
|
|
|
|
|
overlapped: *mut c::OVERLAPPED,
|
|
|
|
|
wait: bool,
|
|
|
|
|
) -> io::Result<usize> {
|
2016-02-12 10:29:25 -08:00
|
|
|
unsafe {
|
|
|
|
|
let mut bytes = 0;
|
2019-11-27 10:28:39 -08:00
|
|
|
let wait = if wait { c::TRUE } else { c::FALSE };
|
2020-03-27 21:55:15 +01:00
|
|
|
let res = cvt(c::GetOverlappedResult(self.raw(), overlapped, &mut bytes, wait));
|
2016-02-12 10:29:25 -08:00
|
|
|
match res {
|
|
|
|
|
Ok(_) => Ok(bytes as usize),
|
|
|
|
|
Err(e) => {
|
2019-11-27 10:28:39 -08:00
|
|
|
if e.raw_os_error() == Some(c::ERROR_HANDLE_EOF as i32)
|
|
|
|
|
|| e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32)
|
|
|
|
|
{
|
2016-02-12 10:29:25 -08:00
|
|
|
Ok(0)
|
|
|
|
|
} else {
|
|
|
|
|
Err(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn cancel_io(&self) -> io::Result<()> {
|
2020-01-02 08:56:12 +00:00
|
|
|
unsafe { cvt(c::CancelIo(self.raw())).map(drop) }
|
2016-02-12 10:29:25 -08:00
|
|
|
}
|
|
|
|
|
|
2015-02-06 09:42:57 -08:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2015-04-14 11:17:47 -07:00
|
|
|
let mut amt = 0;
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
|
2016-03-22 22:01:37 -05:00
|
|
|
cvt(unsafe {
|
2019-11-27 10:28:39 -08:00
|
|
|
c::WriteFile(self.0, buf.as_ptr() as c::LPVOID, len, &mut amt, ptr::null_mut())
|
2016-03-22 22:01:37 -05:00
|
|
|
})?;
|
2015-04-14 11:17:47 -07:00
|
|
|
Ok(amt as usize)
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
2015-04-27 13:44:20 -07:00
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 12:51:25 -07:00
|
|
|
crate::io::default_write_vectored(|buf| self.write(buf), bufs)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 01:11:33 +02:00
|
|
|
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
|
|
|
|
|
let mut written = 0;
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
|
2016-08-16 01:11:33 +02:00
|
|
|
unsafe {
|
|
|
|
|
let mut overlapped: c::OVERLAPPED = mem::zeroed();
|
|
|
|
|
overlapped.Offset = offset as u32;
|
|
|
|
|
overlapped.OffsetHigh = (offset >> 32) as u32;
|
2019-11-27 10:28:39 -08:00
|
|
|
cvt(c::WriteFile(
|
|
|
|
|
self.0,
|
|
|
|
|
buf.as_ptr() as c::LPVOID,
|
|
|
|
|
len,
|
|
|
|
|
&mut written,
|
|
|
|
|
&mut overlapped,
|
|
|
|
|
))?;
|
2016-08-16 01:11:33 +02:00
|
|
|
}
|
|
|
|
|
Ok(written as usize)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
pub fn duplicate(
|
|
|
|
|
&self,
|
|
|
|
|
access: c::DWORD,
|
|
|
|
|
inherit: bool,
|
|
|
|
|
options: c::DWORD,
|
|
|
|
|
) -> io::Result<Handle> {
|
2015-11-02 16:23:22 -08:00
|
|
|
let mut ret = 0 as c::HANDLE;
|
2016-03-22 22:01:37 -05:00
|
|
|
cvt(unsafe {
|
2015-11-02 16:23:22 -08:00
|
|
|
let cur_proc = c::GetCurrentProcess();
|
2019-11-27 10:28:39 -08:00
|
|
|
c::DuplicateHandle(
|
|
|
|
|
cur_proc,
|
|
|
|
|
self.0,
|
|
|
|
|
cur_proc,
|
|
|
|
|
&mut ret,
|
|
|
|
|
access,
|
|
|
|
|
inherit as c::BOOL,
|
|
|
|
|
options,
|
|
|
|
|
)
|
2016-03-22 22:01:37 -05:00
|
|
|
})?;
|
2015-04-27 13:44:20 -07:00
|
|
|
Ok(Handle::new(ret))
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
2016-02-12 00:17:24 -08:00
|
|
|
|
|
|
|
|
impl<'a> Read for &'a RawHandle {
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
(**self).read(buf)
|
|
|
|
|
}
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 12:51:25 -07:00
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 12:51:25 -07:00
|
|
|
(**self).read_vectored(bufs)
|
|
|
|
|
}
|
2016-02-12 00:17:24 -08:00
|
|
|
}
|