1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Fix ls: panicking on dangling symlink with --color=auto -l (#6346)

* Fixed unwrap being called on dereferenced dangling symlink

* Added test

* Switched to regex matching in test

* Remove unnecessary mkdir call

* Modified documentation of the test and added assertion of the colors

* Fixed a typo
This commit is contained in:
Anirban Halder 2024-05-12 00:37:44 +05:30 committed by GitHub
parent dddf9f26d6
commit 1a5639b7d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 5 deletions

View file

@ -3353,10 +3353,9 @@ fn color_name(
// use the optional target_symlink
// Use fn get_metadata_with_deref_opt instead of get_metadata() here because ls
// should not exit with an err, if we are unable to obtain the target_metadata
let md = get_metadata_with_deref_opt(target.p_buf.as_path(), path.must_dereference)
.unwrap_or_else(|_| target.get_metadata(out).unwrap().clone());
apply_style_based_on_metadata(path, Some(&md), ls_colors, style_manager, &name)
let md_res = get_metadata_with_deref_opt(target.p_buf.as_path(), path.must_dereference);
let md = md_res.or(path.p_buf.symlink_metadata());
apply_style_based_on_metadata(path, md.ok().as_ref(), ls_colors, style_manager, &name)
} else {
let md_option = path.get_metadata(out);
let symlink_metadata = path.p_buf.symlink_metadata().ok();

View file

@ -2,7 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc neee naaaaa nbcdef nfffff dired subdired tmpfs mdir COLORTERM mexe bcdef
// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc neee naaaaa nbcdef nfffff dired subdired tmpfs mdir COLORTERM mexe bcdef mfoo
#[cfg(any(unix, feature = "feat_selinux"))]
use crate::common::util::expected_result;
@ -1376,6 +1376,44 @@ fn test_ls_long_symlink_color() {
}
}
/// This test is for "ls -l --color=auto|--color=always"
/// We use "--color=always" as the colors are the same regardless of the color option being "auto" or "always"
/// tests whether the specific color of the target and the dangling_symlink are equal and checks
/// whether checks whether ls outputs the correct path for the symlink and the file it points to and applies the color code to it.
#[test]
fn test_ls_long_dangling_symlink_color() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir1");
at.symlink_dir("foo", "dir1/dangling_symlink");
let result = ts
.ucmd()
.arg("-l")
.arg("--color=always")
.arg("dir1/dangling_symlink")
.succeeds();
let stdout = result.stdout_str();
// stdout contains output like in the below sequence. We match for the color i.e. 01;36
// \x1b[0m\x1b[01;36mdir1/dangling_symlink\x1b[0m -> \x1b[01;36mfoo\x1b[0m
let color_regex = Regex::new(r"(\d\d;)\d\dm").unwrap();
// colors_vec[0] contains the symlink color and style and colors_vec[1] contains the color and style of the file the
// symlink points to.
let colors_vec: Vec<_> = color_regex
.find_iter(stdout)
.map(|color| color.as_str())
.collect();
assert_eq!(colors_vec[0], colors_vec[1]);
// constructs the string of file path with the color code
let symlink_color_name = colors_vec[0].to_owned() + "dir1/dangling_symlink\x1b";
let target_color_name = colors_vec[1].to_owned() + at.plus_as_string("foo\x1b").as_str();
assert!(stdout.contains(&symlink_color_name));
assert!(stdout.contains(&target_color_name));
}
#[test]
fn test_ls_long_total_size() {
let scene = TestScenario::new(util_name!());