Skip to content

Commit 5a432e3

Browse files
committed
process: Add name() method
Don't leak implementation details of Linux proc interfaces to users of the module.
1 parent 6dd29c5 commit 5a432e3

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/uu/pgrep/src/pgrep.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4545
// pgrep from procps-ng outputs the process name inside square brackets
4646
// if /proc/<PID>/cmdline is empty
4747
if it.cmdline.is_empty() {
48-
format!("{} [{}]", it.pid, it.clone().status().get("Name").unwrap())
48+
format!("{} [{}]", it.pid, it.clone().name().unwrap())
4949
} else {
5050
format!("{} {}", it.pid, it.cmdline)
5151
}
5252
})
5353
.collect()
5454
} else if matches.get_flag("list-name") {
5555
pids.into_iter()
56-
.map(|it| format!("{} {}", it.pid, it.clone().status().get("Name").unwrap()))
56+
.map(|it| format!("{} {}", it.pid, it.clone().name().unwrap()))
5757
.collect()
5858
} else {
5959
pids.into_iter().map(|it| format!("{}", it.pid)).collect()

src/uu/pgrep/src/process.rs

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

277+
pub fn name(&mut self) -> Result<String, io::Error> {
278+
self.status()
279+
.get("Name")
280+
.cloned()
281+
.ok_or(io::ErrorKind::InvalidData.into())
282+
}
283+
277284
fn get_numeric_stat_field(&mut self, index: usize) -> Result<u64, io::Error> {
278285
self.stat()
279286
.get(index)

src/uu/pgrep/src/process_matcher.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,11 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
208208
_ => true,
209209
};
210210

211-
let binding = pid.status();
212-
let name = binding.get("Name").unwrap();
211+
let name = pid.name().unwrap();
213212
let name = if settings.ignore_case {
214213
name.to_lowercase()
215214
} else {
216-
name.into()
215+
name
217216
};
218217
let pattern_matched = {
219218
let want = if settings.full {

0 commit comments

Comments
 (0)