Skip to content

Commit

Permalink
Simplify parse_maps()
Browse files Browse the repository at this point in the history
Remove one indentation level by forwarding potential errors.
  • Loading branch information
cgzones authored and etke committed Oct 8, 2022
1 parent 42bbe24 commit d90389e
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,35 +370,31 @@ impl Process {
}
#[cfg(all(feature = "maps", target_os = "linux"))]
pub fn parse_maps(pid: usize) -> Result<Vec<MapEntry>, Error> {
let mut maps: Vec<MapEntry> = 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::<Vec<&str>>().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::<Vec<&str>>().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"))]
Expand Down

0 comments on commit d90389e

Please sign in to comment.