1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

df: refactor filter_mount_list() to be more flat

Use a `for` loop in the `filter_mount_list()` function to make the
filtering logic easier to read.
This commit is contained in:
Jeffrey Finkelstein 2022-02-06 11:07:19 -05:00
parent 1f7c08d87b
commit 572b2e032c

View file

@ -161,35 +161,52 @@ impl Filesystem {
} }
} }
/// Keep only the specified subset of [`MountInfo`] instances.
///
/// If `paths` is non-empty, this function excludes any [`MountInfo`]
/// that is not mounted at the specified path.
///
/// The `opt` argument specifies a variety of ways of excluding
/// [`MountInfo`] instances; see [`Options`] for more information.
///
/// Finally, if there are duplicate entries, the one with the shorter
/// path is kept.
fn filter_mount_list(vmi: Vec<MountInfo>, paths: &[String], opt: &Options) -> Vec<MountInfo> { fn filter_mount_list(vmi: Vec<MountInfo>, paths: &[String], opt: &Options) -> Vec<MountInfo> {
vmi.into_iter() let mut mount_info_by_id = HashMap::<String, Cell<MountInfo>>::new();
.filter_map(|mi| { for mi in vmi {
if (mi.remote && opt.show_local_fs) // Don't show remote filesystems if `--local` has been given.
|| (mi.dummy && !opt.show_all_fs && !opt.show_listed_fs) if mi.remote && opt.show_local_fs {
|| !opt.fs_selector.should_select(&mi.fs_type) continue;
{
None
} else {
if paths.is_empty() {
// No path specified
return Some((mi.dev_id.clone(), mi));
} }
if paths.contains(&mi.mount_dir) {
// One or more paths have been provided // Don't show pseudo filesystems unless `--all` has been given.
Some((mi.dev_id.clone(), mi)) if mi.dummy && !opt.show_all_fs && !opt.show_listed_fs {
} else { continue;
// Not a path we want to see
None
} }
// Don't show filesystems if they have been explicitly excluded.
if !opt.fs_selector.should_select(&mi.fs_type) {
continue;
} }
})
.fold( // Don't show filesystems other than the ones specified on the
HashMap::<String, Cell<MountInfo>>::new(), // command line, if any.
|mut acc, (id, mi)| { if !paths.is_empty() && !paths.contains(&mi.mount_dir) {
continue;
}
// If the device ID has not been encountered yet, just store it.
let id = mi.dev_id.clone();
#[allow(clippy::map_entry)] #[allow(clippy::map_entry)]
{ if !mount_info_by_id.contains_key(&id) {
if acc.contains_key(&id) { mount_info_by_id.insert(id, Cell::new(mi));
let seen = acc[&id].replace(mi.clone()); continue;
}
// Otherwise, if we have seen the current device ID before,
// then check if we need to update it or keep the previously
// seen one.
let seen = mount_info_by_id[&id].replace(mi.clone());
let target_nearer_root = seen.mount_dir.len() > mi.mount_dir.len(); let target_nearer_root = seen.mount_dir.len() > mi.mount_dir.len();
// With bind mounts, prefer items nearer the root of the source // With bind mounts, prefer items nearer the root of the source
let source_below_root = !seen.mount_root.is_empty() let source_below_root = !seen.mount_root.is_empty()
@ -207,18 +224,16 @@ fn filter_mount_list(vmi: Vec<MountInfo>, paths: &[String], opt: &Options) -> Ve
environments for example. */ environments for example. */
|| seen.mount_dir != mi.mount_dir) || seen.mount_dir != mi.mount_dir)
{ {
acc[&id].replace(seen); mount_info_by_id[&id].replace(seen);
} }
} else {
acc.insert(id, Cell::new(mi));
} }
acc
} // Take ownership of the `MountInfo` instances and collect them
}, // into a `Vec`.
) mount_info_by_id
.into_iter() .into_values()
.map(|ent| ent.1.into_inner()) .map(|m| m.into_inner())
.collect::<Vec<_>>() .collect()
} }
/// Convert `value` to a human readable string based on `base`. /// Convert `value` to a human readable string based on `base`.