Skip to content

Commit f9c267b

Browse files
committed
Auto merge of #11656 - attila-lin:dev/windows, r=epage
Replace `winapi` with `windows-sys` crate. ### What does this PR try to resolve? replace `winapi` with `windows-sys` crate. It's officially maintained. ### How should we test and review this PR? I just do the replacement of API. I think it is quite clear. And I found a `cfg` for `windows` may miss checked error. ### Additional information No one.
2 parents d6a734e + a199718 commit f9c267b

File tree

17 files changed

+107
-101
lines changed

17 files changed

+107
-101
lines changed

Cargo.toml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,16 @@ rustc-workspace-hack = "1.0.0"
8484
[target.'cfg(windows)'.dependencies]
8585
fwdansi = "1.1.0"
8686

87-
[target.'cfg(windows)'.dependencies.winapi]
88-
version = "0.3"
87+
[target.'cfg(windows)'.dependencies.windows-sys]
88+
version = "0.45"
8989
features = [
90-
"basetsd",
91-
"handleapi",
92-
"jobapi",
93-
"jobapi2",
94-
"memoryapi",
95-
"minwindef",
96-
"ntdef",
97-
"ntstatus",
98-
"processenv",
99-
"processthreadsapi",
100-
"psapi",
101-
"synchapi",
102-
"winerror",
103-
"winbase",
104-
"wincon",
105-
"winnt",
90+
"Win32_Foundation",
91+
"Win32_Storage_FileSystem",
92+
"Win32_System_IO",
93+
"Win32_System_Threading",
94+
"Win32_System_JobObjects",
95+
"Win32_Security",
96+
"Win32_System_SystemServices"
10697
]
10798

10899
[dev-dependencies]

crates/cargo-test-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
2929
url = "2.2.2"
3030

3131
[target.'cfg(windows)'.dependencies]
32-
winapi = "0.3"
32+
windows-sys = { version = "0.45.0", features = ["Win32_Storage_FileSystem"] }
3333

3434
[features]
3535
deny-warnings = []

crates/cargo-test-support/src/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ pub fn windows_reserved_names_are_allowed() -> bool {
305305
use std::ffi::OsStr;
306306
use std::os::windows::ffi::OsStrExt;
307307
use std::ptr;
308-
use winapi::um::fileapi::GetFullPathNameW;
308+
use windows_sys::Win32::Storage::FileSystem::GetFullPathNameW;
309309

310310
let test_file_name: Vec<_> = OsStr::new("aux.rs").encode_wide().collect();
311311

crates/cargo-util/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }
2525

2626
[target.'cfg(windows)'.dependencies]
2727
miow = "0.5.0"
28-
winapi = { version = "0.3.9", features = ["consoleapi", "minwindef"] }
28+
windows-sys = { version = "0.45.0", features = ["Win32_Storage_FileSystem", "Win32_Foundation", "Win32_System_Console"] }

crates/cargo-util/src/paths.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,9 @@ fn exclude_from_content_indexing(path: &Path) {
701701
{
702702
use std::iter::once;
703703
use std::os::windows::prelude::OsStrExt;
704-
use winapi::um::fileapi::{GetFileAttributesW, SetFileAttributesW};
705-
use winapi::um::winnt::FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
704+
use windows_sys::Win32::Storage::FileSystem::{
705+
GetFileAttributesW, SetFileAttributesW, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
706+
};
706707

707708
let path: Vec<u16> = path.as_os_str().encode_wide().chain(once(0)).collect();
708709
unsafe {

crates/cargo-util/src/process_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ mod imp {
606606
use super::{ProcessBuilder, ProcessError};
607607
use anyhow::Result;
608608
use std::io;
609-
use winapi::shared::minwindef::{BOOL, DWORD, FALSE, TRUE};
610-
use winapi::um::consoleapi::SetConsoleCtrlHandler;
609+
use windows_sys::Win32::Foundation::{BOOL, FALSE, TRUE};
610+
use windows_sys::Win32::System::Console::SetConsoleCtrlHandler;
611611

612-
unsafe extern "system" fn ctrlc_handler(_: DWORD) -> BOOL {
612+
unsafe extern "system" fn ctrlc_handler(_: u32) -> BOOL {
613613
// Do nothing; let the child process handle it.
614614
TRUE
615615
}
@@ -626,7 +626,7 @@ mod imp {
626626
}
627627

628628
pub fn command_line_too_big(err: &io::Error) -> bool {
629-
use winapi::shared::winerror::ERROR_FILENAME_EXCED_RANGE;
629+
use windows_sys::Win32::Foundation::ERROR_FILENAME_EXCED_RANGE;
630630
err.raw_os_error() == Some(ERROR_FILENAME_EXCED_RANGE as i32)
631631
}
632632
}

crates/cargo-util/src/process_error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ pub fn exit_status_to_string(status: ExitStatus) -> String {
140140

141141
#[cfg(windows)]
142142
fn status_to_string(status: ExitStatus) -> String {
143-
use winapi::shared::minwindef::DWORD;
144-
use winapi::um::winnt::*;
143+
use windows_sys::Win32::Foundation::*;
145144

146145
let mut base = status.to_string();
147-
let extra = match status.code().unwrap() as DWORD {
146+
let extra = match status.code().unwrap() as i32 {
148147
STATUS_ACCESS_VIOLATION => "STATUS_ACCESS_VIOLATION",
149148
STATUS_IN_PAGE_ERROR => "STATUS_IN_PAGE_ERROR",
150149
STATUS_INVALID_HANDLE => "STATUS_INVALID_HANDLE",

crates/cargo-util/src/read2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod imp {
8484
use miow::iocp::{CompletionPort, CompletionStatus};
8585
use miow::pipe::NamedPipe;
8686
use miow::Overlapped;
87-
use winapi::shared::winerror::ERROR_BROKEN_PIPE;
87+
use windows_sys::Win32::Foundation::ERROR_BROKEN_PIPE;
8888

8989
struct Pipe<'a> {
9090
dst: &'a mut Vec<u8>,

crates/credential/cargo-credential-wincred/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ description = "A Cargo credential process that stores tokens with Windows Creden
88

99
[dependencies]
1010
cargo-credential = { version = "0.2.0", path = "../cargo-credential" }
11-
winapi = { version = "0.3.9", features = ["wincred", "winerror", "impl-default"] }
11+
windows-sys = { version = "0.45", features = ["Win32_Foundation", "Win32_Security_Credentials"] }

crates/credential/cargo-credential-wincred/src/main.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
use cargo_credential::{Credential, Error};
44
use std::ffi::OsStr;
55
use std::os::windows::ffi::OsStrExt;
6-
use winapi::shared::minwindef::{DWORD, FILETIME, LPBYTE, TRUE};
7-
use winapi::shared::winerror;
8-
use winapi::um::wincred;
9-
use winapi::um::winnt::LPWSTR;
6+
7+
use windows_sys::core::PWSTR;
8+
use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
9+
use windows_sys::Win32::Foundation::FILETIME;
10+
use windows_sys::Win32::Foundation::TRUE;
11+
use windows_sys::Win32::Security::Credentials::CredDeleteW;
12+
use windows_sys::Win32::Security::Credentials::CredReadW;
13+
use windows_sys::Win32::Security::Credentials::CredWriteW;
14+
use windows_sys::Win32::Security::Credentials::CREDENTIALW;
15+
use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
16+
use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;
1017

1118
struct WindowsCredential;
1219

@@ -31,13 +38,13 @@ impl Credential for WindowsCredential {
3138

3239
fn get(&self, index_url: &str) -> Result<String, Error> {
3340
let target_name = target_name(index_url);
34-
let mut p_credential: wincred::PCREDENTIALW = std::ptr::null_mut();
41+
let p_credential: *mut CREDENTIALW = std::ptr::null_mut() as *mut _;
3542
unsafe {
36-
if wincred::CredReadW(
43+
if CredReadW(
3744
target_name.as_ptr(),
38-
wincred::CRED_TYPE_GENERIC,
45+
CRED_TYPE_GENERIC,
3946
0,
40-
&mut p_credential,
47+
p_credential as *mut _ as *mut _,
4148
) != TRUE
4249
{
4350
return Err(
@@ -59,21 +66,24 @@ impl Credential for WindowsCredential {
5966
Some(name) => wstr(&format!("Cargo registry token for {}", name)),
6067
None => wstr("Cargo registry token"),
6168
};
62-
let mut credential = wincred::CREDENTIALW {
69+
let mut credential = CREDENTIALW {
6370
Flags: 0,
64-
Type: wincred::CRED_TYPE_GENERIC,
65-
TargetName: target_name.as_ptr() as LPWSTR,
66-
Comment: comment.as_ptr() as LPWSTR,
67-
LastWritten: FILETIME::default(),
68-
CredentialBlobSize: token.len() as DWORD,
69-
CredentialBlob: token.as_ptr() as LPBYTE,
70-
Persist: wincred::CRED_PERSIST_LOCAL_MACHINE,
71+
Type: CRED_TYPE_GENERIC,
72+
TargetName: target_name.as_ptr() as PWSTR,
73+
Comment: comment.as_ptr() as PWSTR,
74+
LastWritten: FILETIME {
75+
dwLowDateTime: 0,
76+
dwHighDateTime: 0,
77+
},
78+
CredentialBlobSize: token.len() as u32,
79+
CredentialBlob: token.as_ptr() as *mut u8,
80+
Persist: CRED_PERSIST_LOCAL_MACHINE,
7181
AttributeCount: 0,
7282
Attributes: std::ptr::null_mut(),
7383
TargetAlias: std::ptr::null_mut(),
7484
UserName: std::ptr::null_mut(),
7585
};
76-
let result = unsafe { wincred::CredWriteW(&mut credential, 0) };
86+
let result = unsafe { CredWriteW(&mut credential, 0) };
7787
if result != TRUE {
7888
let err = std::io::Error::last_os_error();
7989
return Err(format!("failed to store token: {}", err).into());
@@ -83,11 +93,10 @@ impl Credential for WindowsCredential {
8393

8494
fn erase(&self, index_url: &str) -> Result<(), Error> {
8595
let target_name = target_name(index_url);
86-
let result =
87-
unsafe { wincred::CredDeleteW(target_name.as_ptr(), wincred::CRED_TYPE_GENERIC, 0) };
96+
let result = unsafe { CredDeleteW(target_name.as_ptr(), CRED_TYPE_GENERIC, 0) };
8897
if result != TRUE {
8998
let err = std::io::Error::last_os_error();
90-
if err.raw_os_error() == Some(winerror::ERROR_NOT_FOUND as i32) {
99+
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
91100
eprintln!("not currently logged in to `{}`", index_url);
92101
return Ok(());
93102
}

crates/home/Cargo.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,5 @@ readme = "README.md"
1616
repository = "https://github.com/brson/home"
1717
description = "Shared definitions of home directories"
1818

19-
[target."cfg(windows)".dependencies.winapi]
20-
version = "0.3"
21-
features = [
22-
"shlobj",
23-
"std",
24-
"winerror",
25-
]
19+
[target.'cfg(windows)'.dependencies]
20+
windows-sys = { version = "0.45.0", features = ["Win32_Foundation", "Win32_UI_Shell"] }

crates/home/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
pub mod env;
3232

33-
#[cfg(windows)]
33+
#[cfg(target_os = "windows")]
3434
mod windows;
3535

3636
use std::io;

crates/home/src/windows.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use std::env;
22
use std::ffi::OsString;
33
use std::os::windows::ffi::OsStringExt;
44
use std::path::PathBuf;
5-
use std::ptr;
65

7-
use winapi::shared::minwindef::MAX_PATH;
8-
use winapi::shared::winerror::S_OK;
9-
use winapi::um::shlobj::{SHGetFolderPathW, CSIDL_PROFILE};
6+
use windows_sys::Win32::Foundation::{MAX_PATH, S_OK};
7+
use windows_sys::Win32::UI::Shell::{SHGetFolderPathW, CSIDL_PROFILE};
108

119
pub fn home_dir_inner() -> Option<PathBuf> {
1210
env::var_os("USERPROFILE")
@@ -18,14 +16,8 @@ pub fn home_dir_inner() -> Option<PathBuf> {
1816
#[cfg(not(target_vendor = "uwp"))]
1917
fn home_dir_crt() -> Option<PathBuf> {
2018
unsafe {
21-
let mut path: Vec<u16> = Vec::with_capacity(MAX_PATH);
22-
match SHGetFolderPathW(
23-
ptr::null_mut(),
24-
CSIDL_PROFILE,
25-
ptr::null_mut(),
26-
0,
27-
path.as_mut_ptr(),
28-
) {
19+
let mut path: Vec<u16> = Vec::with_capacity(MAX_PATH as usize);
20+
match SHGetFolderPathW(0, CSIDL_PROFILE as i32, 0, 0, path.as_mut_ptr()) {
2921
S_OK => {
3022
let len = wcslen(path.as_ptr());
3123
path.set_len(len);

src/cargo/core/shell.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,17 @@ mod imp {
557557
#[cfg(windows)]
558558
mod imp {
559559
use std::{cmp, mem, ptr};
560-
use winapi::um::fileapi::*;
561-
use winapi::um::handleapi::*;
562-
use winapi::um::processenv::*;
563-
use winapi::um::winbase::*;
564-
use winapi::um::wincon::*;
565-
use winapi::um::winnt::*;
560+
561+
use windows_sys::core::PCSTR;
562+
use windows_sys::Win32::Foundation::CloseHandle;
563+
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
564+
use windows_sys::Win32::Storage::FileSystem::{
565+
CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
566+
};
567+
use windows_sys::Win32::System::Console::{
568+
GetConsoleScreenBufferInfo, GetStdHandle, CONSOLE_SCREEN_BUFFER_INFO, STD_ERROR_HANDLE,
569+
};
570+
use windows_sys::Win32::System::SystemServices::{GENERIC_READ, GENERIC_WRITE};
566571

567572
pub(super) use super::{default_err_erase_line as err_erase_line, TtyWidth};
568573

@@ -578,13 +583,13 @@ mod imp {
578583
// INVALID_HANDLE_VALUE. Use an alternate method which works
579584
// in that case as well.
580585
let h = CreateFileA(
581-
"CONOUT$\0".as_ptr() as *const CHAR,
586+
"CONOUT$\0".as_ptr() as PCSTR,
582587
GENERIC_READ | GENERIC_WRITE,
583588
FILE_SHARE_READ | FILE_SHARE_WRITE,
584589
ptr::null_mut(),
585590
OPEN_EXISTING,
586591
0,
587-
ptr::null_mut(),
592+
0,
588593
);
589594
if h == INVALID_HANDLE_VALUE {
590595
return TtyWidth::NoTty;

src/cargo/util/cpu.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ mod imp {
190190
mod imp {
191191
use std::io;
192192
use std::mem;
193-
use winapi::shared::minwindef::*;
194-
use winapi::um::processthreadsapi::*;
193+
194+
use windows_sys::Win32::Foundation::FILETIME;
195+
use windows_sys::Win32::System::Threading::GetSystemTimes;
195196

196197
pub struct State {
197198
idle: FILETIME,

src/cargo/util/flock.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,11 @@ mod sys {
437437
use std::mem;
438438
use std::os::windows::io::AsRawHandle;
439439

440-
use winapi::shared::minwindef::DWORD;
441-
use winapi::shared::winerror::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION};
442-
use winapi::um::fileapi::{LockFileEx, UnlockFile};
443-
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY};
440+
use windows_sys::Win32::Foundation::HANDLE;
441+
use windows_sys::Win32::Foundation::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION};
442+
use windows_sys::Win32::Storage::FileSystem::{
443+
LockFileEx, UnlockFile, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY,
444+
};
444445

445446
pub(super) fn lock_shared(file: &File) -> Result<()> {
446447
lock_file(file, 0)
@@ -470,7 +471,7 @@ mod sys {
470471

471472
pub(super) fn unlock(file: &File) -> Result<()> {
472473
unsafe {
473-
let ret = UnlockFile(file.as_raw_handle(), 0, 0, !0, !0);
474+
let ret = UnlockFile(file.as_raw_handle() as HANDLE, 0, 0, !0, !0);
474475
if ret == 0 {
475476
Err(Error::last_os_error())
476477
} else {
@@ -479,10 +480,17 @@ mod sys {
479480
}
480481
}
481482

482-
fn lock_file(file: &File, flags: DWORD) -> Result<()> {
483+
fn lock_file(file: &File, flags: u32) -> Result<()> {
483484
unsafe {
484485
let mut overlapped = mem::zeroed();
485-
let ret = LockFileEx(file.as_raw_handle(), flags, 0, !0, !0, &mut overlapped);
486+
let ret = LockFileEx(
487+
file.as_raw_handle() as HANDLE,
488+
flags,
489+
0,
490+
!0,
491+
!0,
492+
&mut overlapped,
493+
);
486494
if ret == 0 {
487495
Err(Error::last_os_error())
488496
} else {

0 commit comments

Comments
 (0)