From d90389ef62ba08e8f83afb4fc10c88dea5fd5829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Thu, 6 Oct 2022 13:58:02 +0200 Subject: [PATCH] Simplify parse_maps() Remove one indentation level by forwarding potential errors. --- src/proc.rs | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/proc.rs b/src/proc.rs index 51c9162..7029376 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -370,35 +370,31 @@ impl Process { } #[cfg(all(feature = "maps", target_os = "linux"))] pub fn parse_maps(pid: usize) -> Result, Error> { - let mut maps: Vec = Vec::new(); - if let Ok(maps_str) = fs::read_to_string(format!("/proc/{}/maps", pid)) + let mut maps = Vec::new(); + for line in fs::read_to_string(format!("/proc/{}/maps", pid))?.lines() { - for line in maps_str.lines() { - let mut split_line = line.split_whitespace(); - let (start_str, end_str) = split_line - .next() - .ok_or(ErrorKind::InvalidData)? - .split_once('-') - .ok_or(ErrorKind::InvalidData)?; - let region = Region::new( - usize::from_str_radix(start_str, 16).unwrap_or(0), - usize::from_str_radix(end_str, 16).unwrap_or(0), - ); - let flags = MapFlags::new( - split_line.next().ok_or(ErrorKind::InvalidData)?, - ); - split_line.next(); // skip offset - split_line.next(); // skip dev - split_line.next(); // skip inode - let pathname = - Some(split_line.collect::>().join(" ")) - .filter(|x| !x.is_empty()) - .map(PathBuf::from); - maps.push(MapEntry { region, flags, pathname }); - } - return Ok(maps); + let mut split_line = line.split_whitespace(); + let (start_str, end_str) = split_line + .next() + .ok_or(ErrorKind::InvalidData)? + .split_once('-') + .ok_or(ErrorKind::InvalidData)?; + let region = Region::new( + usize::from_str_radix(start_str, 16).unwrap_or(0), + usize::from_str_radix(end_str, 16).unwrap_or(0), + ); + let flags = MapFlags::new( + split_line.next().ok_or(ErrorKind::InvalidData)?, + ); + split_line.next(); // skip offset + split_line.next(); // skip dev + split_line.next(); // skip inode + let pathname = Some(split_line.collect::>().join(" ")) + .filter(|x| !x.is_empty()) + .map(PathBuf::from); + maps.push(MapEntry { region, flags, pathname }); } - Err(Error::last_os_error()) + Ok(maps) } #[cfg(all(feature = "maps", target_os = "windows"))]