Mercurial > lbo > hg > myi3stat
view src/helper.rs @ 21:229a4f04038c
Fix critical bug in procfs parsers (helper.rs)
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 14 Feb 2016 17:54:13 +0100 |
parents | c2dc7a85a0e3 |
children | 0ab7f88b225f |
line wrap: on
line source
#![allow(dead_code)] use std::fs; use std::io::Read; /// Reads and returns a full procfs file. /// Example: /// /// read_procfs_file("/net/dev") pub fn read_procfs_file(path: String) -> Option<String> { let mut file; let size; let mut fullpath = String::from("/proc/"); fullpath.push_str(&path); match fs::OpenOptions::new().read(true).open(fullpath) { Err(_) => return None, Ok(f) => file = f, } match file.metadata() { Err(_) => return None, Ok(md) => size = md.len() as usize, } let mut buf = String::with_capacity(size); match file.read_to_string(&mut buf) { Err(_) => None, Ok(_) => Some(buf), } } pub fn get_procfs_file_lines(path: String) -> Option<Vec<String>> { match read_procfs_file(path) { None => None, Some(s) => Some(s.lines().map(String::from).collect()), } } pub fn commaseparated_to_vec(s: String) -> Vec<String> { s.split(",").map(String::from).collect() }