From 9b9d369298193a72a45888a5c4a165432e70840c Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Wed, 29 Jun 2022 10:41:22 +0200 Subject: [PATCH] df: fix output if input path is device name Fixes #3246 --- src/uu/df/src/filesystem.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/uu/df/src/filesystem.rs b/src/uu/df/src/filesystem.rs index 00b81007317..aac63ac9dee 100644 --- a/src/uu/df/src/filesystem.rs +++ b/src/uu/df/src/filesystem.rs @@ -67,8 +67,17 @@ where } else { path.as_ref().to_path_buf() }; - let matches = mounts.iter().filter(|mi| path.starts_with(&mi.mount_dir)); - matches.max_by_key(|mi| mi.mount_dir.len()) + + let maybe_mount_point = mounts + .iter() + .find(|mi| mi.dev_name.eq(&path.to_string_lossy())); + + maybe_mount_point.or_else(|| { + mounts + .iter() + .filter(|mi| path.starts_with(&mi.mount_dir)) + .max_by_key(|mi| mi.mount_dir.len()) + }) } impl Filesystem { @@ -199,5 +208,14 @@ mod tests { let mounts = [mount_info("/foo/bar")]; assert!(mount_info_from_path(&mounts, "/foo/baz", false).is_none()); } + + #[test] + fn test_dev_name_match() { + let mut mount_info = mount_info("/foo"); + mount_info.dev_name = "/dev/sda2".to_string(); + let mounts = [mount_info]; + let actual = mount_info_from_path(&mounts, "/dev/sda2", false).unwrap(); + assert!(mount_info_eq(actual, &mounts[0])); + } } }