1 #![unstable(issue = "none", feature = "windows_handle")]
4 use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf};
6 use crate::os::windows::io::{
7 AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
12 use crate::sys_common::{AsInner, FromInner, IntoInner};
14 /// An owned container for `HANDLE` object, closing them on Drop.
16 /// All methods are inherited through a `Deref` impl to `RawHandle`
17 pub struct Handle(OwnedHandle);
20 pub fn new_event(manual: bool, init: bool) -> io::Result<Handle> {
23 c::CreateEventW(ptr::null_mut(), manual as c::BOOL, init as c::BOOL, ptr::null());
25 Err(io::Error::last_os_error())
27 Ok(Handle::from_raw_handle(event))
33 impl AsInner<OwnedHandle> for Handle {
34 fn as_inner(&self) -> &OwnedHandle {
39 impl IntoInner<OwnedHandle> for Handle {
40 fn into_inner(self) -> OwnedHandle {
45 impl FromInner<OwnedHandle> for Handle {
46 fn from_inner(file_desc: OwnedHandle) -> Self {
51 impl AsHandle for Handle {
52 fn as_handle(&self) -> BorrowedHandle<'_> {
57 impl AsRawHandle for Handle {
58 fn as_raw_handle(&self) -> RawHandle {
59 self.0.as_raw_handle()
63 impl IntoRawHandle for Handle {
64 fn into_raw_handle(self) -> RawHandle {
65 self.0.into_raw_handle()
69 impl FromRawHandle for Handle {
70 unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
71 Self(FromRawHandle::from_raw_handle(raw_handle))
76 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
77 let res = unsafe { self.synchronous_read(buf.as_mut_ptr().cast(), buf.len(), None) };
80 Ok(read) => Ok(read as usize),
82 // The special treatment of BrokenPipe is to deal with Windows
83 // pipe semantics, which yields this error when *reading* from
84 // a pipe after the other end has closed; we interpret that as
86 Err(ref e) if e.kind() == ErrorKind::BrokenPipe => Ok(0),
92 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
93 crate::io::default_read_vectored(|buf| self.read(buf), bufs)
97 pub fn is_read_vectored(&self) -> bool {
101 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
103 unsafe { self.synchronous_read(buf.as_mut_ptr().cast(), buf.len(), Some(offset)) };
106 Ok(read) => Ok(read as usize),
107 Err(ref e) if e.raw_os_error() == Some(c::ERROR_HANDLE_EOF as i32) => Ok(0),
112 pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
114 self.synchronous_read(buf.unfilled_mut().as_mut_ptr(), buf.remaining(), None)
119 // Safety: `read` bytes were written to the initialized portion of the buffer
121 buf.assume_init(read as usize);
123 buf.add_filled(read as usize);
127 // The special treatment of BrokenPipe is to deal with Windows
128 // pipe semantics, which yields this error when *reading* from
129 // a pipe after the other end has closed; we interpret that as
131 Err(ref e) if e.kind() == ErrorKind::BrokenPipe => Ok(()),
137 pub unsafe fn read_overlapped(
140 overlapped: *mut c::OVERLAPPED,
141 ) -> io::Result<Option<usize>> {
142 let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
144 let res = cvt(c::ReadFile(
146 buf.as_ptr() as c::LPVOID,
152 Ok(_) => Ok(Some(amt as usize)),
154 if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
156 } else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) {
165 pub fn overlapped_result(
167 overlapped: *mut c::OVERLAPPED,
169 ) -> io::Result<usize> {
172 let wait = if wait { c::TRUE } else { c::FALSE };
174 cvt(c::GetOverlappedResult(self.as_raw_handle(), overlapped, &mut bytes, wait));
176 Ok(_) => Ok(bytes as usize),
178 if e.raw_os_error() == Some(c::ERROR_HANDLE_EOF as i32)
179 || e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32)
190 pub fn cancel_io(&self) -> io::Result<()> {
191 unsafe { cvt(c::CancelIo(self.as_raw_handle())).map(drop) }
194 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
195 self.synchronous_write(&buf, None)
198 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
199 crate::io::default_write_vectored(|buf| self.write(buf), bufs)
203 pub fn is_write_vectored(&self) -> bool {
207 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
208 self.synchronous_write(&buf, Some(offset))
211 pub fn try_clone(&self) -> io::Result<Self> {
212 Ok(Self(self.0.try_clone()?))
220 ) -> io::Result<Self> {
221 Ok(Self(self.0.duplicate(access, inherit, options)?))
224 #[cfg(not(target_vendor = "uwp"))]
225 pub(crate) fn set_inheritable(&self) -> io::Result<()> {
226 self.0.set_inheritable()
229 /// Performs a synchronous read.
231 /// If the handle is opened for asynchronous I/O then this abort the process.
234 /// If `offset` is `None` then the current file position is used.
235 unsafe fn synchronous_read(
237 buf: *mut mem::MaybeUninit<u8>,
240 ) -> io::Result<usize> {
241 let mut io_status = c::IO_STATUS_BLOCK::default();
243 // The length is clamped at u32::MAX.
244 let len = cmp::min(len, c::DWORD::MAX as usize) as c::DWORD;
245 let status = c::NtReadFile(
253 offset.map(|n| n as _).as_ref(),
257 // If the operation has not completed then abort the process.
258 // Doing otherwise means that the buffer and stack may be written to
259 // after this function returns.
260 c::STATUS_PENDING => {
261 eprintln!("I/O error: operation failed to complete synchronously");
262 crate::process::abort();
265 // Return `Ok(0)` when there's nothing more to read.
266 c::STATUS_END_OF_FILE => Ok(0),
269 status if c::nt_success(status) => Ok(io_status.Information),
272 let error = c::RtlNtStatusToDosError(status);
273 Err(io::Error::from_raw_os_error(error as _))
278 /// Performs a synchronous write.
280 /// If the handle is opened for asynchronous I/O then this abort the process.
283 /// If `offset` is `None` then the current file position is used.
284 fn synchronous_write(&self, buf: &[u8], offset: Option<u64>) -> io::Result<usize> {
285 let mut io_status = c::IO_STATUS_BLOCK::default();
287 // The length is clamped at u32::MAX.
288 let len = cmp::min(buf.len(), c::DWORD::MAX as usize) as c::DWORD;
289 let status = unsafe {
298 offset.map(|n| n as _).as_ref(),
303 // If the operation has not completed then abort the process.
304 // Doing otherwise means that the buffer may be read and the stack
305 // written to after this function returns.
306 c::STATUS_PENDING => {
307 rtabort!("I/O error: operation failed to complete synchronously");
311 status if c::nt_success(status) => Ok(io_status.Information),
314 let error = unsafe { c::RtlNtStatusToDosError(status) };
315 Err(io::Error::from_raw_os_error(error as _))
321 impl<'a> Read for &'a Handle {
322 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
326 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
327 (**self).read_vectored(bufs)