Skip to content

Commit

Permalink
Changed sorting from reverse lexicographical to decreasing order of s…
Browse files Browse the repository at this point in the history
…tring length. Added comments for explaining sorting and path prefix matching
  • Loading branch information
crazystylus committed Feb 19, 2022
1 parent 5b79c80 commit 90ee693
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,17 @@ fn is_included(mi: &MountInfo, paths: &mut [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) {
// Print all suitable paths if input is empty
if paths.is_empty() {
return 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) {
// Remove path so that recursive parent paths like "\" don't get added
// 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;
}
Expand Down Expand Up @@ -300,8 +301,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let mut mounts = read_fs_list();

// Sort mounts in desc ordered lexicographically
mounts.sort_by(|a, b| b.mount_dir.cmp(&a.mount_dir));
// 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)
Expand Down

0 comments on commit 90ee693

Please sign in to comment.