diff --git a/Cargo.lock b/Cargo.lock index 20d2a8a3..2d097b4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,9 +46,9 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "cc" -version = "1.0.83" +version = "1.0.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +checksum = "0f8e7c90afad890484a21653d08b6e209ae34770fb5ee298f9c699fcc1e5c856" dependencies = [ "libc", ] @@ -98,9 +98,9 @@ dependencies = [ [[package]] name = "futures" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" dependencies = [ "futures-channel", "futures-core", @@ -113,9 +113,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" dependencies = [ "futures-core", "futures-sink", @@ -123,15 +123,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" [[package]] name = "futures-executor" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" dependencies = [ "futures-core", "futures-task", @@ -140,15 +140,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" [[package]] name = "futures-macro" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" dependencies = [ "proc-macro2", "quote", @@ -157,21 +157,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" +checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" [[package]] name = "futures-task" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" [[package]] name = "futures-util" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" dependencies = [ "futures-channel", "futures-core", @@ -217,7 +217,8 @@ dependencies = [ "rustc_version", "to_method", "tokio", - "winapi", + "windows", + "windows-sys", ] [[package]] @@ -228,9 +229,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.149" +version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" [[package]] name = "memchr" @@ -249,9 +250,9 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.8" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" dependencies = [ "libc", "wasi", @@ -303,9 +304,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "proc-macro2" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b1106fec09662ec6dd98ccac0f81cef56984d0b49f75c92d8cbad76e20c005c" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] @@ -336,9 +337,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad977052201c6de01a8ef2aa3378c4bd23217a056337d1d6da40468d267a4fb0" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "sharded-slab" @@ -360,9 +361,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.4" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" +checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" dependencies = [ "libc", "windows-sys", @@ -370,9 +371,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.38" +version = "2.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" dependencies = [ "proc-macro2", "quote", @@ -397,9 +398,9 @@ checksum = "c7c4ceeeca15c8384bbc3e011dbd8fccb7f068a440b752b7d9b32ceb0ca0e2e8" [[package]] name = "tokio" -version = "1.32.0" +version = "1.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17ed6077ed6cd6c74735e21f37eb16dc3935f96878b1fe961074089cc80893f9" +checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" dependencies = [ "backtrace", "bytes", @@ -414,9 +415,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", @@ -425,20 +426,19 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.37" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ - "cfg-if", "pin-project-lite", "tracing-core", ] [[package]] name = "tracing-core" -version = "0.1.31" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", "valuable", @@ -484,26 +484,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] -name = "winapi" -version = "0.3.9" +name = "windows" +version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "windows-core", + "windows-targets", ] [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows-core" +version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +dependencies = [ + "windows-targets", +] [[package]] name = "windows-sys" diff --git a/Cargo.toml b/Cargo.toml index 926ea727..9993bccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,15 +58,15 @@ futures = "0.3.28" color-eyre = "0.6.2" [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3", features = [ - "std", - "winbase", - "winerror", - "processthreadsapi", - "fileapi", - "handleapi", - "namedpipeapi", -] } +windows = "0.51.1" +windows-sys = {version = "0.48.0", features = [ + "Win32_Foundation", + "Win32_Security", + "Win32_Storage_FileSystem", + "Win32_System_IO", + "Win32_System_Pipes", + "Win32_System_Threading", +]} [target.'cfg(unix)'.dependencies] libc = { version = "0.2.137", features = ["extra_traits"] } diff --git a/src/os/windows/c_wrappers.rs b/src/os/windows/c_wrappers.rs index 9d06470d..7e44e178 100644 --- a/src/os/windows/c_wrappers.rs +++ b/src/os/windows/c_wrappers.rs @@ -1,35 +1,34 @@ use super::winprelude::*; +use crate::os::windows::get_borrowed; use std::{ io, mem::{size_of, zeroed}, }; -use winapi::um::{ - handleapi::DuplicateHandle, minwinbase::SECURITY_ATTRIBUTES, processthreadsapi::GetCurrentProcess, - winnt::DUPLICATE_SAME_ACCESS, +use windows_sys::Win32::{ + Foundation::{DuplicateHandle, DUPLICATE_SAME_ACCESS}, + Security::SECURITY_ATTRIBUTES, + System::Threading::GetCurrentProcess, }; pub fn duplicate_handle(handle: BorrowedHandle<'_>) -> io::Result { let raw = duplicate_handle_inner(handle, None)?; - unsafe { Ok(OwnedHandle::from_raw_handle(raw)) } + unsafe { Ok(OwnedHandle::from_raw_handle(raw as RawHandle)) } } pub fn duplicate_handle_to_foreign( handle: BorrowedHandle<'_>, other_process: BorrowedHandle<'_>, -) -> io::Result { +) -> io::Result { duplicate_handle_inner(handle, Some(other_process)) } -fn duplicate_handle_inner( - handle: BorrowedHandle<'_>, - other_process: Option>, -) -> io::Result { +fn duplicate_handle_inner(handle: BorrowedHandle<'_>, other_process: Option>) -> io::Result { let mut new_handle = INVALID_HANDLE_VALUE; let success = unsafe { let proc = GetCurrentProcess(); DuplicateHandle( proc, - handle.as_raw_handle(), - other_process.map(|h| h.as_raw_handle()).unwrap_or(proc), + get_borrowed(handle), + other_process.map(|h| get_borrowed(h)).unwrap_or(proc), &mut new_handle, 0, 0, diff --git a/src/os/windows/file_handle.rs b/src/os/windows/file_handle.rs index aa808515..f7f9dbf7 100644 --- a/src/os/windows/file_handle.rs +++ b/src/os/windows/file_handle.rs @@ -1,14 +1,14 @@ -use super::{c_wrappers, downgrade_eof, winprelude::*}; +use super::{c_wrappers, downgrade_eof, get_borrowed, get_owned, winprelude::*}; use crate::TryClone; use std::{io, mem::MaybeUninit, ptr}; -use winapi::um::fileapi::{FlushFileBuffers, ReadFile, WriteFile}; +use windows_sys::Win32::Storage::FileSystem::{FlushFileBuffers, ReadFile, WriteFile}; /// Newtype wrapper which defines file I/O operations on a `HANDLE` to a file. #[repr(transparent)] pub(crate) struct FileHandle(pub(crate) OwnedHandle); impl FileHandle { pub fn read(&self, buf: &mut [MaybeUninit]) -> io::Result { - let len = DWORD::try_from(buf.len()).map_err(|_| { + let len = u32::try_from(buf.len()).map_err(|_| { io::Error::new( io::ErrorKind::InvalidInput, "buffer is bigger than maximum buffer size for ReadFile", @@ -16,9 +16,9 @@ impl FileHandle { })?; let (success, num_bytes_read) = unsafe { - let mut num_bytes_read: DWORD = 0; + let mut num_bytes_read: u32 = 0; let result = ReadFile( - self.0.as_raw_handle(), + get_owned(&self.0), buf.as_mut_ptr().cast(), len, &mut num_bytes_read as *mut _, @@ -29,7 +29,7 @@ impl FileHandle { downgrade_eof(ok_or_ret_errno!(success => num_bytes_read)) } pub fn write(&self, buf: &[u8]) -> io::Result { - let len = DWORD::try_from(buf.len()).map_err(|_| { + let len = u32::try_from(buf.len()).map_err(|_| { io::Error::new( io::ErrorKind::InvalidInput, "buffer is bigger than maximum buffer size for ReadFile", @@ -37,9 +37,9 @@ impl FileHandle { })?; let (success, bytes_written) = unsafe { - let mut bytes_written: DWORD = 0; + let mut bytes_written: u32 = 0; let result = WriteFile( - self.0.as_raw_handle(), + get_owned(&self.0), buf.as_ptr().cast(), len, &mut bytes_written as *mut _, @@ -51,7 +51,7 @@ impl FileHandle { } #[inline(always)] pub fn flush(&self) -> io::Result<()> { - Self::flush_hndl(self.0.as_raw_handle()) + Self::flush_hndl(get_owned(&self.0)) } #[inline] pub fn flush_hndl(handle: HANDLE) -> io::Result<()> { diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 463aecac..7b77faa7 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -13,14 +13,9 @@ pub(crate) use file_handle::*; use std::{io, task::Poll}; mod winprelude { pub use std::os::windows::prelude::*; - pub use winapi::{ - shared::{ - minwindef::{BOOL, DWORD, LPVOID}, - ntdef::HANDLE, - }, - um::handleapi::INVALID_HANDLE_VALUE, - }; + pub use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE}; } +use windows_sys::Win32::Foundation::ERROR_PIPE_NOT_CONNECTED; use winprelude::*; mod c_wrappers; @@ -29,7 +24,7 @@ mod c_wrappers; /// /// On Windows, like with most other operating systems, handles belong to specific processes. You shouldn't just send /// the value of a handle to another process (with a named pipe, for example) and expect it to work on the other side. -/// For this to work, you need [`DuplicateHandle`](winapi::um::handleapi::DuplicateHandle) – the Win32 API function +/// For this to work, you need [`DuplicateHandle`](windows_sys::Win32::Foundation::DuplicateHandle) – the Win32 API function /// which duplicates a handle into the handle table of the specified process (the receiver is referred to by its /// handle). This trait exposes the `DuplicateHandle` functionality in a safe manner. /// @@ -44,7 +39,7 @@ pub trait ShareHandle: AsHandle { /// the only way to use any form of IPC other than named pipes to communicate between two processes which do not /// have a parent-child relationship or if the handle wasn't created as inheritable. /// - /// Backed by [`DuplicateHandle`](winapi::um::handleapi::DuplicateHandle). Doesn't require unsafe code since + /// Backed by [`DuplicateHandle`](windows_sys::Win32::Foundation::DuplicateHandle). Doesn't require unsafe code since /// `DuplicateHandle` never leads to undefined behavior if the `lpTargetHandle` parameter is a valid pointer, only /// creates an error. #[allow(clippy::not_unsafe_ptr_arg_deref)] // Handles are not pointers, they have handle checks @@ -56,8 +51,7 @@ impl ShareHandle for crate::unnamed_pipe::UnnamedPipeReader {} impl ShareHandle for crate::unnamed_pipe::UnnamedPipeWriter {} fn is_eof_like(e: &io::Error) -> bool { - e.kind() == io::ErrorKind::BrokenPipe - || e.raw_os_error() == Some(winapi::shared::winerror::ERROR_PIPE_NOT_CONNECTED as _) + e.kind() == io::ErrorKind::BrokenPipe || e.raw_os_error() == Some(ERROR_PIPE_NOT_CONNECTED as _) } #[allow(unused)] @@ -70,3 +64,11 @@ fn downgrade_eof(r: io::Result) -> io::Result { els => els, } } + +fn get_borrowed(h: BorrowedHandle<'_>) -> HANDLE { + h.as_raw_handle() as HANDLE +} + +fn get_owned(h: &OwnedHandle) -> HANDLE { + h.as_raw_handle() as HANDLE +} diff --git a/src/os/windows/named_pipe/enums.rs b/src/os/windows/named_pipe/enums.rs index f89330dc..355efe6d 100644 --- a/src/os/windows/named_pipe/enums.rs +++ b/src/os/windows/named_pipe/enums.rs @@ -1,9 +1,9 @@ use super::PipeModeTag; use crate::os::windows::winprelude::*; use std::{convert::TryFrom, mem}; -use winapi::um::winbase::{ - PIPE_ACCESS_DUPLEX, PIPE_ACCESS_INBOUND, PIPE_ACCESS_OUTBOUND, PIPE_READMODE_BYTE, PIPE_READMODE_MESSAGE, - PIPE_TYPE_BYTE, PIPE_TYPE_MESSAGE, +use windows_sys::Win32::{ + Storage::FileSystem::{PIPE_ACCESS_DUPLEX, PIPE_ACCESS_INBOUND, PIPE_ACCESS_OUTBOUND}, + System::Pipes::{PIPE_READMODE_BYTE, PIPE_READMODE_MESSAGE, PIPE_TYPE_BYTE, PIPE_TYPE_MESSAGE}, }; /// The direction of a named pipe connection, designating who can read data and who can write it. This describes the @@ -75,19 +75,19 @@ impl PipeDirection { } } } -impl TryFrom for PipeDirection { +impl TryFrom for PipeDirection { type Error = (); /// Converts a Windows constant to a `PipeDirection` if it's in range. /// /// # Errors /// Returns `Err` if the value is not a valid pipe direction constant. - fn try_from(op: DWORD) -> Result { + fn try_from(op: u32) -> Result { assert!((1..=3).contains(&op)); // See the comment block above for why this is safe. unsafe { mem::transmute(op) } } } -impl From for DWORD { +impl From for u32 { fn from(op: PipeDirection) -> Self { unsafe { mem::transmute(op) } } @@ -180,28 +180,28 @@ pub enum PipeMode { Messages = PIPE_TYPE_MESSAGE, } impl PipeMode { - /// Converts the value into a raw `DWORD`-typed constant, either `PIPE_TYPE_BYTE` or `PIPE_TYPE_MESSAGE` depending + /// Converts the value into a raw `u32`-typed constant, either `PIPE_TYPE_BYTE` or `PIPE_TYPE_MESSAGE` depending /// on the value. - pub const fn to_pipe_type(self) -> DWORD { + pub const fn to_pipe_type(self) -> u32 { self as _ } - /// Converts the value into a raw `DWORD`-typed constant, either `PIPE_READMODE_BYTE` or `PIPE_READMODE_MESSAGE` + /// Converts the value into a raw `u32`-typed constant, either `PIPE_READMODE_BYTE` or `PIPE_READMODE_MESSAGE` /// depending on the value. - pub const fn to_readmode(self) -> DWORD { + pub const fn to_readmode(self) -> u32 { match self { Self::Bytes => PIPE_READMODE_BYTE, Self::Messages => PIPE_READMODE_MESSAGE, } } } -impl TryFrom for PipeMode { +impl TryFrom for PipeMode { type Error = (); /// Converts a Windows constant to a `PipeMode` if it's in range. Both `PIPE_TYPE_*` and `PIPE_READMODE_*` are /// supported. /// /// # Errors /// Returns `Err` if the value is not a valid pipe stream mode constant. - fn try_from(op: DWORD) -> Result { + fn try_from(op: u32) -> Result { // It's nicer to only match than to check and transmute #[allow(unreachable_patterns)] // PIPE_READMODE_BYTE and PIPE_TYPE_BYTE are equal match op { diff --git a/src/os/windows/named_pipe/listener.rs b/src/os/windows/named_pipe/listener.rs index 2208db65..34c8c411 100644 --- a/src/os/windows/named_pipe/listener.rs +++ b/src/os/windows/named_pipe/listener.rs @@ -1,5 +1,5 @@ use super::{path_conversion, pipe_mode, PipeMode, PipeModeTag, PipeStream, PipeStreamRole, RawPipeStream}; -use crate::os::windows::{c_wrappers::init_security_attributes, winprelude::*, FileHandle}; +use crate::os::windows::{c_wrappers::init_security_attributes, get_borrowed, winprelude::*, FileHandle}; use std::{ borrow::Cow, ffi::OsStr, @@ -15,15 +15,10 @@ use std::{ }, }; use to_method::To; -use winapi::{ - shared::winerror::ERROR_PIPE_CONNECTED, - um::{ - namedpipeapi::{ConnectNamedPipe, CreateNamedPipeW}, - winbase::{ - FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, FILE_FLAG_WRITE_THROUGH, PIPE_NOWAIT, - PIPE_REJECT_REMOTE_CLIENTS, - }, - }, +use windows_sys::Win32::{ + Foundation::ERROR_PIPE_CONNECTED, + Storage::FileSystem::{FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, FILE_FLAG_WRITE_THROUGH}, + System::Pipes::{ConnectNamedPipe, CreateNamedPipeW, PIPE_NOWAIT, PIPE_REJECT_REMOTE_CLIENTS}, }; /// The server for a named pipe, listening for connections to clients and producing pipe streams. @@ -170,10 +165,10 @@ pub struct PipeListenerOptions<'a> { pub accept_remote: bool, /// Specifies how big the input buffer should be. The system will automatically adjust this size to align it as /// required or clip it by the minimum or maximum buffer size. - pub input_buffer_size_hint: DWORD, + pub input_buffer_size_hint: u32, /// Specifies how big the output buffer should be. The system will automatically adjust this size to align it as /// required or clip it by the minimum or maximum buffer size. - pub output_buffer_size_hint: DWORD, + pub output_buffer_size_hint: u32, /// The default timeout clients use when connecting. Used unless another timeout is specified when waiting by a /// client. // TODO use WaitTimeout struct @@ -239,8 +234,8 @@ impl<'a> PipeListenerOptions<'a> { instance_limit: Option, write_through: bool, accept_remote: bool, - input_buffer_size_hint: DWORD, - output_buffer_size_hint: DWORD, + input_buffer_size_hint: u32, + output_buffer_size_hint: u32, wait_timeout: NonZeroU32, ); /// Creates an instance of a pipe for a listener with the specified stream type and with the first-instance flag set @@ -277,7 +272,7 @@ cannot create pipe server that has byte type but reads messages – have you for "cannot set 255 as the named pipe instance limit due to 255 being a reserved value", )) } - Some(x) => x.to::(), + Some(x) => x.to::(), None => 255, }; @@ -296,7 +291,7 @@ cannot create pipe server that has byte type but reads messages – have you for }; ok_or_ret_errno!(success => unsafe { // SAFETY: we just made it and received ownership - OwnedHandle::from_raw_handle(handle) + OwnedHandle::from_raw_handle(handle as RawHandle) }) } /// Creates the pipe listener from the builder. The `Rm` and `Sm` generic arguments specify the type of pipe stream @@ -343,9 +338,9 @@ cannot create pipe server that has byte type but reads messages – have you for Ok((owned_config, instance)) } - fn open_mode(&self, first: bool, role: PipeStreamRole, overlapped: bool) -> DWORD { + fn open_mode(&self, first: bool, role: PipeStreamRole, overlapped: bool) -> u32 { let mut open_mode = 0_u32; - open_mode |= role.direction_as_server().to::(); + open_mode |= role.direction_as_server().to::(); if first { open_mode |= FILE_FLAG_FIRST_PIPE_INSTANCE; } @@ -357,7 +352,7 @@ cannot create pipe server that has byte type but reads messages – have you for } open_mode } - fn pipe_mode(&self, read_mode: Option, nonblocking: bool) -> DWORD { + fn pipe_mode(&self, read_mode: Option, nonblocking: bool) -> u32 { let mut pipe_mode = 0_u32; pipe_mode |= self.mode.to_pipe_type(); pipe_mode |= read_mode.map_or(0, PipeMode::to_readmode); @@ -378,7 +373,7 @@ impl Default for PipeListenerOptions<'_> { } fn block_on_connect(handle: BorrowedHandle<'_>) -> io::Result<()> { - let success = unsafe { ConnectNamedPipe(handle.as_raw_handle(), ptr::null_mut()) != 0 }; + let success = unsafe { ConnectNamedPipe(get_borrowed(handle), ptr::null_mut()) != 0 }; if success { Ok(()) } else { diff --git a/src/os/windows/named_pipe/mod.rs b/src/os/windows/named_pipe/mod.rs index 925f1ae8..20a33ad0 100644 --- a/src/os/windows/named_pipe/mod.rs +++ b/src/os/windows/named_pipe/mod.rs @@ -43,8 +43,9 @@ mod path_conversion; pub mod tokio; use super::winprelude::*; +use crate::os::windows::get_borrowed; use std::{io, ptr}; -use winapi::um::namedpipeapi::SetNamedPipeHandleState; +use windows_sys::Win32::System::Pipes::SetNamedPipeHandleState; unsafe fn set_nonblocking_for_stream( handle: BorrowedHandle<'_>, @@ -57,7 +58,7 @@ unsafe fn set_nonblocking_for_stream( let mut mode: u32 = read_mode | nonblocking as u32; let success = unsafe { SetNamedPipeHandleState( - handle.as_raw_handle(), + get_borrowed(handle), &mut mode as *mut _, ptr::null_mut(), ptr::null_mut(), diff --git a/src/os/windows/named_pipe/stream/impls.rs b/src/os/windows/named_pipe/stream/impls.rs index 928057db..784eafff 100644 --- a/src/os/windows/named_pipe/stream/impls.rs +++ b/src/os/windows/named_pipe/stream/impls.rs @@ -22,9 +22,9 @@ use std::{ slice, sync::atomic::Ordering, }; -use winapi::{ - shared::winerror::ERROR_MORE_DATA, - um::winbase::{ +use windows_sys::Win32::{ + Foundation::ERROR_MORE_DATA, + System::Pipes::{ GetNamedPipeClientProcessId, GetNamedPipeClientSessionId, GetNamedPipeServerProcessId, GetNamedPipeServerSessionId, }, diff --git a/src/os/windows/named_pipe/stream/limbo.rs b/src/os/windows/named_pipe/stream/limbo.rs index 5e2bb0a8..8d44911d 100644 --- a/src/os/windows/named_pipe/stream/limbo.rs +++ b/src/os/windows/named_pipe/stream/limbo.rs @@ -1,3 +1,4 @@ +use crate::os::windows::get_owned; use crate::{ os::windows::{ named_pipe::limbo_pool::{LimboPool, MaybeReject}, @@ -14,7 +15,7 @@ use std::{ }, thread, }; -use winapi::um::namedpipeapi::DisconnectNamedPipe; +use windows_sys::Win32::System::Pipes::DisconnectNamedPipe; pub(super) struct Corpse { pub handle: FileHandle, @@ -23,7 +24,7 @@ pub(super) struct Corpse { impl Corpse { #[inline] pub fn disconnect(&self) -> io::Result<()> { - let success = unsafe { DisconnectNamedPipe(self.handle.0.as_raw_handle()) != 0 }; + let success = unsafe { DisconnectNamedPipe(get_owned(&self.handle.0)) != 0 }; ok_or_ret_errno!(success => ()) } } diff --git a/src/os/windows/named_pipe/stream/wrapper_fns.rs b/src/os/windows/named_pipe/stream/wrapper_fns.rs index c1661aa1..c1377ff4 100644 --- a/src/os/windows/named_pipe/stream/wrapper_fns.rs +++ b/src/os/windows/named_pipe/stream/wrapper_fns.rs @@ -1,30 +1,31 @@ -use crate::os::windows::{winprelude::*, FileHandle}; -use std::{io, os::windows::prelude::*, ptr}; -use winapi::{ - shared::winerror::ERROR_PIPE_BUSY, - um::{ - fileapi::{CreateFileW, OPEN_EXISTING}, - handleapi::INVALID_HANDLE_VALUE, - namedpipeapi::{GetNamedPipeInfo, PeekNamedPipe, WaitNamedPipeW}, - winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE}, - }, +use crate::os::windows::{get_borrowed, winprelude::*, FileHandle}; +use std::{ + io, + os::windows::{io::RawHandle, prelude::*}, + ptr, +}; +use windows::core::imp::BOOL; +use windows_sys::Win32::{ + Foundation::{ERROR_PIPE_BUSY, GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE}, + Storage::FileSystem::{CreateFileW, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING}, + System::Pipes::{GetNamedPipeInfo, PeekNamedPipe, WaitNamedPipeW}, }; -/// Helper for several functions that take a handle and a DWORD out-pointer. +/// Helper for several functions that take a handle and a u32 out-pointer. pub(crate) unsafe fn hget( handle: BorrowedHandle<'_>, - f: unsafe extern "system" fn(HANDLE, *mut DWORD) -> BOOL, -) -> io::Result { + f: unsafe extern "system" fn(HANDLE, *mut u32) -> BOOL, +) -> io::Result { let mut x: u32 = 0; - let ok = unsafe { f(handle.as_raw_handle(), &mut x as *mut _) != 0 }; + let ok = unsafe { f(get_borrowed(handle), &mut x as *mut _) != 0 }; ok_or_ret_errno!(ok => x) } -pub(crate) fn get_flags(handle: BorrowedHandle<'_>) -> io::Result { +pub(crate) fn get_flags(handle: BorrowedHandle<'_>) -> io::Result { let mut flags: u32 = 0; let success = unsafe { GetNamedPipeInfo( - handle.as_raw_handle(), + get_borrowed(handle), &mut flags as *mut _, ptr::null_mut(), ptr::null_mut(), @@ -48,10 +49,10 @@ pub(crate) fn has_msg_boundaries_from_sys(handle: BorrowedHandle<'_>) -> io::Res Ok((flags & PIPE_IS_MESSAGE_BIT) != 0) } pub(crate) fn peek_msg_len(handle: BorrowedHandle<'_>) -> io::Result { - let mut len: DWORD = 0; + let mut len: u32 = 0; let ok = unsafe { PeekNamedPipe( - handle.as_raw_handle(), + get_borrowed(handle), ptr::null_mut(), 0, ptr::null_mut(), @@ -80,7 +81,7 @@ fn connect_without_waiting(path: &[u16], read: bool, write: bool) -> io::Result< let handle = CreateFileW( path.as_ptr().cast(), { - let mut access_flags: DWORD = 0; + let mut access_flags: u32 = 0; if read { access_flags |= GENERIC_READ; } @@ -93,17 +94,17 @@ fn connect_without_waiting(path: &[u16], read: bool, write: bool) -> io::Result< ptr::null_mut(), OPEN_EXISTING, 0, - ptr::null_mut(), + 0, ); (handle != INVALID_HANDLE_VALUE, handle) }; ok_or_ret_errno!(success => unsafe { // SAFETY: we just created this handle - FileHandle(OwnedHandle::from_raw_handle(handle)) + FileHandle(OwnedHandle::from_raw_handle(handle as RawHandle)) }) } -#[repr(transparent)] // #[repr(DWORD)] +#[repr(transparent)] // #[repr(u32)] #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub(crate) struct WaitTimeout(u32); impl WaitTimeout { diff --git a/src/os/windows/named_pipe/tokio/stream/impls.rs b/src/os/windows/named_pipe/tokio/stream/impls.rs index 302ee32b..30bdc957 100644 --- a/src/os/windows/named_pipe/tokio/stream/impls.rs +++ b/src/os/windows/named_pipe/tokio/stream/impls.rs @@ -37,9 +37,9 @@ use tokio::{ net::windows::named_pipe::{NamedPipeClient as TokioNPClient, NamedPipeServer as TokioNPServer}, sync::MutexGuard as TokioMutexGuard, }; -use winapi::{ - shared::winerror::ERROR_MORE_DATA, - um::winbase::{ +use windows_sys::Win32::{ + Foundation::ERROR_MORE_DATA, + System::Pipes::{ GetNamedPipeClientProcessId, GetNamedPipeClientSessionId, GetNamedPipeServerProcessId, GetNamedPipeServerSessionId, }, @@ -424,7 +424,7 @@ impl PipeStream { return; } - let handle = AssertHandleSyncSend(self.as_raw_handle()); + let handle = AssertHandleSyncSend(self.as_raw_handle() as HANDLE); #[allow(clippy::redundant_locals)] let task = tokio::task::spawn_blocking(move || { let handle = handle; diff --git a/src/os/windows/named_pipe/tokio/stream/limbo.rs b/src/os/windows/named_pipe/tokio/stream/limbo.rs index e4db8b09..409f41af 100644 --- a/src/os/windows/named_pipe/tokio/stream/limbo.rs +++ b/src/os/windows/named_pipe/tokio/stream/limbo.rs @@ -1,6 +1,7 @@ //! Does not use the limbo pool. use super::*; +use crate::os::windows::get_borrowed; use crate::{ os::windows::{winprelude::*, FileHandle}, DebugExpectExt, @@ -45,7 +46,7 @@ outside of another Tokio runtime)", fn bury(c: Corpse) { task::spawn_blocking(move || { - let handle = c.0.as_handle().as_raw_handle(); + let handle = get_borrowed(c.0.as_handle()); FileHandle::flush_hndl(handle).debug_expect("limbo flush failed"); }); } diff --git a/src/os/windows/named_pipe/tokio/stream/wrapper_fns.rs b/src/os/windows/named_pipe/tokio/stream/wrapper_fns.rs index aa644ac0..b776c0ce 100644 --- a/src/os/windows/named_pipe/tokio/stream/wrapper_fns.rs +++ b/src/os/windows/named_pipe/tokio/stream/wrapper_fns.rs @@ -1,6 +1,6 @@ use std::{ffi::OsStr, io}; use tokio::net::windows::named_pipe::{ClientOptions, NamedPipeClient as TokioNPClient}; -use winapi::shared::winerror::ERROR_PIPE_BUSY; +use windows_sys::Win32::Foundation::ERROR_PIPE_BUSY; pub(crate) fn _connect(path: &OsStr, read: bool, write: bool) -> io::Result { let result = ClientOptions::new().read(read).write(write).open(path); diff --git a/src/os/windows/unnamed_pipe.rs b/src/os/windows/unnamed_pipe.rs index 1c6df076..34b45d8b 100644 --- a/src/os/windows/unnamed_pipe.rs +++ b/src/os/windows/unnamed_pipe.rs @@ -11,13 +11,14 @@ use crate::{ unnamed_pipe::{UnnamedPipeReader as PubReader, UnnamedPipeWriter as PubWriter}, weaken_buf_init_mut, }; +use core::ffi::c_void; use std::{ fmt::{self, Debug, Formatter}, io::{self, Read, Write}, num::NonZeroUsize, ptr, }; -use winapi::um::{minwinbase::SECURITY_ATTRIBUTES, namedpipeapi::CreatePipe}; +use windows_sys::Win32::{Security::SECURITY_ATTRIBUTES, System::Pipes::CreatePipe}; /// Builder used to create unnamed pipes while supplying additional options. /// @@ -35,7 +36,7 @@ pub struct UnnamedPipeCreationOptions { /// specific. /// /// [security descriptor]: https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-security_descriptor " " - pub security_descriptor: LPVOID, + pub security_descriptor: *mut c_void, /// A hint on the buffer size for the pipe. There is no way to ensure or check that the system actually uses this /// exact size, since it's only a hint. Set to `None` to disable the hint and rely entirely on the system's default /// buffer size. @@ -66,7 +67,7 @@ impl UnnamedPipeCreationOptions { /// /// [associated field]: #structfield.security_descriptor " " #[must_use = "this is not an in-place operation"] - pub fn security_descriptor(mut self, security_descriptor: LPVOID) -> Self { + pub fn security_descriptor(mut self, security_descriptor: *mut c_void) -> Self { self.security_descriptor = security_descriptor; self } @@ -134,8 +135,8 @@ impl UnnamedPipeCreationOptions { if success { let (w, r) = unsafe { // SAFETY: we just created those handles which means that we own them - let w = OwnedHandle::from_raw_handle(w); - let r = OwnedHandle::from_raw_handle(r); + let w = OwnedHandle::from_raw_handle(w as RawHandle); + let r = OwnedHandle::from_raw_handle(r as RawHandle); (w, r) }; let w = PubWriter(UnnamedPipeWriter(FileHandle(w)));