Skip to content

Commit

Permalink
deps: bump windows to 0.51.1 (#1279)
Browse files Browse the repository at this point in the history
* deps: bump windows to 0.51.1

* some changes to fit new API
  • Loading branch information
ClementTsang authored Aug 22, 2023
1 parent 2e2b32c commit ac55add
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
14 changes: 12 additions & 2 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 @@ -114,7 +114,7 @@ core-foundation = "0.9.3"
mach2 = "0.4.1"

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.48.0", features = [
windows = { version = "0.51.1", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
Expand Down
54 changes: 16 additions & 38 deletions src/app/data_harvester/disks/windows/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::{

use anyhow::bail;
use windows::Win32::{
Foundation::{self, CloseHandle},
Foundation::{self, CloseHandle, HANDLE},
Storage::FileSystem::{
CreateFileW, FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, FindVolumeHandle,
CreateFileW, FindFirstVolumeW, FindNextVolumeW, FindVolumeClose,
GetVolumeNameForVolumeMountPointW, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_READ,
FILE_SHARE_WRITE, OPEN_EXISTING,
},
Expand Down Expand Up @@ -77,28 +77,12 @@ fn volume_io(volume: &Path) -> anyhow::Result<DISK_PERFORMANCE> {

// SAFETY: This should be safe, we will check the result as well.
let handle_result = unsafe { CloseHandle(h_device) };
if !handle_result.as_bool() {
const ERROR_INVALID_FUNCTION: i32 = Foundation::ERROR_INVALID_FUNCTION.0 as i32;
const ERROR_NOT_SUPPORTED: i32 = Foundation::ERROR_NOT_SUPPORTED.0 as i32;

match io::Error::last_os_error().raw_os_error() {
Some(ERROR_INVALID_FUNCTION) => {
bail!("Handle error: invalid function");
}
Some(ERROR_NOT_SUPPORTED) => {
bail!("Handle error: not supported");
}
_ => {
bail!(
"Unknown handle device result error: {:?}",
io::Error::last_os_error()
);
}
}
if let Err(err) = handle_result {
bail!("Handle error: {err:?}");
}

if !ret.as_bool() {
bail!("Device I/O error: {:?}", io::Error::last_os_error());
if let Err(err) = ret {
bail!("Device I/O error: {err:?}");
} else {
Ok(disk_performance)
}
Expand All @@ -111,15 +95,11 @@ fn current_volume(buffer: &[u16]) -> PathBuf {
PathBuf::from(path_string)
}

fn close_find_handle(handle: FindVolumeHandle) -> anyhow::Result<()> {
fn close_find_handle(handle: HANDLE) -> anyhow::Result<()> {
// Clean up the handle.
// SAFETY: This should be safe, we will check the result as well.
let handle_result = unsafe { FindVolumeClose(handle) };
if !handle_result.as_bool() {
bail!("Could not close volume handle.");
} else {
Ok(())
}
let res = unsafe { FindVolumeClose(handle) };
Ok(res?)
}

/// Returns the I/O for all volumes.
Expand All @@ -144,17 +124,18 @@ pub(crate) fn all_volume_io() -> anyhow::Result<Vec<anyhow::Result<(DISK_PERFORM
}

// Now iterate until there are no more volumes.
while unsafe { FindNextVolumeW(handle, &mut buffer) }.as_bool() {
while unsafe { FindNextVolumeW(handle, &mut buffer) }.is_ok() {
let volume = current_volume(&buffer);
ret.push(volume_io(&volume).map(|res| (res, volume.to_string_lossy().to_string())));
}

let err = io::Error::last_os_error();
match err.raw_os_error() {
// Iteration completed successfully, continue on.
Some(ERROR_NO_MORE_FILES) => {}
// Some error occured.
Some(ERROR_NO_MORE_FILES) => {
// Iteration completed successfully, continue on.
}
_ => {
// Some error occured.
close_find_handle(handle)?;
bail!("Error while iterating over volumes: {err:?}");
}
Expand Down Expand Up @@ -187,11 +168,8 @@ pub(crate) fn volume_name_from_mount(mount: &str) -> anyhow::Result<String> {
GetVolumeNameForVolumeMountPointW(windows::core::PCWSTR(mount.as_ptr()), &mut buffer)
};

if !result.as_bool() {
bail!(
"Could not get volume name for mount point: {:?}",
io::Error::last_os_error()
);
if let Err(err) = result {
bail!("Could not get volume name for mount point: {err:?}");
} else {
Ok(current_volume(&buffer).to_string_lossy().to_string())
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/data_harvester/memory/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::mem::{size_of, zeroed};

use windows::Win32::Foundation::TRUE;
use windows::Win32::System::ProcessStatus::{GetPerformanceInfo, PERFORMANCE_INFORMATION};

use crate::data_harvester::memory::MemHarvest;
Expand All @@ -14,7 +13,7 @@ pub(crate) fn get_swap_usage() -> Option<MemHarvest> {
// the bindings are "safe" to use with how we call them.
unsafe {
let mut perf_info: PERFORMANCE_INFORMATION = zeroed();
if GetPerformanceInfo(&mut perf_info, size_of::<PERFORMANCE_INFORMATION>() as u32) == TRUE {
if GetPerformanceInfo(&mut perf_info, size_of::<PERFORMANCE_INFORMATION>() as u32).is_ok() {
// Saturating sub by perf_info.PhysicalTotal for what sysinfo does.
let swap_total = perf_info.PageSize.saturating_mul(perf_info.CommitLimit) as u64;
let swap_used = perf_info.PageSize.saturating_mul(perf_info.CommitTotal) as u64;
Expand Down
4 changes: 2 additions & 2 deletions src/app/process_killer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Process {
fn kill(self) -> Result<(), String> {
// SAFETY: Windows API call, this is safe as we are passing in the handle.
let result = unsafe { TerminateProcess(self.0, 1) };
if result.0 == 0 {
if result.is_err() {
return Err("process may have already been terminated.".to_string());
}

Expand All @@ -42,7 +42,7 @@ impl Drop for Process {
fn drop(&mut self) {
// SAFETY: Windows API call, this is safe as we are passing in the handle.
unsafe {
CloseHandle(self.0);
let _ = CloseHandle(self.0);
}
}
}
Expand Down

0 comments on commit ac55add

Please sign in to comment.