Fix device detection to properly parse lsblk output

- Handle lsblk tree symbols (├─, └─) in device parsing
- Extract base device names from partitions (nvme0n1p2 -> nvme0n1)
- Support both NVMe and traditional device naming schemes
- Fixes missing device lines in storage display
This commit is contained in:
Christoffer Martinsson 2025-10-23 19:16:33 +02:00
parent ad298ac70c
commit d193b90ba1

View File

@ -277,14 +277,54 @@ impl DiskCollector {
for line in output_str.lines() { for line in output_str.lines() {
let parts: Vec<&str> = line.split_whitespace().collect(); let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 && parts[1] == mount_point { if parts.len() >= 2 && parts[1] == mount_point {
let device_name = parts[0].trim(); // Remove tree symbols and extract device name (e.g., "├─nvme0n1p2" -> "nvme0n1p2")
return Ok(vec![device_name.to_string()]); let device_name = parts[0]
.trim_start_matches('├')
.trim_start_matches('└')
.trim_start_matches('─')
.trim();
// Extract base device name (e.g., "nvme0n1p2" -> "nvme0n1")
if let Some(base_device) = Self::extract_base_device(device_name) {
return Ok(vec![base_device]);
}
} }
} }
Ok(Vec::new()) Ok(Vec::new())
} }
/// Extract base device name from partition (e.g., "nvme0n1p2" -> "nvme0n1", "sda1" -> "sda")
fn extract_base_device(device_name: &str) -> Option<String> {
// Handle NVMe devices (nvme0n1p1 -> nvme0n1)
if device_name.starts_with("nvme") {
if let Some(p_pos) = device_name.find('p') {
return Some(device_name[..p_pos].to_string());
}
}
// Handle traditional devices (sda1 -> sda)
if device_name.len() > 1 {
let chars: Vec<char> = device_name.chars().collect();
let mut end_idx = chars.len();
// Find where the device name ends and partition number begins
for (i, &c) in chars.iter().enumerate().rev() {
if !c.is_ascii_digit() {
end_idx = i + 1;
break;
}
}
if end_idx > 0 && end_idx < chars.len() {
return Some(chars[..end_idx].iter().collect());
}
}
// If no partition detected, return as-is
Some(device_name.to_string())
}
/// Get directory size using du command (efficient for single directory) /// Get directory size using du command (efficient for single directory)
fn get_directory_size(&self, path: &str) -> Result<u64> { fn get_directory_size(&self, path: &str) -> Result<u64> {
let output = Command::new("du") let output = Command::new("du")