Skip to content

Commit 428cb2b

Browse files
committed
process: Add helper for stat parsing
This pattern is already used twice and more will come in next commit.
1 parent 9e28ec2 commit 428cb2b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ impl ProcessInformation {
274274
Rc::clone(&result)
275275
}
276276

277+
fn get_numeric_stat_field(&mut self, index: usize) -> Result<u64, io::Error> {
278+
self.stat()
279+
.get(index)
280+
.ok_or(io::ErrorKind::InvalidData)?
281+
.parse::<u64>()
282+
.map_err(|_| io::ErrorKind::InvalidData.into())
283+
}
284+
277285
/// Fetch start time from [ProcessInformation::cached_stat]
278286
///
279287
/// - [The /proc Filesystem: Table 1-4](https://docs.kernel.org/filesystems/proc.html#id10)
@@ -284,12 +292,7 @@ impl ProcessInformation {
284292

285293
// Kernel doc: https://docs.kernel.org/filesystems/proc.html#process-specific-subdirectories
286294
// Table 1-4
287-
let time = self
288-
.stat()
289-
.get(21)
290-
.ok_or(io::ErrorKind::InvalidData)?
291-
.parse::<u64>()
292-
.map_err(|_| io::ErrorKind::InvalidData)?;
295+
let time = self.get_numeric_stat_field(21)?;
293296

294297
self.cached_start_time = Some(time);
295298

@@ -299,11 +302,7 @@ impl ProcessInformation {
299302
pub fn ppid(&mut self) -> Result<u64, io::Error> {
300303
// the PPID is the fourth field in /proc/<PID>/stat
301304
// (https://www.kernel.org/doc/html/latest/filesystems/proc.html#id10)
302-
self.stat()
303-
.get(3)
304-
.ok_or(io::ErrorKind::InvalidData)?
305-
.parse::<u64>()
306-
.map_err(|_| io::ErrorKind::InvalidData.into())
305+
self.get_numeric_stat_field(3)
307306
}
308307

309308
fn get_uid_or_gid_field(&mut self, field: &str, index: usize) -> Result<u32, io::Error> {

0 commit comments

Comments
 (0)