diff --git a/Cargo.toml b/Cargo.toml index b28e425dcc4..9b96a75339c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/crates/cargo-test-support/Cargo.toml b/crates/cargo-test-support/Cargo.toml index 653c5db4d61..89eb94a5ef7 100644 --- a/crates/cargo-test-support/Cargo.toml +++ b/crates/cargo-test-support/Cargo.toml @@ -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 = [] diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs index 6925597a1da..ef1fddb7003 100644 --- a/crates/cargo-test-support/src/paths.rs +++ b/crates/cargo-test-support/src/paths.rs @@ -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(); diff --git a/crates/cargo-util/Cargo.toml b/crates/cargo-util/Cargo.toml index ff47587b3c7..7c9d89df12a 100644 --- a/crates/cargo-util/Cargo.toml +++ b/crates/cargo-util/Cargo.toml @@ -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"] } diff --git a/crates/cargo-util/src/paths.rs b/crates/cargo-util/src/paths.rs index d0ac2a79a08..69df7a2096c 100644 --- a/crates/cargo-util/src/paths.rs +++ b/crates/cargo-util/src/paths.rs @@ -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 = path.as_os_str().encode_wide().chain(once(0)).collect(); unsafe { diff --git a/crates/cargo-util/src/process_builder.rs b/crates/cargo-util/src/process_builder.rs index be8a5fca3b3..76392f2564b 100644 --- a/crates/cargo-util/src/process_builder.rs +++ b/crates/cargo-util/src/process_builder.rs @@ -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 } @@ -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) } } diff --git a/crates/cargo-util/src/process_error.rs b/crates/cargo-util/src/process_error.rs index e8607d6614d..9b4a38cb5e3 100644 --- a/crates/cargo-util/src/process_error.rs +++ b/crates/cargo-util/src/process_error.rs @@ -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", diff --git a/crates/cargo-util/src/read2.rs b/crates/cargo-util/src/read2.rs index 53322a51d70..742dc1daa60 100644 --- a/crates/cargo-util/src/read2.rs +++ b/crates/cargo-util/src/read2.rs @@ -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, diff --git a/crates/credential/cargo-credential-wincred/Cargo.toml b/crates/credential/cargo-credential-wincred/Cargo.toml index b612c26fe7a..83c38e80dbb 100644 --- a/crates/credential/cargo-credential-wincred/Cargo.toml +++ b/crates/credential/cargo-credential-wincred/Cargo.toml @@ -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"] } diff --git a/crates/credential/cargo-credential-wincred/src/main.rs b/crates/credential/cargo-credential-wincred/src/main.rs index 8661d28b80b..8ae48f34899 100644 --- a/crates/credential/cargo-credential-wincred/src/main.rs +++ b/crates/credential/cargo-credential-wincred/src/main.rs @@ -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; @@ -31,13 +38,13 @@ impl Credential for WindowsCredential { fn get(&self, index_url: &str) -> Result { 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( @@ -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()); @@ -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(()); } diff --git a/crates/home/Cargo.toml b/crates/home/Cargo.toml index 052b6193215..18459dfef1b 100644 --- a/crates/home/Cargo.toml +++ b/crates/home/Cargo.toml @@ -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"] } diff --git a/crates/home/src/lib.rs b/crates/home/src/lib.rs index ad254ca3bb1..306026e4010 100644 --- a/crates/home/src/lib.rs +++ b/crates/home/src/lib.rs @@ -30,7 +30,7 @@ pub mod env; -#[cfg(windows)] +#[cfg(target_os = "windows")] mod windows; use std::io; diff --git a/crates/home/src/windows.rs b/crates/home/src/windows.rs index 4e0cd8ec513..a35dc9c5719 100644 --- a/crates/home/src/windows.rs +++ b/crates/home/src/windows.rs @@ -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 { env::var_os("USERPROFILE") @@ -18,14 +16,8 @@ pub fn home_dir_inner() -> Option { #[cfg(not(target_vendor = "uwp"))] fn home_dir_crt() -> Option { unsafe { - let mut path: Vec = Vec::with_capacity(MAX_PATH); - match SHGetFolderPathW( - ptr::null_mut(), - CSIDL_PROFILE, - ptr::null_mut(), - 0, - path.as_mut_ptr(), - ) { + let mut path: Vec = 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); diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index d1f413adb8f..79f09e6f9ce 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -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}; @@ -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; diff --git a/src/cargo/util/cpu.rs b/src/cargo/util/cpu.rs index 3fe50d372da..6ebb01b8ea1 100644 --- a/src/cargo/util/cpu.rs +++ b/src/cargo/util/cpu.rs @@ -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, diff --git a/src/cargo/util/flock.rs b/src/cargo/util/flock.rs index 0cdd9434495..295eb1e140b 100644 --- a/src/cargo/util/flock.rs +++ b/src/cargo/util/flock.rs @@ -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) @@ -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 { @@ -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 { diff --git a/src/cargo/util/job.rs b/src/cargo/util/job.rs index 7a9cd1bcaed..9ff9b53042d 100644 --- a/src/cargo/util/job.rs +++ b/src/cargo/util/job.rs @@ -44,15 +44,20 @@ mod imp { use std::io; use std::mem; use std::ptr; + use std::ptr::addr_of; use log::info; - use winapi::shared::minwindef::*; - use winapi::um::handleapi::*; - use winapi::um::jobapi2::*; - use winapi::um::processthreadsapi::*; - use winapi::um::winnt::HANDLE; - use winapi::um::winnt::*; + use windows_sys::Win32::Foundation::CloseHandle; + use windows_sys::Win32::Foundation::HANDLE; + use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; + use windows_sys::Win32::System::JobObjects::AssignProcessToJobObject; + use windows_sys::Win32::System::JobObjects::CreateJobObjectW; + use windows_sys::Win32::System::JobObjects::JobObjectExtendedLimitInformation; + use windows_sys::Win32::System::JobObjects::SetInformationJobObject; + use windows_sys::Win32::System::JobObjects::JOBOBJECT_EXTENDED_LIMIT_INFORMATION; + use windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + use windows_sys::Win32::System::Threading::GetCurrentProcess; pub struct Setup { job: Handle, @@ -77,7 +82,7 @@ mod imp { // we're otherwise part of someone else's job object in this case. let job = CreateJobObjectW(ptr::null_mut(), ptr::null()); - if job.is_null() { + if job == INVALID_HANDLE_VALUE { return None; } let job = Handle { inner: job }; @@ -92,8 +97,8 @@ mod imp { let r = SetInformationJobObject( job.inner, JobObjectExtendedLimitInformation, - &mut info as *mut _ as LPVOID, - mem::size_of_val(&info) as DWORD, + addr_of!(info) as *const _, + mem::size_of_val(&info) as u32, ); if r == 0 { return None; @@ -116,13 +121,13 @@ mod imp { // processes. The destructor here configures our job object to // **not** kill everything on close, then closes the job object. unsafe { - let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION; + let info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION; info = mem::zeroed(); let r = SetInformationJobObject( self.job.inner, JobObjectExtendedLimitInformation, - &mut info as *mut _ as LPVOID, - mem::size_of_val(&info) as DWORD, + addr_of!(info) as *const _, + mem::size_of_val(&info) as u32, ); if r == 0 { info!("failed to configure job object to defaults: {}", last_err());