From 1d07bd361e94e3230b9e71b1fa0727b61e06beba Mon Sep 17 00:00:00 2001 From: "J. David Lowe" Date: Tue, 26 Nov 2024 12:52:15 -0800 Subject: [PATCH] windows impl --- src/windows/disk.rs | 126 ++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 52 deletions(-) diff --git a/src/windows/disk.rs b/src/windows/disk.rs index a4ea8bf87..f23829f7d 100644 --- a/src/windows/disk.rs +++ b/src/windows/disk.rs @@ -167,25 +167,32 @@ impl DiskInner { self.is_read_only } - pub(crate) fn refresh_specifics(&mut self, _refreshes: DiskRefreshKind) -> bool { - let Some((read_bytes, written_bytes)) = get_disk_io(&self.device_path, None) else { - sysinfo_debug!("Failed to update disk i/o stats"); - return false; - }; + pub(crate) fn refresh_specifics(&mut self, refreshes: DiskRefreshKind) -> bool { + if refreshes.io_usage() { + let Some((read_bytes, written_bytes)) = get_disk_io(&self.device_path, None) else { + sysinfo_debug!("Failed to update disk i/o stats"); + return false; + }; - self.old_read_bytes = self.read_bytes; - self.old_written_bytes = self.written_bytes; - self.read_bytes = read_bytes; - self.written_bytes = written_bytes; - - if self.total_space != 0 { - unsafe { - let mut tmp = 0; - let lpdirectoryname = PCWSTR::from_raw(self.mount_point.as_ptr()); - if GetDiskFreeSpaceExW(lpdirectoryname, None, None, Some(&mut tmp)).is_ok() { - self.available_space = tmp; - return true; - } + self.old_read_bytes = self.read_bytes; + self.old_written_bytes = self.written_bytes; + self.read_bytes = read_bytes; + self.written_bytes = written_bytes; + } + + if refreshes.details() { + if self.total_space != 0 { + let available_space = unsafe { + let mut tmp = 0; + let lpdirectoryname = PCWSTR::from_raw(self.mount_point.as_ptr()); + if GetDiskFreeSpaceExW(lpdirectoryname, None, None, Some(&mut tmp)).is_ok() { + tmp + } else { + 0 + } + }; + + self.available_space = available_space; } } false @@ -220,9 +227,9 @@ impl DisksInner { self.disks } - pub(crate) fn refresh_list_specifics(&mut self, _refreshes: DiskRefreshKind) { + pub(crate) fn refresh_list_specifics(&mut self, refreshes: DiskRefreshKind) { unsafe { - self.disks = get_list(); + self.disks = get_list(refreshes); } } @@ -259,7 +266,7 @@ unsafe fn get_drive_size(mount_point: &[u16]) -> Option<(u64, u64)> { } } -pub(crate) unsafe fn get_list() -> Vec { +pub(crate) unsafe fn get_list(refreshes: DiskRefreshKind) -> Vec { #[cfg(feature = "multithread")] use rayon::iter::ParallelIterator; @@ -268,7 +275,11 @@ pub(crate) unsafe fn get_list() -> Vec { let raw_volume_name = PCWSTR::from_raw(volume_name.as_ptr()); let drive_type = GetDriveTypeW(raw_volume_name); - let is_removable = drive_type == DRIVE_REMOVABLE; + let is_removable = if refreshes.details() { + drive_type == DRIVE_REMOVABLE + } else { + false + }; if drive_type != DRIVE_FIXED && drive_type != DRIVE_REMOVABLE { return Vec::new(); @@ -292,7 +303,11 @@ pub(crate) unsafe fn get_list() -> Vec { ); return Vec::new(); } - let is_read_only = (flags & FILE_READ_ONLY_VOLUME) != 0; + let is_read_only = if refreshes.details() { + (flags & FILE_READ_ONLY_VOLUME) != 0 + } else { + false + }; let mount_paths = get_volume_path_names_for_volume_name(&volume_name[..]); if mount_paths.is_empty() { @@ -309,13 +324,12 @@ pub(crate) unsafe fn get_list() -> Vec { else { return Vec::new(); }; - let Some((total_space, available_space)) = get_drive_size(&mount_paths[0][..]) else { - return Vec::new(); + + let (total_space, available_space) = if refreshes.details() { + get_drive_size(&mount_paths[0][..]).unwrap_or_default() + } else { + (0, 0) }; - if total_space == 0 { - sysinfo_debug!("total_space == 0"); - return Vec::new(); - } let spq_trim = STORAGE_PROPERTY_QUERY { PropertyId: StorageDeviceSeekPenaltyProperty, QueryType: PropertyStandardQuery, @@ -323,33 +337,41 @@ pub(crate) unsafe fn get_list() -> Vec { }; let mut result: DEVICE_SEEK_PENALTY_DESCRIPTOR = std::mem::zeroed(); - let mut dw_size = 0; - let device_io_control = DeviceIoControl( - handle.0, - IOCTL_STORAGE_QUERY_PROPERTY, - Some(&spq_trim as *const STORAGE_PROPERTY_QUERY as *const c_void), - size_of::() as u32, - Some(&mut result as *mut DEVICE_SEEK_PENALTY_DESCRIPTOR as *mut c_void), - size_of::() as u32, - Some(&mut dw_size), - None, - ) - .is_ok(); - let type_ = if !device_io_control - || dw_size != size_of::() as u32 - { - DiskKind::Unknown(-1) - } else { - let is_hdd = result.IncursSeekPenalty.as_bool(); - if is_hdd { - DiskKind::HDD + let type_ = if refreshes.kind() { + let mut dw_size = 0; + let device_io_control = DeviceIoControl( + handle.0, + IOCTL_STORAGE_QUERY_PROPERTY, + Some(&spq_trim as *const STORAGE_PROPERTY_QUERY as *const c_void), + size_of::() as u32, + Some(&mut result as *mut DEVICE_SEEK_PENALTY_DESCRIPTOR as *mut c_void), + size_of::() as u32, + Some(&mut dw_size), + None, + ) + .is_ok(); + + if !device_io_control + || dw_size != size_of::() as u32 + { + DiskKind::Unknown(-1) } else { - DiskKind::SSD + let is_hdd = result.IncursSeekPenalty.as_bool(); + if is_hdd { + DiskKind::HDD + } else { + DiskKind::SSD + } } + } else { + DiskKind::Unknown(-1) }; - let (read_bytes, written_bytes) = - get_disk_io(&device_path, Some(handle)).unwrap_or_default(); + let (read_bytes, written_bytes) = if refreshes.io_usage() { + get_disk_io(&device_path, Some(handle)).unwrap_or_default() + } else { + (0, 0) + }; let name = os_string_from_zero_terminated(&name); let file_system = os_string_from_zero_terminated(&file_system);