Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

df: Adds support for mount path prefix matching and input path #3161

Merged
merged 15 commits into from
Mar 11, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::iter::FromIterator;
#[cfg(unix)]
use std::mem;

#[cfg(windows)]
use std::path::Path;

use crate::table::{DisplayRow, Header, Row};
Expand Down Expand Up @@ -160,7 +159,7 @@ impl Filesystem {
}

/// Whether to display the mount info given the inclusion settings.
fn is_included(mi: &MountInfo, paths: &[String], opt: &Options) -> bool {
fn is_included(mi: &MountInfo, paths: &mut [String], opt: &Options) -> bool {
// Don't show remote filesystems if `--local` has been given.
if mi.remote && opt.show_local_fs {
return false;
Expand All @@ -176,13 +175,23 @@ fn is_included(mi: &MountInfo, paths: &[String], opt: &Options) -> bool {
return false;
}

// Don't show filesystems other than the ones specified on the
// command line, if any.
if !paths.is_empty() && !paths.contains(&mi.mount_dir) {
return false;
// Print all suitable paths if input is empty
if paths.is_empty() {
return true;
}

true
// Checks if mount dir path is prefix for any path
// This will also cover complete matches
for path in paths {
if path.starts_with(&mi.mount_dir) {
// once a path is matched with mount_path, path is cleared to avoid 2nd matches like
// `/` will match as prefix for all paths
path.clear();
return true;
}
}

false
}

/// Whether the mount info in `m2` should be prioritized over `m1`.
Expand Down Expand Up @@ -239,7 +248,7 @@ fn is_best(previous: &[MountInfo], mi: &MountInfo) -> bool {
///
/// 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: &mut [String], opt: &Options) -> Vec<MountInfo> {
let mut result = vec![];
for mi in vmi {
// TODO The running time of the `is_best()` function is linear
Expand All @@ -259,11 +268,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = usage();
let matches = uu_app().override_usage(&usage[..]).get_matches_from(args);

let paths: Vec<String> = matches
let mut paths: Vec<String> = matches
.values_of(OPT_PATHS)
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default();

// Canonicalize all paths and clear invalid paths
let mut tmp_path: &Path;
for path in &mut paths {
tmp_path = Path::new(&path);
if tmp_path.exists() {
*path = tmp_path
.canonicalize()
.unwrap_or_default()
.into_os_string()
.into_string()
.unwrap_or_default();
} else {
// Remove invalid paths
path.clear();
}
}
#[cfg(windows)]
{
if matches.is_present(OPT_INODES) {
Expand All @@ -274,8 +299,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let opt = Options::from(&matches);

let mounts = read_fs_list();
let data: Vec<Row> = filter_mount_list(mounts, &paths, &opt)
let mut mounts = read_fs_list();

// sorting mounts in order of decreasing length
// in prefix matching, only first match is considered. So if we have
// mounts['/','/boot',/boot/efi] and input_path is '/boot/efi'. Without sorting the selected
// mount path will be '/' and rest will be ignored, but correct selection is '/boot/efi'
mounts.sort_unstable_by_key(|k| std::cmp::Reverse(k.mount_dir.len()));
let data: Vec<Row> = filter_mount_list(mounts, &mut paths, &opt)
.into_iter()
.filter_map(Filesystem::new)
.filter(|fs| fs.usage.blocks != 0 || opt.show_all_fs || opt.show_listed_fs)
Expand Down