Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
samin-cf committed Nov 3, 2024
1 parent a4e7bd8 commit fd52c86
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/common/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ impl Disk {

/// Returns number of bytes read and written by the disk
///
/// ⚠️ Note that FreeBSD is not yet supported
///
/// ```no_run
/// use sysinfo::Disks;
///
Expand Down
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) mod user;
/// println!("[{:?}] disk usage: {:?}", disk.name(), disk.usage());
/// }
/// ```
#[cfg(any(feature = "disk", feature = "system"))]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub struct DiskUsage {
/// Total number of written bytes.
Expand Down
32 changes: 10 additions & 22 deletions src/unix/apple/macos/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,17 @@ pub(crate) fn get_disk_type(bsd_name: &[u8]) -> Option<DiskKind> {
};

iterate_service_tree(bsd_name, characteristics_string, |_, properties| {
let disk_type = unsafe {
let medium = unsafe {
super::disk::get_str_value(
properties.inner(),
DictKey::Defined(ffi::kIOPropertyMediumTypeKey),
)
};
}?;

if let Some(disk_type) = disk_type.and_then(|medium| match medium.as_str() {
match medium.as_str() {
_ if medium == ffi::kIOPropertyMediumTypeSolidStateKey => Some(DiskKind::SSD),
_ if medium == ffi::kIOPropertyMediumTypeRotationalKey => Some(DiskKind::HDD),
_ => None,
}) {
Some(disk_type)
} else {
// Many external drive vendors do not advertise their device's storage medium.
//
// In these cases, assuming that there were _any_ properties about them registered, we fallback
// to `HDD` when no storage medium is provided by the device instead of `Unknown`.
Some(DiskKind::HDD)
_ => Some(DiskKind::Unknown(-1)),
}
})
}
Expand All @@ -146,20 +138,16 @@ pub(crate) fn get_disk_io(bsd_name: &[u8]) -> Option<(u64, u64)> {
}

unsafe {
super::disk::get_int_value(
let read_bytes = super::disk::get_int_value(
properties.inner(),
DictKey::Defined(ffi::kIOBlockStorageDriverStatisticsBytesReadKey),
)
.zip(super::disk::get_int_value(
)?;
let written_bytes = super::disk::get_int_value(
properties.inner(),
DictKey::Defined(ffi::kIOBlockStorageDriverStatisticsBytesWrittenKey),
))
)?;

Some((read_bytes.try_into().ok()?, written_bytes.try_into().ok()?))
}
.and_then(|(read_bytes, written_bytes)| {
read_bytes
.try_into()
.ok()
.zip(written_bytes.try_into().ok())
})
})
}
6 changes: 3 additions & 3 deletions src/unknown/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub(crate) struct DiskInner;

impl DiskInner {
pub(crate) fn kind(&self) -> DiskKind {
unreachable!()
DiskKind::Unknown(-1)
}

pub(crate) fn name(&self) -> &OsStr {
unreachable!()
OsStr::new("")
}

pub(crate) fn file_system(&self) -> &OsStr {
Expand Down Expand Up @@ -44,7 +44,7 @@ impl DiskInner {
}

pub(crate) fn usage(&self) -> DiskUsage {
unreachable!()
DiskUsage::default()
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/windows/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,10 @@ fn get_disk_io(device_path: &[u16], handle: Option<HandleWrapper>) -> Option<(u6
sysinfo_debug!("Error: DeviceIoControl(IOCTL_DISK_PERFORMANCE) = {:?}", err);
err
})
.ok()
.and_then(|_| {
disk_perf
.BytesRead
.try_into()
.ok()
.zip(disk_perf.BytesWritten.try_into().ok())
})
.ok()?;

Some((
disk_perf.BytesRead.try_into().ok()?,
disk_perf.BytesWritten.try_into().ok()?,
))
}

0 comments on commit fd52c86

Please sign in to comment.