Skip to content

Commit

Permalink
Unix get drive free space
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Oct 2, 2024
1 parent 629b69f commit 4b70700
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions platforms/unix/hardwareinfo/src/linux/drive.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use anyhow::{Context, Result};
use core::str;
use regex::Regex;
use std::{
collections::HashMap,
fmt::Display,
path::{Path, PathBuf},
process::Command,
sync::LazyLock,
};

Expand Down Expand Up @@ -289,3 +291,29 @@ impl Drive {
}
}
}

pub fn get_free_space(path: &PathBuf) -> u64 {
// Define the device path as a variable
let path_str = path.to_str().unwrap_or("").replace("/sys/block/", "/dev/");

// Construct the command string
let command = format!(
"lsblk -bno FSAVAIL,MOUNTPOINT {} | awk '{{sum += $1}} END {{print sum / (1024^3) \"\"}}'",
path_str
);

// Execute the command
let output = Command::new("sh").arg("-c").arg(&command).output();

// Check if the command was successful
if let Ok(output) = output {
// Print the command's standard output
if let Ok(result) = str::from_utf8(&output.stdout) {
return (result.trim().parse::<f64>().unwrap_or(0.0) * 1.074) as u64;
} else {
return 0;
}
} else {
return 0;
}
}
4 changes: 3 additions & 1 deletion platforms/unix/hardwareinfo/src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ pub fn linux_hardware_info(data: &mut Data) {
if !d.is_virtual {
let inner = &d.inner;

let drive_space = drive::get_free_space(path);

data.hw_info.system.storage.disks.push(CoresDisk {
name: inner.clone().model.unwrap_or("N/A".to_string()),
total_space: inner.clone().capacity().unwrap_or(1) / 1000 / 1000 / 1000,
free_space: 0,
free_space: drive_space,
throughput_read: 0.0,
throughput_write: 0.0,
data_read: 0.0,
Expand Down

0 comments on commit 4b70700

Please sign in to comment.