From ac55add21e88939d78c23e9be99ff5f38f5a461e Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:27:36 -0400 Subject: deps: bump windows to 0.51.1 (#1279) * deps: bump windows to 0.51.1 * some changes to fit new API --- src/app/data_harvester/disks/windows/bindings.rs | 54 +++++++----------------- src/app/data_harvester/memory/windows.rs | 3 +- src/app/process_killer.rs | 4 +- 3 files changed, 19 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/app/data_harvester/disks/windows/bindings.rs b/src/app/data_harvester/disks/windows/bindings.rs index 247c6b6d..c5436145 100644 --- a/src/app/data_harvester/disks/windows/bindings.rs +++ b/src/app/data_harvester/disks/windows/bindings.rs @@ -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, }, @@ -77,28 +77,12 @@ fn volume_io(volume: &Path) -> anyhow::Result { // 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) } @@ -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. @@ -144,17 +124,18 @@ pub(crate) fn all_volume_io() -> anyhow::Result {} - // 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:?}"); } @@ -187,11 +168,8 @@ pub(crate) fn volume_name_from_mount(mount: &str) -> anyhow::Result { 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()) } diff --git a/src/app/data_harvester/memory/windows.rs b/src/app/data_harvester/memory/windows.rs index c54657e9..5925a756 100644 --- a/src/app/data_harvester/memory/windows.rs +++ b/src/app/data_harvester/memory/windows.rs @@ -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; @@ -14,7 +13,7 @@ pub(crate) fn get_swap_usage() -> Option { // 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::() as u32) == TRUE { + if GetPerformanceInfo(&mut perf_info, size_of::() 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; diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index aac85639..106419ee 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -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()); } @@ -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); } } } -- cgit v1.2.3