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
}

0 commit comments

Comments
 (0)