diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 8f81b3efe..338648e10 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -230,39 +230,49 @@ fn filter_mount_list(vmi: Vec, opt: &Options) -> Vec { result } -/// Assign 1 `MountInfo` entry to each path -/// `lofs` entries are skipped and dummy mount points are skipped -/// Only the longest matching prefix for that path is considered -/// `lofs` is for Solaris style loopback filesystem and is present in Solaris and FreeBSD. -/// It works similar to symlinks -fn get_point_list(vmi: &[MountInfo], paths: &[String]) -> Vec { +/// Get all currently mounted filesystems. +/// +/// `opt` excludes certain filesystems from consideration; see +/// [`Options`] for more information. +fn get_all_filesystems(opt: &Options) -> Vec { + // The list of all mounted filesystems. + // + // Filesystems excluded by the command-line options are + // not considered. + let mounts: Vec = filter_mount_list(read_fs_list(), opt); + + // Convert each `MountInfo` into a `Filesystem`, which contains + // both the mount information and usage information. + mounts.into_iter().filter_map(Filesystem::new).collect() +} + +/// For each path, get the filesystem that contains that path. +fn get_named_filesystems

(paths: &[P]) -> Vec +where + P: AsRef, +{ + // The list of all mounted filesystems. + // + // Filesystems marked as `dummy` or of type "lofs" are not + // considered. The "lofs" filesystem is a loopback + // filesystem present on Solaris and FreeBSD systems. It + // is similar to a symbolic link. + let mounts: Vec = read_fs_list() + .into_iter() + .filter(|mi| mi.fs_type != "lofs" && !mi.dummy) + .collect(); + + // Convert each path into a `Filesystem`, which contains + // both the mount information and usage information. paths .iter() - .map(|p| { - vmi.iter() - .filter(|mi| mi.fs_type.ne("lofs")) - .filter(|mi| !mi.dummy) - .filter(|mi| p.starts_with(&mi.mount_dir)) - .max_by_key(|mi| mi.mount_dir.len()) - .unwrap() - .clone() - }) - .collect::>() + .filter_map(|p| Filesystem::from_path(&mounts, p)) + .collect() } #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().get_matches_from(args); - - // Canonicalize the input_paths and then convert to string - let paths = matches - .values_of(OPT_PATHS) - .unwrap_or_default() - .map(Path::new) - .filter_map(|v| v.canonicalize().ok()) - .filter_map(|v| v.into_os_string().into_string().ok()) - .collect::>(); - #[cfg(windows)] { if matches.is_present(OPT_INODES) { @@ -273,21 +283,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let opt = Options::from(&matches).map_err(|e| USimpleError::new(1, format!("{}", e)))?; - let mounts = read_fs_list(); - - let op_mount_points: Vec = if paths.is_empty() { - // Get all entries - filter_mount_list(mounts, &opt) - } else { - // Get Point for each input_path - get_point_list(&mounts, &paths) - }; - // Get the list of filesystems to display in the output table. - let filesystems: Vec = op_mount_points - .into_iter() - .filter_map(Filesystem::new) - .collect(); + let filesystems: Vec = match matches.values_of(OPT_PATHS) { + None => get_all_filesystems(&opt), + Some(paths) => { + let paths: Vec<&str> = paths.collect(); + get_named_filesystems(&paths) + } + }; // The running total of filesystem sizes and usage. //