mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 21:47:46 +00:00
sync: migrate winapi
to windows-sys
crate
This commit is contained in:
parent
5ef1745960
commit
580bff02f8
3 changed files with 23 additions and 36 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2943,7 +2943,7 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"nix",
|
"nix",
|
||||||
"uucore",
|
"uucore",
|
||||||
"winapi",
|
"windows-sys 0.42.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -23,11 +23,11 @@ uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=[
|
||||||
nix = "0.25"
|
nix = "0.25"
|
||||||
|
|
||||||
[target.'cfg(target_os = "windows")'.dependencies]
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["errhandlingapi", "fileapi", "handleapi", "std", "winbase", "winerror"] }
|
windows-sys = { version = "0.42.0", default-features = false, features = ["Win32_Storage_FileSystem", "Win32_System_WindowsProgramming", "Win32_Foundation"] }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "sync"
|
name = "sync"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[package.metadata.cargo-udeps.ignore]
|
[package.metadata.cargo-udeps.ignore]
|
||||||
normal = ["uucore_procs", "winapi"]
|
normal = ["uucore_procs"]
|
||||||
|
|
|
@ -70,29 +70,27 @@ mod platform {
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
mod platform {
|
mod platform {
|
||||||
extern crate winapi;
|
|
||||||
use self::winapi::shared::minwindef;
|
|
||||||
use self::winapi::shared::winerror;
|
|
||||||
use self::winapi::um::handleapi;
|
|
||||||
use self::winapi::um::winbase;
|
|
||||||
use self::winapi::um::winnt;
|
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::os::windows::prelude::*;
|
use std::os::windows::prelude::*;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use uucore::crash;
|
use uucore::crash;
|
||||||
use uucore::wide::{FromWide, ToWide};
|
use uucore::wide::{FromWide, ToWide};
|
||||||
|
use windows_sys::Win32::Foundation::{
|
||||||
|
GetLastError, ERROR_NO_MORE_FILES, HANDLE, INVALID_HANDLE_VALUE, MAX_PATH,
|
||||||
|
};
|
||||||
|
use windows_sys::Win32::Storage::FileSystem::{
|
||||||
|
FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, FlushFileBuffers, GetDriveTypeW,
|
||||||
|
};
|
||||||
|
use windows_sys::Win32::System::WindowsProgramming::DRIVE_FIXED;
|
||||||
|
|
||||||
unsafe fn flush_volume(name: &str) {
|
unsafe fn flush_volume(name: &str) {
|
||||||
let name_wide = name.to_wide_null();
|
let name_wide = name.to_wide_null();
|
||||||
if winapi::um::fileapi::GetDriveTypeW(name_wide.as_ptr()) == winbase::DRIVE_FIXED {
|
if GetDriveTypeW(name_wide.as_ptr()) == DRIVE_FIXED {
|
||||||
let sliced_name = &name[..name.len() - 1]; // eliminate trailing backslash
|
let sliced_name = &name[..name.len() - 1]; // eliminate trailing backslash
|
||||||
match OpenOptions::new().write(true).open(sliced_name) {
|
match OpenOptions::new().write(true).open(sliced_name) {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
if winapi::um::fileapi::FlushFileBuffers(file.as_raw_handle()) == 0 {
|
if FlushFileBuffers(file.as_raw_handle() as HANDLE) == 0 {
|
||||||
crash!(
|
crash!(GetLastError() as i32, "failed to flush file buffer");
|
||||||
winapi::um::errhandlingapi::GetLastError() as i32,
|
|
||||||
"failed to flush file buffer"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => crash!(
|
Err(e) => crash!(
|
||||||
|
@ -103,17 +101,11 @@ mod platform {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn find_first_volume() -> (String, winnt::HANDLE) {
|
unsafe fn find_first_volume() -> (String, HANDLE) {
|
||||||
let mut name: [winnt::WCHAR; minwindef::MAX_PATH] = [0; minwindef::MAX_PATH];
|
let mut name: [u16; MAX_PATH as usize] = [0; MAX_PATH as usize];
|
||||||
let handle = winapi::um::fileapi::FindFirstVolumeW(
|
let handle = FindFirstVolumeW(name.as_mut_ptr(), name.len() as u32);
|
||||||
name.as_mut_ptr(),
|
if handle == INVALID_HANDLE_VALUE {
|
||||||
name.len() as minwindef::DWORD,
|
crash!(GetLastError() as i32, "failed to find first volume");
|
||||||
);
|
|
||||||
if handle == handleapi::INVALID_HANDLE_VALUE {
|
|
||||||
crash!(
|
|
||||||
winapi::um::errhandlingapi::GetLastError() as i32,
|
|
||||||
"failed to find first volume"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
(String::from_wide_null(&name), handle)
|
(String::from_wide_null(&name), handle)
|
||||||
}
|
}
|
||||||
|
@ -122,16 +114,11 @@ mod platform {
|
||||||
let (first_volume, next_volume_handle) = find_first_volume();
|
let (first_volume, next_volume_handle) = find_first_volume();
|
||||||
let mut volumes = vec![first_volume];
|
let mut volumes = vec![first_volume];
|
||||||
loop {
|
loop {
|
||||||
let mut name: [winnt::WCHAR; minwindef::MAX_PATH] = [0; minwindef::MAX_PATH];
|
let mut name: [u16; MAX_PATH as usize] = [0; MAX_PATH as usize];
|
||||||
if winapi::um::fileapi::FindNextVolumeW(
|
if FindNextVolumeW(next_volume_handle, name.as_mut_ptr(), name.len() as u32) == 0 {
|
||||||
next_volume_handle,
|
match GetLastError() {
|
||||||
name.as_mut_ptr(),
|
ERROR_NO_MORE_FILES => {
|
||||||
name.len() as minwindef::DWORD,
|
FindVolumeClose(next_volume_handle);
|
||||||
) == 0
|
|
||||||
{
|
|
||||||
match winapi::um::errhandlingapi::GetLastError() {
|
|
||||||
winerror::ERROR_NO_MORE_FILES => {
|
|
||||||
winapi::um::fileapi::FindVolumeClose(next_volume_handle);
|
|
||||||
return volumes;
|
return volumes;
|
||||||
}
|
}
|
||||||
err => crash!(err as i32, "failed to find next volume"),
|
err => crash!(err as i32, "failed to find next volume"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue