Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace winapi with windows-sys crate. #11656

Merged
merged 8 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,16 @@ rustc-workspace-hack = "1.0.0"
[target.'cfg(windows)'.dependencies]
fwdansi = "1.1.0"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.45"
features = [
"basetsd",
"handleapi",
"jobapi",
"jobapi2",
"memoryapi",
"minwindef",
"ntdef",
"ntstatus",
"processenv",
"processthreadsapi",
"psapi",
"synchapi",
"winerror",
"winbase",
"wincon",
"winnt",
"Win32_Foundation",
"Win32_Storage_FileSystem",
"Win32_System_IO",
"Win32_System_Threading",
"Win32_System_JobObjects",
"Win32_Security",
"Win32_System_SystemServices"
]

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ toml_edit = { version = "0.15.0", features = ["serde", "easy", "perf"] }
url = "2.2.2"

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

[features]
deny-warnings = []
2 changes: 1 addition & 1 deletion crates/cargo-test-support/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ pub fn windows_reserved_names_are_allowed() -> bool {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
use winapi::um::fileapi::GetFullPathNameW;
use windows_sys::Win32::Storage::FileSystem::GetFullPathNameW;

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

Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }

[target.'cfg(windows)'.dependencies]
miow = "0.5.0"
winapi = { version = "0.3.9", features = ["consoleapi", "minwindef"] }
windows-sys = { version = "0.45.0", features = ["Win32_Storage_FileSystem", "Win32_Foundation", "Win32_System_Console"] }
5 changes: 3 additions & 2 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,9 @@ fn exclude_from_content_indexing(path: &Path) {
{
use std::iter::once;
use std::os::windows::prelude::OsStrExt;
use winapi::um::fileapi::{GetFileAttributesW, SetFileAttributesW};
use winapi::um::winnt::FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
use windows_sys::Win32::Storage::FileSystem::{
GetFileAttributesW, SetFileAttributesW, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
};

let path: Vec<u16> = path.as_os_str().encode_wide().chain(once(0)).collect();
unsafe {
Expand Down
8 changes: 4 additions & 4 deletions crates/cargo-util/src/process_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,10 @@ mod imp {
use super::{ProcessBuilder, ProcessError};
use anyhow::Result;
use std::io;
use winapi::shared::minwindef::{BOOL, DWORD, FALSE, TRUE};
use winapi::um::consoleapi::SetConsoleCtrlHandler;
use windows_sys::Win32::Foundation::{BOOL, FALSE, TRUE};
use windows_sys::Win32::System::Console::SetConsoleCtrlHandler;

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

pub fn command_line_too_big(err: &io::Error) -> bool {
use winapi::shared::winerror::ERROR_FILENAME_EXCED_RANGE;
use windows_sys::Win32::Foundation::ERROR_FILENAME_EXCED_RANGE;
err.raw_os_error() == Some(ERROR_FILENAME_EXCED_RANGE as i32)
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/cargo-util/src/process_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ pub fn exit_status_to_string(status: ExitStatus) -> String {

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

let mut base = status.to_string();
let extra = match status.code().unwrap() as DWORD {
let extra = match status.code().unwrap() as i32 {
STATUS_ACCESS_VIOLATION => "STATUS_ACCESS_VIOLATION",
STATUS_IN_PAGE_ERROR => "STATUS_IN_PAGE_ERROR",
STATUS_INVALID_HANDLE => "STATUS_INVALID_HANDLE",
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util/src/read2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod imp {
use miow::iocp::{CompletionPort, CompletionStatus};
use miow::pipe::NamedPipe;
use miow::Overlapped;
use winapi::shared::winerror::ERROR_BROKEN_PIPE;
use windows_sys::Win32::Foundation::ERROR_BROKEN_PIPE;

struct Pipe<'a> {
dst: &'a mut Vec<u8>,
Expand Down
2 changes: 1 addition & 1 deletion crates/credential/cargo-credential-wincred/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ description = "A Cargo credential process that stores tokens with Windows Creden

[dependencies]
cargo-credential = { version = "0.2.0", path = "../cargo-credential" }
winapi = { version = "0.3.9", features = ["wincred", "winerror", "impl-default"] }
windows-sys = { version = "0.45", features = ["Win32_Foundation", "Win32_Security_Credentials"] }
49 changes: 29 additions & 20 deletions crates/credential/cargo-credential-wincred/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
use cargo_credential::{Credential, Error};
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use winapi::shared::minwindef::{DWORD, FILETIME, LPBYTE, TRUE};
use winapi::shared::winerror;
use winapi::um::wincred;
use winapi::um::winnt::LPWSTR;

use windows_sys::core::PWSTR;
use windows_sys::Win32::Foundation::ERROR_NOT_FOUND;
use windows_sys::Win32::Foundation::FILETIME;
use windows_sys::Win32::Foundation::TRUE;
use windows_sys::Win32::Security::Credentials::CredDeleteW;
use windows_sys::Win32::Security::Credentials::CredReadW;
use windows_sys::Win32::Security::Credentials::CredWriteW;
use windows_sys::Win32::Security::Credentials::CREDENTIALW;
use windows_sys::Win32::Security::Credentials::CRED_PERSIST_LOCAL_MACHINE;
use windows_sys::Win32::Security::Credentials::CRED_TYPE_GENERIC;

struct WindowsCredential;

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

fn get(&self, index_url: &str) -> Result<String, Error> {
let target_name = target_name(index_url);
let mut p_credential: wincred::PCREDENTIALW = std::ptr::null_mut();
let p_credential: *mut CREDENTIALW = std::ptr::null_mut() as *mut _;
unsafe {
if wincred::CredReadW(
if CredReadW(
target_name.as_ptr(),
wincred::CRED_TYPE_GENERIC,
CRED_TYPE_GENERIC,
0,
&mut p_credential,
p_credential as *mut _ as *mut _,
) != TRUE
{
return Err(
Expand All @@ -59,21 +66,24 @@ impl Credential for WindowsCredential {
Some(name) => wstr(&format!("Cargo registry token for {}", name)),
None => wstr("Cargo registry token"),
};
let mut credential = wincred::CREDENTIALW {
let mut credential = CREDENTIALW {
Flags: 0,
Type: wincred::CRED_TYPE_GENERIC,
TargetName: target_name.as_ptr() as LPWSTR,
Comment: comment.as_ptr() as LPWSTR,
LastWritten: FILETIME::default(),
CredentialBlobSize: token.len() as DWORD,
CredentialBlob: token.as_ptr() as LPBYTE,
Persist: wincred::CRED_PERSIST_LOCAL_MACHINE,
Type: CRED_TYPE_GENERIC,
TargetName: target_name.as_ptr() as PWSTR,
Comment: comment.as_ptr() as PWSTR,
LastWritten: FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
},
CredentialBlobSize: token.len() as u32,
CredentialBlob: token.as_ptr() as *mut u8,
Persist: CRED_PERSIST_LOCAL_MACHINE,
AttributeCount: 0,
Attributes: std::ptr::null_mut(),
TargetAlias: std::ptr::null_mut(),
UserName: std::ptr::null_mut(),
};
let result = unsafe { wincred::CredWriteW(&mut credential, 0) };
let result = unsafe { CredWriteW(&mut credential, 0) };
if result != TRUE {
let err = std::io::Error::last_os_error();
return Err(format!("failed to store token: {}", err).into());
Expand All @@ -83,11 +93,10 @@ impl Credential for WindowsCredential {

fn erase(&self, index_url: &str) -> Result<(), Error> {
let target_name = target_name(index_url);
let result =
unsafe { wincred::CredDeleteW(target_name.as_ptr(), wincred::CRED_TYPE_GENERIC, 0) };
let result = unsafe { CredDeleteW(target_name.as_ptr(), CRED_TYPE_GENERIC, 0) };
if result != TRUE {
let err = std::io::Error::last_os_error();
if err.raw_os_error() == Some(winerror::ERROR_NOT_FOUND as i32) {
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
eprintln!("not currently logged in to `{}`", index_url);
return Ok(());
}
Expand Down
9 changes: 2 additions & 7 deletions crates/home/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ readme = "README.md"
repository = "https://github.com/brson/home"
description = "Shared definitions of home directories"

[target."cfg(windows)".dependencies.winapi]
version = "0.3"
features = [
"shlobj",
"std",
"winerror",
]
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.45.0", features = ["Win32_Foundation", "Win32_UI_Shell"] }
2 changes: 1 addition & 1 deletion crates/home/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

pub mod env;

#[cfg(windows)]
#[cfg(target_os = "windows")]
mod windows;

use std::io;
Expand Down
16 changes: 4 additions & 12 deletions crates/home/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use std::env;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::path::PathBuf;
use std::ptr;

use winapi::shared::minwindef::MAX_PATH;
use winapi::shared::winerror::S_OK;
use winapi::um::shlobj::{SHGetFolderPathW, CSIDL_PROFILE};
use windows_sys::Win32::Foundation::{MAX_PATH, S_OK};
use windows_sys::Win32::UI::Shell::{SHGetFolderPathW, CSIDL_PROFILE};

pub fn home_dir_inner() -> Option<PathBuf> {
env::var_os("USERPROFILE")
Expand All @@ -18,14 +16,8 @@ pub fn home_dir_inner() -> Option<PathBuf> {
#[cfg(not(target_vendor = "uwp"))]
fn home_dir_crt() -> Option<PathBuf> {
unsafe {
let mut path: Vec<u16> = Vec::with_capacity(MAX_PATH);
match SHGetFolderPathW(
ptr::null_mut(),
CSIDL_PROFILE,
ptr::null_mut(),
0,
path.as_mut_ptr(),
) {
let mut path: Vec<u16> = Vec::with_capacity(MAX_PATH as usize);
match SHGetFolderPathW(0, CSIDL_PROFILE as i32, 0, 0, path.as_mut_ptr()) {
S_OK => {
let len = wcslen(path.as_ptr());
path.set_len(len);
Expand Down
21 changes: 13 additions & 8 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,17 @@ mod imp {
#[cfg(windows)]
mod imp {
use std::{cmp, mem, ptr};
use winapi::um::fileapi::*;
use winapi::um::handleapi::*;
use winapi::um::processenv::*;
use winapi::um::winbase::*;
use winapi::um::wincon::*;
use winapi::um::winnt::*;

use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::Storage::FileSystem::{
CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
};
use windows_sys::Win32::System::Console::{
GetConsoleScreenBufferInfo, GetStdHandle, CONSOLE_SCREEN_BUFFER_INFO, STD_ERROR_HANDLE,
};
use windows_sys::Win32::System::SystemServices::{GENERIC_READ, GENERIC_WRITE};

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

Expand All @@ -578,13 +583,13 @@ mod imp {
// INVALID_HANDLE_VALUE. Use an alternate method which works
// in that case as well.
let h = CreateFileA(
"CONOUT$\0".as_ptr() as *const CHAR,
"CONOUT$\0".as_ptr() as PCSTR,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ptr::null_mut(),
OPEN_EXISTING,
0,
ptr::null_mut(),
0,
);
if h == INVALID_HANDLE_VALUE {
return TtyWidth::NoTty;
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/util/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ mod imp {
mod imp {
use std::io;
use std::mem;
use winapi::shared::minwindef::*;
use winapi::um::processthreadsapi::*;

use windows_sys::Win32::Foundation::FILETIME;
use windows_sys::Win32::System::Threading::GetSystemTimes;

pub struct State {
idle: FILETIME,
Expand Down
22 changes: 15 additions & 7 deletions src/cargo/util/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,11 @@ mod sys {
use std::mem;
use std::os::windows::io::AsRawHandle;

use winapi::shared::minwindef::DWORD;
use winapi::shared::winerror::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION};
use winapi::um::fileapi::{LockFileEx, UnlockFile};
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY};
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Foundation::{ERROR_INVALID_FUNCTION, ERROR_LOCK_VIOLATION};
use windows_sys::Win32::Storage::FileSystem::{
LockFileEx, UnlockFile, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY,
};

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

pub(super) fn unlock(file: &File) -> Result<()> {
unsafe {
let ret = UnlockFile(file.as_raw_handle(), 0, 0, !0, !0);
let ret = UnlockFile(file.as_raw_handle() as HANDLE, 0, 0, !0, !0);
if ret == 0 {
Err(Error::last_os_error())
} else {
Expand All @@ -479,10 +480,17 @@ mod sys {
}
}

fn lock_file(file: &File, flags: DWORD) -> Result<()> {
fn lock_file(file: &File, flags: u32) -> Result<()> {
unsafe {
let mut overlapped = mem::zeroed();
let ret = LockFileEx(file.as_raw_handle(), flags, 0, !0, !0, &mut overlapped);
let ret = LockFileEx(
file.as_raw_handle() as HANDLE,
flags,
0,
!0,
!0,
&mut overlapped,
);
if ret == 0 {
Err(Error::last_os_error())
} else {
Expand Down
Loading