A little cleaner, split the line only once
@@ -869,14 +869,22 @@ pub fn get_all_data<P: AsRef<Path>>(file_path: P, size: usize) -> io::Result<Str
fn get_all_disks() -> Vec<Disk> {
let content = get_all_data("/proc/mounts", 16_385).unwrap_or_default();
- let disks = content.lines().filter(|line| {
+ content.lines()
+ .map(|line| {
let line = line.trim_start();
// mounts format
//http://man7.org/linux/man-pages/man5/fstab.5.html
//fs_spec<tab>fs_file<tab>fs_vfstype<tab>other fields
- let fields: Vec<&str> = line.split_whitespace().collect();
+ let mut fields = line.split_whitespace();
+ let fs_spec = fields.next().unwrap_or("");
+ let fs_file = fields.next().unwrap_or("");
+ let fs_vfstype = fields.next().unwrap_or("");
+ (fs_spec, fs_file, fs_vfstype)
+ })
+ .filter(|(fs_spec, fs_file, fs_vfstype)| {
+
// Check if fs_vfstype is one of our 'ignored' file systems
- let skip = match fields[2]{
+ let filtered = match *fs_vfstype{
"sysfs" => true, // pseudo file system for kernel objects
"proc" => true, // another pseudo file system
"tmpfs" => true,
@@ -887,33 +895,27 @@ fn get_all_disks() -> Vec<Disk> {
"iso9660" => true, // optical media
_ => false
};
- // check if fs_file is an 'ignored' mount point
- if skip ||
- fields[1].starts_with("/sys") ||
- fields[1].starts_with("/proc") ||
- fields[1].starts_with("/run") ||
- fields[1].starts_with("/dev") ||
- fields[0].starts_with("sunrpc")
- {
+
+ if filtered ||
+ fs_file.starts_with("/sys") || // check if fs_file is an 'ignored' mount point
+ fs_file.starts_with("/proc") ||
+ fs_file.starts_with("/run") ||
+ fs_file.starts_with("/dev") ||
+ fs_spec.starts_with("sunrpc"){
false
}
else{
true
}
- });
- let mut ret = vec![];
-
- for line in disks {
- let mut split = line.split(' ');
- if let (Some(name), Some(mountpt), Some(fs)) = (split.next(), split.next(), split.next()) {
- ret.push(disk::new(
- name.as_ref(),
- Path::new(mountpt),
- fs.as_bytes(),
- ));
- }
- }
- ret
+ })
+ .map(|(fs_spec, fs_file, fs_vfstype)|{
+ disk::new(
+ fs_spec.as_ref(),
+ Path::new(fs_file),
+ fs_vfstype.as_bytes()
+ )
+ })
+ .collect()
}
fn get_uptime() -> u64 {
