mirror of
https://github.com/posborne/rust-pstree.git
synced 2025-01-30 23:05:04 +01:00
Updates based on code review by @nastevens
This addresses the following feeback: Your code looks really good, and very Rustic. I only have a few comments: · In ProcessRecord you use isize for the pid and ppid. These aren’t really sizes, so I’d go with i32 · Line 102: since all you’re doing is panic!() on Err, you can just use .unwrap() · Line 107: you could use “filter_map” instead of filter and then “collect” the results. This eliminates the need for “mut records” on line 98 · Line 126: use “filter_map” · I’d add Cargo support · You could consider using a map type containing vector types for storing the ppid->pid translation. That would improve performance since the record list wouldn’t getting iterated over multiple times. Unfortunately Rust doesn’t have a multimap yet (see https://github.com/rust-lang/rfcs/issues/784), otherwise you could just use that! Using a map type for the lookup is not supported with this but will be evaluated.
This commit is contained in:
parent
c28b00b769
commit
5823d36e84
1 changed files with 20 additions and 26 deletions
40
pstree.rs
40
pstree.rs
|
@ -39,8 +39,8 @@ use std::old_io::BufferedReader;
|
||||||
#[derive(Clone,Debug)]
|
#[derive(Clone,Debug)]
|
||||||
struct ProcessRecord {
|
struct ProcessRecord {
|
||||||
name: String,
|
name: String,
|
||||||
pid: isize,
|
pid: i32,
|
||||||
ppid: isize,
|
ppid: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone,Debug)]
|
#[derive(Clone,Debug)]
|
||||||
|
@ -65,8 +65,8 @@ impl ProcessTreeNode {
|
||||||
// Given a status file path, return a hashmap with the following form:
|
// Given a status file path, return a hashmap with the following form:
|
||||||
// pid -> ProcessRecord
|
// pid -> ProcessRecord
|
||||||
fn get_process_record(status_path: &Path) -> Option<ProcessRecord> {
|
fn get_process_record(status_path: &Path) -> Option<ProcessRecord> {
|
||||||
let mut pid : Option<isize> = None;
|
let mut pid : Option<i32> = None;
|
||||||
let mut ppid : Option<isize> = None;
|
let mut ppid : Option<i32> = None;
|
||||||
let mut name : Option<String> = None;
|
let mut name : Option<String> = None;
|
||||||
|
|
||||||
let mut status_file = BufferedReader::new(File::open(status_path));
|
let mut status_file = BufferedReader::new(File::open(status_path));
|
||||||
|
@ -95,41 +95,35 @@ fn get_process_record(status_path: &Path) -> Option<ProcessRecord> {
|
||||||
|
|
||||||
// build a simple struct (ProcessRecord) for each process
|
// build a simple struct (ProcessRecord) for each process
|
||||||
fn get_process_records() -> Vec<ProcessRecord> {
|
fn get_process_records() -> Vec<ProcessRecord> {
|
||||||
let mut records : Vec<ProcessRecord> = Vec::new();
|
|
||||||
let proc_directory = Path::new("/proc");
|
let proc_directory = Path::new("/proc");
|
||||||
|
|
||||||
// find potential process directories under /proc
|
// find potential process directories under /proc
|
||||||
let proc_directory_contents = match fs::readdir(&proc_directory) {
|
let proc_directory_contents = fs::readdir(&proc_directory).unwrap();
|
||||||
Err(why) => panic!("{}", why.desc),
|
proc_directory_contents.iter().filter_map(|entry| {
|
||||||
Ok(res) => res
|
if entry.is_dir() {
|
||||||
};
|
|
||||||
|
|
||||||
for entry in proc_directory_contents.iter().filter(|entry| entry.is_dir()) {
|
|
||||||
let status_path = entry.join("status");
|
let status_path = entry.join("status");
|
||||||
if status_path.exists() {
|
if status_path.exists() {
|
||||||
match get_process_record(&status_path) {
|
return get_process_record(&status_path)
|
||||||
Some(record) => {
|
|
||||||
records.push(record)
|
|
||||||
},
|
|
||||||
None => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
None
|
||||||
records
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn populate_node(node : &mut ProcessTreeNode, records: &Vec<ProcessRecord>) {
|
fn populate_node(node : &mut ProcessTreeNode, records: &Vec<ProcessRecord>) {
|
||||||
// populate the node by finding its children... recursively
|
// populate the node by finding its children... recursively
|
||||||
let pid = node.record.pid; // avoid binding node as immutable in closure
|
let pid = node.record.pid; // avoid binding node as immutable in closure
|
||||||
node.children.extend(
|
node.children.extend(
|
||||||
records.iter()
|
records.iter().filter_map(|record| {
|
||||||
.filter(|record| record.ppid == pid)
|
if record.ppid == pid {
|
||||||
.map(|record| {
|
|
||||||
let mut child = ProcessTreeNode::new(record);
|
let mut child = ProcessTreeNode::new(record);
|
||||||
populate_node(&mut child, records);
|
populate_node(&mut child, records);
|
||||||
child
|
Some(child)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_process_tree() -> ProcessTree {
|
fn build_process_tree() -> ProcessTree {
|
||||||
|
|
Loading…
Reference in a new issue