Skip to content

Commit

Permalink
more review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dlowe committed Nov 26, 2024
1 parent b41a502 commit 5f70cf8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
37 changes: 18 additions & 19 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ macro_rules! cast {
pub(crate) struct DiskInner {
type_: DiskKind,
device_name: OsString,
// Potential future surprise: right now, this field is only needed by usage-related code,
// so it is only populated if DiskRefreshKind::usage() is true.
actual_device_name: String,
actual_device_name: Option<String>,
file_system: OsString,
mount_point: PathBuf,
total_space: u64,
Expand Down Expand Up @@ -99,13 +97,18 @@ impl DiskInner {
}

if refresh_kind.io_usage() {
if self.actual_device_name.is_none() {
self.actual_device_name = Some(get_actual_device_name(&self.device_name));
}
let (read_bytes, written_bytes) = if let Some((read_bytes, written_bytes)) =
procfs_disk_stats.get(&self.actual_device_name).map(|stat| {
(
stat.sectors_read * SECTOR_SIZE,
stat.sectors_written * SECTOR_SIZE,
)
}) {
procfs_disk_stats
.get(self.actual_device_name.as_ref().unwrap())
.map(|stat| {
(
stat.sectors_read * SECTOR_SIZE,
stat.sectors_written * SECTOR_SIZE,
)
}) {
(read_bytes, written_bytes)
} else {
sysinfo_debug!("Failed to update disk i/o stats");
Expand Down Expand Up @@ -252,24 +255,20 @@ fn new_disk(
false
};

let actual_device_name = if refresh_kind.io_usage() {
get_actual_device_name(device_name)
} else {
String::new()
};

let (read_bytes, written_bytes) = if refresh_kind.io_usage() {
procfs_disk_stats
let (actual_device_name, read_bytes, written_bytes) = if refresh_kind.io_usage() {
let actual_device_name = get_actual_device_name(device_name);
let (read_bytes, written_bytes) = procfs_disk_stats
.get(&actual_device_name)
.map(|stat| {
(
stat.sectors_read * SECTOR_SIZE,
stat.sectors_written * SECTOR_SIZE,
)
})
.unwrap_or_default()
.unwrap_or_default();
(Some(actual_device_name), read_bytes, written_bytes)
} else {
(0, 0)
(None, 0, 0)
};

Disk {
Expand Down
22 changes: 7 additions & 15 deletions src/windows/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,16 @@ fn os_string_from_zero_terminated(name: &[u16]) -> OsString {
OsString::from_wide(&name[..len])
}

fn get_disk_kind(device_path: &[u16], borrowed_handle: &Option<HandleWrapper>) -> DiskKind {
let binding = (
borrowed_handle,
if borrowed_handle.is_none() {
unsafe { HandleWrapper::new_from_file(device_path, Default::default()) }
} else {
None
},
);
let handle = match binding {
(Some(ref handle), _) => handle,
(_, Some(ref handle)) => handle,
(None, None) => return DiskKind::Unknown(-1),
fn get_disk_kind(device_path: &[u16], borrowed_handle: Option<&HandleWrapper>) -> DiskKind {
let Some(handle) = borrowed_handle.or(Some(unsafe {
&HandleWrapper::new_from_file(device_path, Default::default())
})) else {
return DiskKind::Unknown(-1);
};

if handle.is_invalid() {
sysinfo_debug!(
"Expected handle to '{:?}' to be valid",
"Expected handle to {:?} to be valid",
String::from_utf16_lossy(device_path)
);
return DiskKind::Unknown(-1);
Expand Down Expand Up @@ -438,7 +430,7 @@ fn get_disk_io(device_path: &[u16], handle: Option<HandleWrapper>) -> Option<(u6

if handle.is_invalid() {
sysinfo_debug!(
"Expected handle to '{:?}' to be valid",
"Expected handle to {:?} to be valid",
String::from_utf16_lossy(device_path)
);
return None;
Expand Down
7 changes: 3 additions & 4 deletions tests/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ fn should_skip() -> bool {
}

let s = sysinfo::System::new_all();
if s.physical_core_count().unwrap_or_default() == 0 {
return true;
}
false

// If we don't have any physical core present, it's very likely that we're inside a VM...
s.physical_core_count().unwrap_or_default() == 0
}

#[test]
Expand Down

0 comments on commit 5f70cf8

Please sign in to comment.