From 7107b395265f2c32538174a687188f393c4a1ca2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 30 Jan 2025 22:36:49 +0100 Subject: [PATCH] Fix linux missing disk entries when some information are not available --- src/unix/linux/disk.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/unix/linux/disk.rs b/src/unix/linux/disk.rs index babbd0839..72084e62f 100644 --- a/src/unix/linux/disk.rs +++ b/src/unix/linux/disk.rs @@ -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 { @@ -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 = 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); + } }