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

windows-sys to windows #5688

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
37 changes: 28 additions & 9 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ unicode-width = "0.1.11"
utf-8 = "0.7.6"
walkdir = "2.4"
winapi-util = "0.1.6"
windows-sys = { version = "0.48.0", default-features = false }
windows = { version = "0.52.0", default-features = false }
xattr = "1.1.3"
zip = { version = "0.6.6", default-features = false, features = ["deflate"] }

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 @@ -25,7 +25,7 @@ parse_datetime = { workspace = true }
libc = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
windows = { workspace = true, features = [
"Win32_Foundation",
"Win32_System_SystemInformation",
] }
Expand Down
7 changes: 4 additions & 3 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use uucore::error::FromIo;
use uucore::error::{UResult, USimpleError};
use uucore::{format_usage, help_about, help_usage, show};
#[cfg(windows)]
use windows_sys::Win32::{Foundation::SYSTEMTIME, System::SystemInformation::SetSystemTime};
use windows::Win32::{Foundation::SYSTEMTIME, System::SystemInformation::SetSystemTime};

use uucore::shortcut_value_parser::ShortcutValueParser;

Expand Down Expand Up @@ -472,8 +472,9 @@ fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {

let result = unsafe { SetSystemTime(&system_time) };

if result == 0 {
Err(std::io::Error::last_os_error().map_err_context(|| "cannot set date".to_string()))
if let Err(e) = result {
Err(std::io::Error::from_raw_os_error(e.code().0)
.map_err_context(|| "cannot set date".to_string()))
} else {
Ok(())
}
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 = { workspace = true }
uucore = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { workspace = true, features = [
windows = { workspace = true, features = [
"Win32_Storage_FileSystem",
"Win32_Foundation",
] }
Expand Down
47 changes: 20 additions & 27 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::{format_usage, help_about, help_section, help_usage, show, show_warning};
#[cfg(windows)]
use windows_sys::Win32::Foundation::HANDLE;
use windows::Win32::Foundation::HANDLE;

Check failure on line 35 in src/uu/du/src/du.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/du/src/du.rs', line:35; use `cargo fmt -- "src/uu/du/src/du.rs"`)

Check failure on line 35 in src/uu/du/src/du.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/du/src/du.rs', line:35; use `cargo fmt -- "src/uu/du/src/du.rs"`)
#[cfg(windows)]
use windows_sys::Win32::Storage::FileSystem::{
FileIdInfo, FileStandardInfo, GetFileInformationByHandleEx, FILE_ID_128, FILE_ID_INFO,
use windows::Win32::Storage::FileSystem::{
FileIdInfo, FileStandardInfo, GetFileInformationByHandleEx, FILE_ID_INFO,
FILE_STANDARD_INFO,
};

Expand Down Expand Up @@ -230,14 +230,14 @@
let mut file_info: FILE_STANDARD_INFO = core::mem::zeroed();
let file_info_ptr: *mut FILE_STANDARD_INFO = &mut file_info;

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

if success != 0 {
if result.is_ok() {
size_on_disk = file_info.AllocationSize as u64;
}
}
Expand All @@ -247,33 +247,26 @@

#[cfg(windows)]
fn get_file_info(path: &Path) -> Option<FileInfo> {
let mut result = None;

let file = match fs::File::open(path) {
Ok(file) => file,
Err(_) => return result,
};
let file = fs::File::open(path).ok()?;

let mut file_info = FILE_ID_INFO::default();
let file_info_ptr: *mut FILE_ID_INFO = &mut file_info;
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(
file.as_raw_handle() as HANDLE,
GetFileInformationByHandleEx(
HANDLE(file.as_raw_handle() as isize),
FileIdInfo,

Check failure on line 257 in src/uu/du/src/du.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/du/src/du.rs', line:257; use `cargo fmt -- "src/uu/du/src/du.rs"`)

Check failure on line 257 in src/uu/du/src/du.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/du/src/du.rs', line:257; use `cargo fmt -- "src/uu/du/src/du.rs"`)
file_info_ptr as _,
std::mem::size_of::<FILE_ID_INFO>() as u32,
);

if success != 0 {
result = Some(FileInfo {
file_id: std::mem::transmute::<FILE_ID_128, u128>(file_info.FileId),
dev_id: file_info.VolumeSerialNumber,
});
}
}
).ok()?
};

result
// `from_le_bytes` won't be correct on all systems. However, we only care
// whether the value is unique for all files since it's only used to put
// the `FileInfo` in a `HashSet`.
Some(FileInfo {
file_id: u128::from_le_bytes(file_info.FileId.Identifier),
dev_id: file_info.VolumeSerialNumber,
})
}

fn read_block_size(s: Option<&str>) -> UResult<u64> {
Expand Down
2 changes: 1 addition & 1 deletion src/uu/hostname/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ hostname = { workspace = true, features = ["set"] }
uucore = { workspace = true, features = ["wide"] }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { workspace = true, features = [
windows = { workspace = true, features = [
"Win32_Networking_WinSock",
"Win32_Foundation",
] }
Expand Down
2 changes: 1 addition & 1 deletion src/uu/hostname/src/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
mod wsa {
use std::io;

use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAStartup, WSADATA};
use windows::Win32::Networking::WinSock::{WSACleanup, WSAStartup, WSADATA};

Check warning on line 33 in src/uu/hostname/src/hostname.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/hostname/src/hostname.rs#L33

Added line #L33 was not covered by tests

pub(super) struct WsaHandle(());

Expand Down
2 changes: 1 addition & 1 deletion src/uu/rm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ uucore = { workspace = true, features = ["fs"] }
libc = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = ["Win32_Storage_FileSystem"] }
windows = { workspace = true, features = ["Win32_Storage_FileSystem"] }

[[bin]]
name = "rm"
Expand Down
8 changes: 4 additions & 4 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata
#[cfg(windows)]
fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata) -> bool {
use std::os::windows::prelude::MetadataExt;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_READONLY;
let not_user_writable = (metadata.file_attributes() & FILE_ATTRIBUTE_READONLY) != 0;
use windows::Win32::Storage::FileSystem::FILE_ATTRIBUTE_READONLY;
let not_user_writable = (metadata.file_attributes() & FILE_ATTRIBUTE_READONLY.0) != 0;
if not_user_writable {
prompt_yes!("remove write-protected directory {}?", path.quote())
} else if options.interactive == InteractiveMode::Always {
Expand Down Expand Up @@ -606,8 +606,8 @@ fn is_symlink_dir(_metadata: &Metadata) -> bool {
#[cfg(windows)]
fn is_symlink_dir(metadata: &Metadata) -> bool {
use std::os::windows::prelude::MetadataExt;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_DIRECTORY;
use windows::Win32::Storage::FileSystem::FILE_ATTRIBUTE_DIRECTORY;

metadata.file_type().is_symlink()
&& ((metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY) != 0)
&& ((metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY.0) != 0)
}
2 changes: 1 addition & 1 deletion src/uu/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ uucore = { workspace = true, features = ["wide"] }
nix = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { workspace = true, features = [
windows = { workspace = true, features = [
"Win32_Storage_FileSystem",
"Win32_System_WindowsProgramming",
"Win32_Foundation",
Expand Down
39 changes: 19 additions & 20 deletions src/uu/sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,30 @@
mod platform {
use std::fs::OpenOptions;
use std::os::windows::prelude::*;
use std::path::Path;

Check failure on line 73 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/sync/src/sync.rs', line:73; use `cargo fmt -- "src/uu/sync/src/sync.rs"`)

Check failure on line 73 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/sync/src/sync.rs', line:73; use `cargo fmt -- "src/uu/sync/src/sync.rs"`)
use uucore::crash;
use uucore::wide::{FromWide, ToWide};
use windows_sys::Win32::Foundation::{
GetLastError, ERROR_NO_MORE_FILES, HANDLE, INVALID_HANDLE_VALUE, MAX_PATH,
use windows::Win32::Foundation::{
ERROR_NO_MORE_FILES, HANDLE, MAX_PATH,
};
use windows_sys::Win32::Storage::FileSystem::{
use windows::Win32::Storage::FileSystem::{
FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, FlushFileBuffers, GetDriveTypeW,
};
use windows_sys::Win32::System::WindowsProgramming::DRIVE_FIXED;
use windows::Win32::System::WindowsProgramming::DRIVE_FIXED;

Check failure on line 82 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/sync/src/sync.rs', line:82; use `cargo fmt -- "src/uu/sync/src/sync.rs"`)

Check failure on line 82 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/sync/src/sync.rs', line:82; use `cargo fmt -- "src/uu/sync/src/sync.rs"`)
use windows::core::{PCWSTR, Error};

Check failure on line 83 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: Unknown word (PCWSTR) (file:'src/uu/sync/src/sync.rs', line:83)

unsafe fn flush_volume(name: &str) {
let name_wide = name.to_wide_null();
if GetDriveTypeW(name_wide.as_ptr()) == DRIVE_FIXED {
if GetDriveTypeW(PCWSTR::from_raw(name_wide.as_ptr())) == DRIVE_FIXED {

Check failure on line 87 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: Unknown word (PCWSTR) (file:'src/uu/sync/src/sync.rs', line:87)
let sliced_name = &name[..name.len() - 1]; // eliminate trailing backslash
match OpenOptions::new().write(true).open(sliced_name) {
Ok(file) => {
if FlushFileBuffers(file.as_raw_handle() as HANDLE) == 0 {
crash!(GetLastError() as i32, "failed to flush file buffer");
if FlushFileBuffers(HANDLE(file.as_raw_handle() as isize)).is_err() {
crash!(1, "failed to flush file buffer");

Check failure on line 92 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/sync/src/sync.rs', line:92; use `cargo fmt -- "src/uu/sync/src/sync.rs"`)

Check failure on line 92 in src/uu/sync/src/sync.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/sync/src/sync.rs', line:92; use `cargo fmt -- "src/uu/sync/src/sync.rs"`)
}
}
Err(e) => crash!(
e.raw_os_error().unwrap_or(1),
Err(_) => crash!(
1,
"failed to create volume handle"
),
}
Expand All @@ -101,10 +102,9 @@

unsafe fn find_first_volume() -> (String, HANDLE) {
let mut name: [u16; MAX_PATH as usize] = [0; MAX_PATH as usize];
let handle = FindFirstVolumeW(name.as_mut_ptr(), name.len() as u32);
if handle == INVALID_HANDLE_VALUE {
crash!(GetLastError() as i32, "failed to find first volume");
}
let Ok(handle) = FindFirstVolumeW(&mut name) else {
crash!(1, "failed to find first volume");
};
(String::from_wide_null(&name), handle)
}

Expand All @@ -113,13 +113,12 @@
let mut volumes = vec![first_volume];
loop {
let mut name: [u16; MAX_PATH as usize] = [0; MAX_PATH as usize];
if FindNextVolumeW(next_volume_handle, name.as_mut_ptr(), name.len() as u32) == 0 {
match GetLastError() {
ERROR_NO_MORE_FILES => {
FindVolumeClose(next_volume_handle);
return volumes;
}
err => crash!(err as i32, "failed to find next volume"),
if let Err(e) = FindNextVolumeW(next_volume_handle, &mut name) {
if e == Error::from(ERROR_NO_MORE_FILES) {
let _ = FindVolumeClose(next_volume_handle);
return volumes;
} else {
crash!(1, "failed to find next volume");
}
} else {
volumes.push(String::from_wide_null(&name));
Expand Down
2 changes: 1 addition & 1 deletion src/uu/tail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ same-file = { workspace = true }
fundu = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
windows = { workspace = true, features = [
"Win32_System_Threading",
"Win32_Foundation",
] }
Expand Down
Loading
Loading