Migrate from `winapi` to `windows-rs`

next
Sayan Nandan 8 months ago
parent fb3b100937
commit 6d6f88fb40
No known key found for this signature in database
GPG Key ID: 0EBD769024B24F0A

21
Cargo.lock generated

@ -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"

@ -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]

@ -371,7 +371,7 @@ mod uptime_impl {
#[cfg(target_os = "windows")]
pub(super) fn uptime() -> std::io::Result<u128> {
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)
}
}

@ -23,22 +23,29 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
// 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)]
{

@ -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::<MEMORYSTATUSEX>() as u32;
unsafe {
GlobalMemoryStatusEx(&mut statex);
// UNSAFE(@ohsayan): correct call to windows API
GlobalMemoryStatusEx(&mut statex).unwrap();
}
// Return free physical memory
return statex.ullAvailPhys;
}

Loading…
Cancel
Save