Skip to content

Commit db55a2c

Browse files
kennykerrrami3l
authored andcommitted
Replace remaining winapi usage with windows-sys
1 parent 54dd3d0 commit db55a2c

File tree

9 files changed

+69
-101
lines changed

9 files changed

+69
-101
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,21 @@ zstd = "0.13"
104104
cc = "1"
105105
winreg = "0.52"
106106

107-
[target."cfg(windows)".dependencies.winapi]
108-
features = [
109-
"combaseapi",
110-
"errhandlingapi",
111-
"fileapi",
112-
"handleapi",
113-
"ioapiset",
114-
"jobapi",
115-
"jobapi2",
116-
"minwindef",
117-
"processthreadsapi",
118-
"psapi",
119-
"shlobj",
120-
"shtypes",
121-
"synchapi",
122-
"sysinfoapi",
123-
"tlhelp32",
124-
"userenv",
125-
"winbase",
126-
"winerror",
127-
"winioctl",
128-
"winnt",
129-
"winuser",
130-
]
131-
version = "0.3"
132-
133107
[target."cfg(windows)".dependencies.windows-sys]
134108
features = [
135109
"Win32_Foundation",
110+
"Win32_Security",
136111
"Win32_Storage_FileSystem",
112+
"Win32_System_Diagnostics_ToolHelp",
113+
"Win32_System_IO",
114+
"Win32_System_Ioctl",
115+
"Win32_System_JobObjects",
116+
"Win32_System_Kernel",
117+
"Win32_System_LibraryLoader",
118+
"Win32_System_SystemInformation",
137119
"Win32_System_SystemServices",
138120
"Win32_System_Threading",
139121
"Win32_System_WindowsProgramming",
140-
"Win32_Security",
141-
"Win32_System_Kernel",
142-
"Win32_System_IO",
143-
"Win32_System_Ioctl",
144122
]
145123
version = "0.52.0"
146124

src/bin/rustup-init.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ fn do_recursion_guard() -> Result<()> {
176176
/// rustup-init in the user's download folder.
177177
#[cfg(windows)]
178178
pub fn pre_rustup_main_init() {
179-
use winapi::um::libloaderapi::{SetDefaultDllDirectories, LOAD_LIBRARY_SEARCH_SYSTEM32};
179+
use windows_sys::Win32::System::LibraryLoader::{
180+
SetDefaultDllDirectories, LOAD_LIBRARY_SEARCH_SYSTEM32,
181+
};
180182
// Default to loading delay loaded DLLs from the system directory.
181183
// For DLLs loaded at load time, this relies on the `delayload` linker flag.
182184
// This is only necessary prior to Windows 10 RS1. See build.rs for details.

src/cli/download_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl DownloadTracker {
181181
let percent = (self.total_downloaded as f64 / content_len as f64) * 100.;
182182
let remaining = content_len - self.total_downloaded;
183183
let eta_h = Duration::from_secs(if speed == 0 {
184-
std::u64::MAX
184+
u64::MAX
185185
} else {
186186
(remaining / speed) as u64
187187
});

src/cli/job.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ mod imp {
4040
use std::mem;
4141
use std::ptr;
4242

43-
use winapi::shared::minwindef::*;
44-
use winapi::um::handleapi::*;
45-
use winapi::um::jobapi2::*;
46-
use winapi::um::processthreadsapi::*;
47-
use winapi::um::winnt::HANDLE;
48-
use winapi::um::winnt::*;
43+
use windows_sys::Win32::Foundation::*;
44+
use windows_sys::Win32::System::JobObjects::*;
45+
use windows_sys::Win32::System::Threading::*;
4946

5047
pub(crate) struct Setup {
5148
job: Handle,
@@ -70,7 +67,7 @@ mod imp {
7067
// we're otherwise part of someone else's job object in this case.
7168

7269
let job = CreateJobObjectW(ptr::null_mut(), ptr::null());
73-
if job.is_null() {
70+
if job == 0 {
7471
return None;
7572
}
7673
let job = Handle { inner: job };
@@ -85,8 +82,8 @@ mod imp {
8582
let r = SetInformationJobObject(
8683
job.inner,
8784
JobObjectExtendedLimitInformation,
88-
&mut info as *mut _ as LPVOID,
89-
mem::size_of_val(&info) as DWORD,
85+
&mut info as *mut _ as *const std::ffi::c_void,
86+
mem::size_of_val(&info) as u32,
9087
);
9188
if r == 0 {
9289
return None;
@@ -114,8 +111,8 @@ mod imp {
114111
let r = SetInformationJobObject(
115112
self.job.inner,
116113
JobObjectExtendedLimitInformation,
117-
&mut info as *mut _ as LPVOID,
118-
mem::size_of_val(&info) as DWORD,
114+
&mut info as *mut _ as *const std::ffi::c_void,
115+
mem::size_of_val(&info) as u32,
119116
);
120117
if r == 0 {
121118
info!("failed to configure job object to defaults: {}", last_err());

src/cli/self_update/windows.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,14 @@ pub fn complete_windows_uninstall() -> Result<utils::ExitCode> {
294294
pub(crate) fn wait_for_parent() -> Result<()> {
295295
use std::io;
296296
use std::mem;
297-
use winapi::shared::minwindef::DWORD;
298-
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
299-
use winapi::um::processthreadsapi::{GetCurrentProcessId, OpenProcess};
300-
use winapi::um::synchapi::WaitForSingleObject;
301-
use winapi::um::tlhelp32::{
297+
use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE, WAIT_OBJECT_0};
298+
use windows_sys::Win32::Storage::FileSystem::SYNCHRONIZE;
299+
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
302300
CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32, TH32CS_SNAPPROCESS,
303301
};
304-
use winapi::um::winbase::{INFINITE, WAIT_OBJECT_0};
305-
use winapi::um::winnt::SYNCHRONIZE;
302+
use windows_sys::Win32::System::Threading::{
303+
GetCurrentProcessId, OpenProcess, WaitForSingleObject, INFINITE,
304+
};
306305

307306
unsafe {
308307
// Take a snapshot of system processes, one of which is ours
@@ -318,7 +317,7 @@ pub(crate) fn wait_for_parent() -> Result<()> {
318317
});
319318

320319
let mut entry: PROCESSENTRY32 = mem::zeroed();
321-
entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
320+
entry.dwSize = mem::size_of::<PROCESSENTRY32>() as u32;
322321

323322
// Iterate over system processes looking for ours
324323
let success = Process32First(*snapshot, &mut entry);
@@ -343,7 +342,7 @@ pub(crate) fn wait_for_parent() -> Result<()> {
343342

344343
// Get a handle to the parent process
345344
let parent = OpenProcess(SYNCHRONIZE, 0, parent_id);
346-
if parent.is_null() {
345+
if parent == 0 {
347346
// This just means the parent has already exited.
348347
return Ok(());
349348
}
@@ -371,8 +370,8 @@ pub(crate) fn do_add_to_path() -> Result<()> {
371370

372371
fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
373372
use std::ptr;
374-
use winapi::shared::minwindef::*;
375-
use winapi::um::winuser::{
373+
use windows_sys::Win32::Foundation::*;
374+
use windows_sys::Win32::UI::WindowsAndMessaging::{
376375
SendMessageTimeoutA, HWND_BROADCAST, SMTO_ABORTIFHUNG, WM_SETTINGCHANGE,
377376
};
378377

@@ -645,12 +644,11 @@ pub(crate) fn delete_rustup_and_cargo_home() -> Result<()> {
645644
use std::ptr;
646645
use std::thread;
647646
use std::time::Duration;
648-
use winapi::shared::minwindef::DWORD;
649-
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
650-
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
651-
use winapi::um::minwinbase::SECURITY_ATTRIBUTES;
652-
use winapi::um::winbase::FILE_FLAG_DELETE_ON_CLOSE;
653-
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, GENERIC_READ};
647+
use windows_sys::Win32::Foundation::{CloseHandle, GENERIC_READ, INVALID_HANDLE_VALUE};
648+
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
649+
use windows_sys::Win32::Storage::FileSystem::{
650+
CreateFileW, FILE_FLAG_DELETE_ON_CLOSE, FILE_SHARE_DELETE, FILE_SHARE_READ, OPEN_EXISTING,
651+
};
654652

655653
// CARGO_HOME, hopefully empty except for bin/rustup.exe
656654
let cargo_home = utils::cargo_home()?;
@@ -671,8 +669,8 @@ pub(crate) fn delete_rustup_and_cargo_home() -> Result<()> {
671669
let gc_exe_win: Vec<_> = gc_exe.as_os_str().encode_wide().chain(Some(0)).collect();
672670

673671
// Make the sub-process opened by gc exe inherit its attribute.
674-
let mut sa = SECURITY_ATTRIBUTES {
675-
nLength: mem::size_of::<SECURITY_ATTRIBUTES>() as DWORD,
672+
let sa = SECURITY_ATTRIBUTES {
673+
nLength: mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
676674
lpSecurityDescriptor: ptr::null_mut(),
677675
bInheritHandle: 1,
678676
};
@@ -684,10 +682,10 @@ pub(crate) fn delete_rustup_and_cargo_home() -> Result<()> {
684682
gc_exe_win.as_ptr(),
685683
GENERIC_READ,
686684
FILE_SHARE_READ | FILE_SHARE_DELETE,
687-
&mut sa,
685+
&sa,
688686
OPEN_EXISTING,
689687
FILE_FLAG_DELETE_ON_CLOSE,
690-
ptr::null_mut(),
688+
0,
691689
);
692690

693691
if gc_handle == INVALID_HANDLE_VALUE {

src/command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ pub(crate) fn run_command_for_dir<S: AsRef<OsStr> + Debug>(
3636

3737
#[cfg(windows)]
3838
fn exec(cmd: &mut Command) -> io::Result<ExitCode> {
39-
use winapi::shared::minwindef::{BOOL, DWORD, FALSE, TRUE};
40-
use winapi::um::consoleapi::SetConsoleCtrlHandler;
39+
use windows_sys::Win32::Foundation::{BOOL, FALSE, TRUE};
40+
use windows_sys::Win32::System::Console::SetConsoleCtrlHandler;
4141

42-
unsafe extern "system" fn ctrlc_handler(_: DWORD) -> BOOL {
42+
unsafe extern "system" fn ctrlc_handler(_: u32) -> BOOL {
4343
// Do nothing. Let the child process handle it.
4444
TRUE
4545
}

src/dist/dist.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ impl TargetTriple {
304304
/// it is only available on Windows 10 1511+, so we use `GetProcAddress`
305305
/// to maintain backward compatibility with older Windows versions.
306306
fn arch_primary() -> Option<&'static str> {
307-
use winapi::shared::minwindef::BOOL;
308-
use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress};
309-
use winapi::um::processthreadsapi::GetCurrentProcess;
310-
use winapi::um::winnt::HANDLE;
307+
use windows_sys::core::s;
308+
use windows_sys::Win32::Foundation::{BOOL, HANDLE};
309+
use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
310+
use windows_sys::Win32::System::Threading::GetCurrentProcess;
311311

312312
const IMAGE_FILE_MACHINE_ARM64: u16 = 0xAA64;
313313
const IMAGE_FILE_MACHINE_AMD64: u16 = 0x8664;
@@ -320,16 +320,11 @@ impl TargetTriple {
320320
*mut u16,
321321
)
322322
-> BOOL = unsafe {
323-
let module = GetModuleHandleA(b"kernel32.dll\0" as *const u8 as *const i8);
324-
if module.is_null() {
323+
let module = GetModuleHandleA(s!("kernel32.dll"));
324+
if module == 0 {
325325
return None;
326326
}
327-
let process =
328-
GetProcAddress(module, b"IsWow64Process2\0" as *const u8 as *const i8);
329-
if process.is_null() {
330-
return None;
331-
}
332-
mem::transmute(process)
327+
mem::transmute(GetProcAddress(module, s!("IsWow64Process2"))?)
333328
};
334329

335330
let mut _machine = 0;
@@ -352,7 +347,7 @@ impl TargetTriple {
352347
/// Get the host architecture using `GetNativeSystemInfo`.
353348
/// Does not support detecting aarch64.
354349
fn arch_fallback() -> Option<&'static str> {
355-
use winapi::um::sysinfoapi::GetNativeSystemInfo;
350+
use windows_sys::Win32::System::SystemInformation::GetNativeSystemInfo;
356351

357352
const PROCESSOR_ARCHITECTURE_AMD64: u16 = 9;
358353
const PROCESSOR_ARCHITECTURE_INTEL: u16 = 0;
@@ -363,7 +358,7 @@ impl TargetTriple {
363358
GetNativeSystemInfo(&mut sys_info);
364359
}
365360

366-
match unsafe { sys_info.u.s() }.wProcessorArchitecture {
361+
match unsafe { sys_info.Anonymous.Anonymous }.wProcessorArchitecture {
367362
PROCESSOR_ARCHITECTURE_AMD64 => Some("x86_64"),
368363
PROCESSOR_ARCHITECTURE_INTEL => Some("i686"),
369364
_ => None,

src/utils/raw.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,24 @@ pub fn symlink_dir(src: &Path, dest: &Path) -> io::Result<()> {
156156
fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> {
157157
use std::os::windows::ffi::OsStrExt;
158158
use std::ptr;
159-
use winapi::shared::minwindef::*;
160-
use winapi::um::fileapi::*;
161-
use winapi::um::ioapiset::*;
162-
use winapi::um::winbase::*;
163-
use winapi::um::winioctl::FSCTL_SET_REPARSE_POINT;
164-
use winapi::um::winnt::*;
159+
use windows_sys::Win32::Foundation::*;
160+
use windows_sys::Win32::Storage::FileSystem::*;
161+
use windows_sys::Win32::System::Ioctl::FSCTL_SET_REPARSE_POINT;
162+
use windows_sys::Win32::System::SystemServices::*;
163+
use windows_sys::Win32::System::IO::*;
165164

166165
const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
167166

168167
#[repr(C)]
169168
#[allow(non_snake_case)]
170169
struct REPARSE_MOUNTPOINT_DATA_BUFFER {
171-
ReparseTag: DWORD,
172-
ReparseDataLength: DWORD,
173-
Reserved: WORD,
174-
ReparseTargetLength: WORD,
175-
ReparseTargetMaximumLength: WORD,
176-
Reserved1: WORD,
177-
ReparseTarget: WCHAR,
170+
ReparseTag: u32,
171+
ReparseDataLength: u32,
172+
Reserved: u16,
173+
ReparseTargetLength: u16,
174+
ReparseTargetMaximumLength: u16,
175+
Reserved1: u16,
176+
ReparseTarget: u16,
178177
}
179178

180179
// We're using low-level APIs to create the junction, and these are more picky about paths.
@@ -194,12 +193,12 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> {
194193
ptr::null_mut(),
195194
OPEN_EXISTING,
196195
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
197-
ptr::null_mut(),
196+
0,
198197
);
199198

200199
let mut data = [0u8; MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
201200
let db = data.as_mut_ptr().cast::<REPARSE_MOUNTPOINT_DATA_BUFFER>();
202-
let buf = &mut (*db).ReparseTarget as *mut WCHAR;
201+
let buf = &mut (*db).ReparseTarget as *mut u16;
203202
let mut i = 0;
204203
// FIXME: this conversion is very hacky
205204
let v = br"\??\";
@@ -211,13 +210,13 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> {
211210
*buf.offset(i) = 0;
212211
i += 1;
213212
(*db).ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
214-
(*db).ReparseTargetMaximumLength = (i * 2) as WORD;
215-
(*db).ReparseTargetLength = ((i - 1) * 2) as WORD;
216-
(*db).ReparseDataLength = (*db).ReparseTargetLength as DWORD + 12;
213+
(*db).ReparseTargetMaximumLength = (i * 2) as u16;
214+
(*db).ReparseTargetLength = ((i - 1) * 2) as u16;
215+
(*db).ReparseDataLength = (*db).ReparseTargetLength as u32 + 12;
217216

218217
let mut ret = 0;
219218
let res = DeviceIoControl(
220-
h.cast(),
219+
h,
221220
FSCTL_SET_REPARSE_POINT,
222221
data.as_mut_ptr().cast(),
223222
(*db).ReparseDataLength + 8,

0 commit comments

Comments
 (0)