mirror of
https://github.com/posborne/rust-pstree.git
synced 2025-01-12 23:36:26 +01:00
nightlies: Update code to build/run with latest 1.0.0 compiler
This commit is contained in:
parent
4053023568
commit
c141feddd8
1 changed files with 39 additions and 25 deletions
64
pstree.rs
64
pstree.rs
|
@ -27,25 +27,29 @@
|
||||||
// of different items, notably the process name and its parent process id (ppid).
|
// of different items, notably the process name and its parent process id (ppid).
|
||||||
// And with that information, we can build the process tree.
|
// And with that information, we can build the process tree.
|
||||||
|
|
||||||
use std::io::fs::PathExtensions;
|
#![feature(old_io)]
|
||||||
use std::io::fs;
|
#![feature(old_path)]
|
||||||
use std::io::File;
|
|
||||||
use std::io::BufferedReader;
|
|
||||||
|
|
||||||
#[deriving(Clone,Show)]
|
use std::old_io::fs::PathExtensions;
|
||||||
|
use std::old_io::fs;
|
||||||
|
use std::old_io::File;
|
||||||
|
use std::old_io::BufferedReader;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone,Debug)]
|
||||||
struct ProcessRecord {
|
struct ProcessRecord {
|
||||||
name: String,
|
name: String,
|
||||||
pid: int,
|
pid: isize,
|
||||||
ppid: int
|
ppid: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deriving(Clone,Show)]
|
#[derive(Clone,Debug)]
|
||||||
struct ProcessTreeNode {
|
struct ProcessTreeNode {
|
||||||
record: ProcessRecord, // the node owns the associated record
|
record: ProcessRecord, // the node owns the associated record
|
||||||
children: Vec<ProcessTreeNode>, // nodes own their children
|
children: Vec<ProcessTreeNode>, // nodes own their children
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deriving(Clone,Show)]
|
#[derive(Clone,Debug)]
|
||||||
struct ProcessTree {
|
struct ProcessTree {
|
||||||
root: ProcessTreeNode, // tree owns ref to root node
|
root: ProcessTreeNode, // tree owns ref to root node
|
||||||
}
|
}
|
||||||
|
@ -61,21 +65,21 @@ 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<int> = None;
|
let mut pid : Option<isize> = None;
|
||||||
let mut ppid : Option<int> = None;
|
let mut ppid : Option<isize> = 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));
|
||||||
for line in status_file.lines() {
|
for line in status_file.lines() {
|
||||||
let unwrapped = line.unwrap(); // need a new lifeline
|
let unwrapped = line.unwrap(); // need a new lifeline
|
||||||
let parts : Vec<&str> = unwrapped.as_slice().splitn(2, ':').collect();
|
let parts : Vec<&str> = unwrapped[..].splitn(2, ':').collect();
|
||||||
if parts.len() == 2 {
|
if parts.len() == 2 {
|
||||||
let key = parts[0].trim();
|
let key = parts[0].trim();
|
||||||
let value = parts[1].trim();
|
let value = parts[1].trim();
|
||||||
match key {
|
match key {
|
||||||
"Name" => name = Some(value.to_string()),
|
"Name" => name = Some(value.to_string()),
|
||||||
"Pid" => pid = from_str(value),
|
"Pid" => pid = value.parse().ok(),
|
||||||
"PPid" => ppid = from_str(value),
|
"PPid" => ppid = value.parse().ok(),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,7 +100,7 @@ fn get_process_records() -> Vec<ProcessRecord> {
|
||||||
|
|
||||||
// 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 = match fs::readdir(&proc_directory) {
|
||||||
Err(why) => fail!("{}", why.desc),
|
Err(why) => panic!("{}", why.desc),
|
||||||
Ok(res) => res
|
Ok(res) => res
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -104,7 +108,9 @@ fn get_process_records() -> Vec<ProcessRecord> {
|
||||||
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) {
|
match get_process_record(&status_path) {
|
||||||
Some(record) => records.push(record),
|
Some(record) => {
|
||||||
|
records.push(record)
|
||||||
|
},
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,18 +121,26 @@ fn get_process_records() -> Vec<ProcessRecord> {
|
||||||
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
|
||||||
for record in records.iter().filter(|record| record.ppid == pid) {
|
node.children.extend(
|
||||||
let mut child = ProcessTreeNode::new(record);
|
records.iter()
|
||||||
populate_node(&mut child, records);
|
.filter(|record| record.ppid == pid)
|
||||||
node.children.push(child);
|
.map(|record| {
|
||||||
}
|
let mut child = ProcessTreeNode::new(record);
|
||||||
|
populate_node(&mut child, records);
|
||||||
|
child
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_process_tree() -> ProcessTree {
|
fn build_process_tree() -> ProcessTree {
|
||||||
let records = get_process_records();
|
let records = get_process_records();
|
||||||
let mut tree = ProcessTree {
|
let mut tree = ProcessTree {
|
||||||
root : ProcessTreeNode::new(
|
root : ProcessTreeNode::new(
|
||||||
&ProcessRecord { name: "/".to_string(), pid: 0, ppid: -1 })
|
&ProcessRecord {
|
||||||
|
name: "/".to_string(),
|
||||||
|
pid: 0,
|
||||||
|
ppid: -1
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
// recursively populate all nodes in the tree starting from root (pid 0)
|
// recursively populate all nodes in the tree starting from root (pid 0)
|
||||||
|
@ -137,10 +151,10 @@ fn build_process_tree() -> ProcessTree {
|
||||||
tree
|
tree
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_node(node : &ProcessTreeNode, indent_level : int) {
|
fn print_node(node : &ProcessTreeNode, indent_level : i32) {
|
||||||
// print indentation
|
// print indentation
|
||||||
for _ in range(0, indent_level * 2) {
|
for _ in (0..indent_level) {
|
||||||
print!(" ");
|
print!(" ");
|
||||||
}
|
}
|
||||||
println!("- {} #{}", node.record.name, node.record.pid);
|
println!("- {} #{}", node.record.name, node.record.pid);
|
||||||
for child in node.children.iter() {
|
for child in node.children.iter() {
|
||||||
|
|
Loading…
Reference in a new issue