1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 03:26:18 +00:00

df: separate functions for two main modes of df

Split the code for getting a list of `Filesystem` objects into two
separate functions: one for getting the list of all filesystems (when
running `df` with no arguments) and one for getting only named
filesystems (when running `df` with one or more arguments). This does
not change the behavior of `df` only the organization of the code.
This commit is contained in:
Jeffrey Finkelstein 2022-03-13 14:51:12 -04:00
parent 6f3dcf5998
commit f4ca963dca

View file

@ -230,39 +230,49 @@ fn filter_mount_list(vmi: Vec<MountInfo>, opt: &Options) -> Vec<MountInfo> {
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<MountInfo> {
/// Get all currently mounted filesystems.
///
/// `opt` excludes certain filesystems from consideration; see
/// [`Options`] for more information.
fn get_all_filesystems(opt: &Options) -> Vec<Filesystem> {
// The list of all mounted filesystems.
//
// Filesystems excluded by the command-line options are
// not considered.
let mounts: Vec<MountInfo> = 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<P>(paths: &[P]) -> Vec<Filesystem>
where
P: AsRef<Path>,
{
// 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<MountInfo> = 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::<Vec<MountInfo>>()
.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::<Vec<_>>();
#[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<MountInfo> = 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<Filesystem> = op_mount_points
.into_iter()
.filter_map(Filesystem::new)
.collect();
let filesystems: Vec<Filesystem> = 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.
//