Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/system_information/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Disk {
pub mount_point: String,
pub total_space: u64,
pub available_space: u64,
pub usage_percent: f64,
}

impl Disk {
Expand All @@ -21,19 +22,41 @@ impl Disk {

impl From<&sysinfo::Disk> for Disk {
fn from(sysinfo_disk: &sysinfo::Disk) -> Self {
let total_space = sysinfo_disk.total_space();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to propose an stylistic improvement (and safety guard). It also stores the usage bytes, and I figured "why not report that as well?"

diff --git a/src/system_information/disk.rs b/src/system_information/disk.rs
index 1cbc429..aadf840 100644
--- a/src/system_information/disk.rs
+++ b/src/system_information/disk.rs
@@ -5,6 +5,7 @@ pub struct Disk {
     pub name: String,
     pub mount_point: String,
     pub total_space: u64,
+    pub used_space: u64,
     pub available_space: u64,
     pub usage_percent: f64,
 }
@@ -24,16 +25,19 @@ impl From<&sysinfo::Disk> for Disk {
     fn from(sysinfo_disk: &sysinfo::Disk) -> Self {
         let total_space = sysinfo_disk.total_space();
         let available_space = sysinfo_disk.available_space();
-        let usage_percent = if total_space > 0 {
-            (total_space - available_space) as f64 / total_space as f64 * 100.0
-        } else {
-            0.0
+        // There should'nt be negative used bytes. We prevent underflow, to not falsely report more
+        // used than total space.
+        let used_space = total_space.saturating_sub(available_space);
+        let usage_percent = match used_space {
+            0 => 0.0,
+            used_space => used_space as f64 / total_space as f64 * 100.0,
         };
 
         let disk = Disk {
             name: sysinfo_disk.name().to_string_lossy().into_owned(),
             mount_point: sysinfo_disk.mount_point().to_string_lossy().into_owned(),
             total_space,
+            used_space,
             available_space,
             usage_percent,
         };
@@ -43,6 +47,7 @@ impl From<&sysinfo::Disk> for Disk {
                 disk.mount_point,
                 disk.name,
                 disk.space.total = disk.total_space,
+                disk.space.used = disk.used_space,
                 disk.space.available = disk.available_space,
                 disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
                 "disk usage high"
@@ -52,6 +57,7 @@ impl From<&sysinfo::Disk> for Disk {
                 disk.mount_point,
                 disk.name,
                 disk.space.total = disk.total_space,
+                disk.space.used = disk.used_space,
                 disk.space.available = disk.available_space,
                 disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
                 "found disk"

let available_space = sysinfo_disk.available_space();
let usage_percent = if total_space > 0 {
(total_space - available_space) as f64 / total_space as f64 * 100.0
} else {
0.0
};

let disk = Disk {
name: sysinfo_disk.name().to_string_lossy().into_owned(),
mount_point: sysinfo_disk.mount_point().to_string_lossy().into_owned(),
total_space: sysinfo_disk.total_space(),
available_space: sysinfo_disk.available_space(),
total_space,
available_space,
usage_percent,
};
tracing::info!(
disk.mount_point,
disk.name,
disk.space.total = disk.total_space,
disk.space.available = disk.available_space,
"found disk"
);

if usage_percent >= 85.0 {
tracing::warn!(
disk.mount_point,
disk.name,
disk.space.total = disk.total_space,
disk.space.available = disk.available_space,
disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
"disk usage high"
);
} else {
tracing::info!(
disk.mount_point,
disk.name,
disk.space.total = disk.total_space,
disk.space.available = disk.available_space,
disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
"found disk"
);
}
disk
}
}
Loading