diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-01-31 20:21:34 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-01-31 22:48:44 +0100 |
commit | 4ca702e1ec9a820c89ba68b97b42509dfce8059d (patch) | |
tree | 24d3d3e83bbbae28f4708dc1a54a3b6ae3492af1 /src | |
parent | 7f0d02d82a0ece8397490e44249ecd4fddada75a (diff) | |
download | rebel-4ca702e1ec9a820c89ba68b97b42509dfce8059d.tar rebel-4ca702e1ec9a820c89ba68b97b42509dfce8059d.zip |
unshare: improve implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/unshare.rs | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/src/unshare.rs b/src/unshare.rs index 2aa2b09..bb72db9 100644 --- a/src/unshare.rs +++ b/src/unshare.rs @@ -21,7 +21,7 @@ struct SubIDRange { } fn parse_pid(s: &OsStr) -> Option<libc::uid_t> { - s.to_str().and_then(|s| s.parse::<libc::uid_t>().ok()) + s.to_str().and_then(|s| s.parse().ok()) } fn parse_id_range(line: Vec<u8>, uid: &OsStr, username: Option<&OsStr>) -> Option<SubIDRange> { @@ -33,11 +33,10 @@ fn parse_id_range(line: Vec<u8>, uid: &OsStr, username: Option<&OsStr>) -> Optio return None; } - if let (Some(start), Some(count)) = (parse_pid(parts[1]), parse_pid(parts[2])) { - Some(SubIDRange { start, count }) - } else { - None - } + let start = parse_pid(parts[1])?; + let count = parse_pid(parts[2])?; + + Some(SubIDRange { start, count }) } fn read_id_ranges(filename: &Path) -> Result<Vec<SubIDRange>> { @@ -50,15 +49,10 @@ fn read_id_ranges(filename: &Path) -> Result<Vec<SubIDRange>> { let file = File::open(filename)?; let lines = io::BufReader::new(file).split(b'\n'); - let mut ranges = Vec::new(); - - for line in lines { - if let Some(range) = parse_id_range(line?, &uidstr, usernamestr) { - ranges.push(range); - } - } - - Ok(ranges) + lines + .map(|line| Ok(parse_id_range(line?, &uidstr, usernamestr))) + .filter_map(Result::transpose) + .collect() } #[derive(Debug)] |