Skip to content

Commit

Permalink
Fix linux missing disk entries when some information are not available
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jan 30, 2025
1 parent c25010a commit 7107b39
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ impl DiskStat {
// 3rd field
let name = iter.nth(2).map(ToString::to_string)?;
// 6th field
let sectors_read = iter.nth(2).and_then(|v| u64::from_str(v).ok())?;
let sectors_read = iter.nth(2).and_then(|v| u64::from_str(v).ok()).unwrap_or(0);
// 10th field
let sectors_written = iter.nth(3).and_then(|v| u64::from_str(v).ok())?;
let sectors_written = iter.nth(3).and_then(|v| u64::from_str(v).ok()).unwrap_or(0);
Some((
name,
Self {
Expand Down Expand Up @@ -583,4 +583,32 @@ mod test {

assert_eq!(data, expected_data);
}

#[test]
fn disk_entry_with_less_information() {
let file_content = "\
systemd-1 /efi autofs rw,relatime,fd=181,pgrp=1,timeout=120,minproto=5,maxproto=5,direct,pipe_ino=8311 0 0
/dev/nvme0n1p1 /efi vfat rw,nosuid,nodev,noexec,relatime,nosymfollow,fmask=0077,dmask=0077 0 0
";

let data = disk_stats_inner(file_content);
let expected_data: HashMap<String, DiskStat> = HashMap::from([
(
"autofs".to_string(),
DiskStat {
sectors_read: 0,
sectors_written: 0,
},
),
(
"vfat".to_string(),
DiskStat {
sectors_read: 0,
sectors_written: 0,
},
),
]);

assert_eq!(data, expected_data);
}
}

0 comments on commit 7107b39

Please sign in to comment.