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 2 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
45 changes: 36 additions & 9 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 @@ -178,11 +177,20 @@ fn is_included(mi: &MountInfo, paths: &[String], opt: &Options) -> bool {

// 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;
if paths.is_empty() || paths.contains(&mi.mount_dir) {
return true;
}

true
// Checks if mount dir path is prefix for any path
for path in paths {
if path.starts_with(&mi.mount_dir) {
// Remove path so that recursive parent paths like "\" don't get added
path.clear();
return true;
}
}

false
}

/// Whether the mount info in `m2` should be prioritized over `m1`.
Expand Down Expand Up @@ -239,7 +247,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 +267,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 +298,11 @@ 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();

// Sort mounts in desc ordered lexicographically
mounts.sort_by(|a, b| b.mount_dir.cmp(&a.mount_dir));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why sort here? On my system (Ubuntu), the output of df is not lexicographically ordered:

$ df --output=source | tail -n +2 | head -n4
udev
tmpfs
/dev/mapper/vgubuntu-root
tmpfs

See also my comment here: #3086 (comment)

Copy link
Contributor Author

@crazystylus crazystylus Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan is to sort in the order of decreasing mount path length, so that when I do a prefix matching, the longest matching mount path is selected first and rest mount paths are ignored. For example if we have mount paths [\,\root] and we have input /root/docs then we only need to output /root and / needs to be skipped. Sorting helps to assure this part.

Sorting in reverse/desc lexicographical order also works here as longest path will come first, but let me change to simple sort by length.

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