From 016d5e72ad6539da5f38634cab36b5ee6a17f96f Mon Sep 17 00:00:00 2001 From: kimono-koans <32370782+kimono-koans@users.noreply.github.com> Date: Tue, 11 Jan 2022 05:01:54 -0600 Subject: [PATCH] ls: Fix padding for dangling links in non-Long formats (#2856) * Fix padding for dangling links in non-long formats Co-authored-by: electricboogie <32370782+electricboogie@users.noreply.github.com> --- src/uu/ls/src/ls.rs | 29 +++++++++++++++++++++++------ tests/by-util/test_ls.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 7dbd0b571..6e6b1c559 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1678,9 +1678,25 @@ fn display_items(items: &[PathData], config: &Config, out: &mut BufWriter = items .iter() - .map(|i| display_file_name(i, config, prefix_context, out)) + .map(|i| display_file_name(i, config, prefix_context, longest_inode_len, out)) .collect::>() .into_iter(); @@ -1878,7 +1894,7 @@ fn display_item_long( ); } - let dfn = display_file_name(item, config, None, out).contents; + let dfn = display_file_name(item, config, None, 0, out).contents; let _ = writeln!( out, @@ -1888,7 +1904,7 @@ fn display_item_long( dfn, ); } else { - // this 'else' is expressly for the case of a dangling symlink + // this 'else' is expressly for the case of a dangling symlink/restricted file #[cfg(unix)] { if config.inode { @@ -1932,7 +1948,7 @@ fn display_item_long( let _ = write!(out, " {}", pad_right("?", padding.longest_uname_len)); } - let dfn = display_file_name(item, config, None, out).contents; + let dfn = display_file_name(item, config, None, 0, out).contents; let date_len = 12; let _ = writeln!( @@ -2174,6 +2190,7 @@ fn display_file_name( path: &PathData, config: &Config, prefix_context: Option, + longest_inode_len: usize, out: &mut BufWriter, ) -> Cell { // This is our return value. We start by `&path.display_name` and modify it along the way. @@ -2193,8 +2210,8 @@ fn display_file_name( { if config.inode && config.format != Format::Long { let inode = match path.md(out) { - Some(md) => get_inode(md), - None => "?".to_string(), + Some(md) => pad_left(&get_inode(md), longest_inode_len), + None => pad_left("?", longest_inode_len), }; // increment width here b/c name was given colors and name.width() is now the wrong // size for display diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index cf266db89..3b3316338 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -2457,6 +2457,36 @@ fn test_ls_dangling_symlinks() { .arg("temp_dir") .fails() .stdout_contains("l?????????"); + + #[cfg(unix)] + { + // Check padding is the same for real files and dangling links, in non-long formats + at.touch("temp_dir/real_file"); + + let real_file_res = scene.ucmd().arg("-Li1").arg("temp_dir").fails(); + let real_file_stdout_len = String::from_utf8(real_file_res.stdout().to_owned()) + .ok() + .unwrap() + .lines() + .nth(1) + .unwrap() + .strip_suffix("real_file") + .unwrap() + .len(); + + let dangle_file_res = scene.ucmd().arg("-Li1").arg("temp_dir").fails(); + let dangle_stdout_len = String::from_utf8(dangle_file_res.stdout().to_owned()) + .ok() + .unwrap() + .lines() + .next() + .unwrap() + .strip_suffix("dangle") + .unwrap() + .len(); + + assert_eq!(real_file_stdout_len, dangle_stdout_len); + } } #[test]