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

Migrate from winapi to windows-sys #4065

Merged
merged 11 commits into from
Oct 20, 2022
Merged
96 changes: 76 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ skip = [
{ name = "clap", version = "=3.2.22" },
# bindgen (via selinux-sys & fts-sys)
{ name = "clap_lex", version = "=0.2.4" },
# windows-sys (via terminal_size, crossterm -> parking_lot, notify -> filetime)
{ name = "windows-sys", version = "=0.36.1" },
{ name = "windows_aarch64_msvc", version = "=0.36.1" },
{ name = "windows_i686_gnu", version = "=0.36.1" },
{ name = "windows_i686_msvc", version = "=0.36.1" },
{ name = "windows_x86_64_gnu", version = "=0.36.1" },
{ name = "windows_x86_64_msvc", version = "=0.36.1" },
]
# spell-checker: enable

Expand Down
3 changes: 0 additions & 3 deletions src/uu/cp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ selinux = { version="0.3", optional=true }
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["entries", "fs", "perms", "mode"] }
walkdir = "2.2"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version="0.3", features=["fileapi"] }

[target.'cfg(unix)'.dependencies]
xattr="0.2.3"
exacl= { version = "0.9.0", optional=true }
Expand Down
2 changes: 1 addition & 1 deletion src/uu/date/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["minwinbase", "sysinfoapi", "minwindef"] }
windows-sys = { version = "0.42.0", default-features = false, features = ["Win32_Foundation", "Win32_System_SystemInformation"] }

[[bin]]
name = "date"
Expand Down
19 changes: 8 additions & 11 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ use uucore::error::FromIo;
use uucore::error::{UResult, USimpleError};
use uucore::{format_usage, show_error};
#[cfg(windows)]
use winapi::{
shared::minwindef::WORD,
um::{minwinbase::SYSTEMTIME, sysinfoapi::SetSystemTime},
};
use windows_sys::Win32::{Foundation::SYSTEMTIME, System::SystemInformation::SetSystemTime};

// Options
const DATE: &str = "date";
Expand Down Expand Up @@ -409,16 +406,16 @@ fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {
/// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {
let system_time = SYSTEMTIME {
wYear: date.year() as WORD,
wMonth: date.month() as WORD,
wYear: date.year() as u16,
wMonth: date.month() as u16,
// Ignored
wDayOfWeek: 0,
wDay: date.day() as WORD,
wHour: date.hour() as WORD,
wMinute: date.minute() as WORD,
wSecond: date.second() as WORD,
wDay: date.day() as u16,
wHour: date.hour() as u16,
wMinute: date.minute() as u16,
wSecond: date.second() as u16,
// TODO: be careful of leap seconds - valid range is [0, 999] - how to handle?
wMilliseconds: ((date.nanosecond() / 1_000_000) % 1000) as WORD,
wMilliseconds: ((date.nanosecond() / 1_000_000) % 1000) as u16,
};

let result = unsafe { SetSystemTime(&system_time) };
Expand Down
2 changes: 1 addition & 1 deletion src/uu/du/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clap = { version = "4.0", features = ["wrap_help", "cargo"] }
uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version="0.3", features=[] }
windows-sys = { version = "0.42.0", default-features = false, features = ["Win32_Storage_FileSystem", "Win32_Foundation"] }

[[bin]]
name = "du"
Expand Down
31 changes: 12 additions & 19 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ use uucore::format_usage;
use uucore::parse_glob;
use uucore::parse_size::{parse_size, ParseSizeError};
#[cfg(windows)]
use winapi::shared::minwindef::{DWORD, LPVOID};
use windows_sys::Win32::Foundation::HANDLE;
#[cfg(windows)]
use winapi::um::fileapi::{FILE_ID_INFO, FILE_STANDARD_INFO};
#[cfg(windows)]
use winapi::um::minwinbase::{FileIdInfo, FileStandardInfo};
#[cfg(windows)]
use winapi::um::winbase::GetFileInformationByHandleEx;
#[cfg(windows)]
use winapi::um::winnt::FILE_ID_128;
use windows_sys::Win32::Storage::FileSystem::{
FileIdInfo, FileStandardInfo, GetFileInformationByHandleEx, FILE_ID_128, FILE_ID_INFO,
FILE_STANDARD_INFO,
};

mod options {
pub const HELP: &str = "help";
Expand Down Expand Up @@ -208,21 +205,19 @@ fn get_size_on_disk(path: &Path) -> u64 {
Err(_) => return size_on_disk, // opening directories will fail
};

let handle = file.as_raw_handle();

unsafe {
let mut file_info: FILE_STANDARD_INFO = core::mem::zeroed();
let file_info_ptr: *mut FILE_STANDARD_INFO = &mut file_info;

let success = GetFileInformationByHandleEx(
handle,
file.as_raw_handle() as HANDLE,
FileStandardInfo,
file_info_ptr as LPVOID,
std::mem::size_of::<FILE_STANDARD_INFO>() as DWORD,
file_info_ptr as _,
std::mem::size_of::<FILE_STANDARD_INFO>() as u32,
);

if success != 0 {
size_on_disk = *file_info.AllocationSize.QuadPart() as u64;
size_on_disk = file_info.AllocationSize as u64;
}
}

Expand All @@ -238,17 +233,15 @@ fn get_file_info(path: &Path) -> Option<FileInfo> {
Err(_) => return result,
};

let handle = file.as_raw_handle();

unsafe {
let mut file_info: FILE_ID_INFO = core::mem::zeroed();
let file_info_ptr: *mut FILE_ID_INFO = &mut file_info;

let success = GetFileInformationByHandleEx(
handle,
file.as_raw_handle() as HANDLE,
FileIdInfo,
file_info_ptr as LPVOID,
std::mem::size_of::<FILE_ID_INFO>() as DWORD,
file_info_ptr as _,
std::mem::size_of::<FILE_ID_INFO>() as u32,
);

if success != 0 {
Expand Down
Loading