From 6d6f88fb40a45f07d8f61fbc3f8bbccea3174657 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Tue, 6 Feb 2024 22:44:18 +0530 Subject: [PATCH] Migrate from `winapi` to `windows-rs` --- Cargo.lock | 21 +++++++++- server/Cargo.toml | 9 ++-- server/src/util/os.rs | 24 ++++++----- server/src/util/os/flock.rs | 68 ++++++++++++------------------- server/src/util/os/free_memory.rs | 13 ++---- 5 files changed, 69 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d759d1d..d60f8e6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1262,7 +1262,7 @@ dependencies = [ "tokio", "tokio-openssl", "uuid", - "winapi", + "windows", ] [[package]] @@ -1525,6 +1525,25 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/server/Cargo.toml b/server/Cargo.toml index 8f506462..f9d6e274 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -30,10 +30,11 @@ serde_yaml = "0.9.31" jemallocator = "0.5.4" [target.'cfg(target_os = "windows")'.dependencies] # external deps -winapi = { version = "0.3.9", features = [ - "fileapi", - "sysinfoapi", - "minwinbase", +windows = { version = "0.52.0", features = [ + "Win32_Foundation", + "Win32_System_IO", + "Win32_Storage_FileSystem", + "Win32_System_SystemInformation", ] } [target.'cfg(unix)'.dependencies] diff --git a/server/src/util/os.rs b/server/src/util/os.rs index 9e1906aa..8d4d28d4 100644 --- a/server/src/util/os.rs +++ b/server/src/util/os.rs @@ -371,7 +371,7 @@ mod uptime_impl { #[cfg(target_os = "windows")] pub(super) fn uptime() -> std::io::Result { - Ok(unsafe { winapi::um::sysinfoapi::GetTickCount64() } as u128) + Ok(unsafe { windows::Win32::System::SystemInformation::GetTickCount64() as u128 }) } } @@ -424,18 +424,22 @@ mod hostname_impl { #[cfg(target_family = "windows")] fn get_hostname() -> Hostname { - use winapi::shared::minwindef::DWORD; - use winapi::um::sysinfoapi::{self, GetComputerNameExA}; - + use windows::{ + core::PSTR, + Win32::System::SystemInformation::{ + ComputerNamePhysicalDnsHostname, GetComputerNameExA, + }, + }; let mut buf: [u8; 256] = [0; 256]; - let mut size: DWORD = buf.len() as u32; - + let mut size: u32 = buf.len() as u32; unsafe { + // UNSAFE(@ohsayan): correct call to the windows API GetComputerNameExA( - sysinfoapi::ComputerNamePhysicalDnsHostname, - buf.as_mut_ptr().cast(), - &mut size, - ); + ComputerNamePhysicalDnsHostname, + PSTR(buf.as_mut_ptr()), + &mut size as *mut u32, + ) + .unwrap(); Hostname::new_from_raw_buf(&buf) } } diff --git a/server/src/util/os/flock.rs b/server/src/util/os/flock.rs index 8af608ca..5c083d38 100644 --- a/server/src/util/os/flock.rs +++ b/server/src/util/os/flock.rs @@ -23,22 +23,29 @@ * along with this program. If not, see . * */ +// Add this to Cargo.toml: +// windows = "0.33.0" or the latest version -// unix imports #[cfg(unix)] extern crate libc; -// windows imports #[cfg(windows)] -extern crate winapi; -#[cfg(windows)] -use std::os::windows::io::AsRawHandle; +use { + std::os::windows::io::AsRawHandle, + windows::Win32::{ + Foundation::HANDLE, + Storage::FileSystem::{ + LockFileEx, UnlockFileEx, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, + }, + System::IO::OVERLAPPED, + }, +}; use std::{fs::File, io, path::Path}; pub struct FileLock { _file: File, #[cfg(windows)] - handle: winapi::um::winnt::HANDLE, + handle: HANDLE, } impl FileLock { @@ -46,35 +53,21 @@ impl FileLock { let file = File::create(path)?; #[cfg(windows)] { - use { - std::mem, - winapi::um::{ - fileapi::LockFileEx, - minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY}, - winnt::HANDLE, - }, - }; let handle = file.as_raw_handle(); - let mut overlapped = unsafe { mem::zeroed() }; - let result = unsafe { + let mut overlapped = OVERLAPPED::default(); + unsafe { LockFileEx( - handle as HANDLE, + HANDLE(handle as isize), LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, - u32::MAX, - u32::MAX, + u32::MAX as u32, + u32::MAX as u32, &mut overlapped, ) - }; - if result == 0 { - return Err(io::Error::new( - io::ErrorKind::AlreadyExists, - "file is already locked", - )); - } + }?; return Ok(Self { _file: file, - handle, + handle: HANDLE(handle as isize), }); } #[cfg(unix)] @@ -96,25 +89,16 @@ impl FileLock { pub fn release(self) -> io::Result<()> { #[cfg(windows)] { - use { - std::mem, - winapi::um::{fileapi::UnlockFileEx, winnt::HANDLE}, - }; - - let mut overlapped = unsafe { mem::zeroed() }; - let result = unsafe { + let mut overlapped = OVERLAPPED::default(); + unsafe { UnlockFileEx( - self.handle as HANDLE, + self.handle, 0, - u32::MAX, - u32::MAX, + u32::MAX as u32, + u32::MAX as u32, &mut overlapped, ) - }; - - if result == 0 { - return Err(io::Error::last_os_error()); - } + }?; } #[cfg(unix)] { diff --git a/server/src/util/os/free_memory.rs b/server/src/util/os/free_memory.rs index 2671b307..444cfae6 100644 --- a/server/src/util/os/free_memory.rs +++ b/server/src/util/os/free_memory.rs @@ -24,24 +24,19 @@ * */ -#[cfg(target_os = "windows")] -extern crate winapi; - #[cfg(any(target_os = "linux", target_os = "macos"))] extern crate libc; pub fn free_memory_in_bytes() -> u64 { #[cfg(target_os = "windows")] { - use winapi::um::sysinfoapi::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; - - let mut statex: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; + use windows::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; + let mut statex = MEMORYSTATUSEX::default(); statex.dwLength = std::mem::size_of::() as u32; - unsafe { - GlobalMemoryStatusEx(&mut statex); + // UNSAFE(@ohsayan): correct call to windows API + GlobalMemoryStatusEx(&mut statex).unwrap(); } - // Return free physical memory return statex.ullAvailPhys; }