1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

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>
This commit is contained in:
kimono-koans 2022-01-11 05:01:54 -06:00 committed by GitHub
parent f60c36f242
commit 016d5e72ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 6 deletions

View file

@ -1678,9 +1678,25 @@ fn display_items(items: &[PathData], config: &Config, out: &mut BufWriter<Stdout
None
};
#[cfg(not(unix))]
let longest_inode_len = 1;
#[cfg(unix)]
let mut longest_inode_len = 1;
#[cfg(unix)]
if config.inode {
for item in items {
let inode_len = if let Some(md) = item.md(out) {
display_inode(md).len()
} else {
continue;
};
longest_inode_len = inode_len.max(longest_inode_len);
}
}
let names: std::vec::IntoIter<Cell> = 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::<Vec<Cell>>()
.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<usize>,
longest_inode_len: usize,
out: &mut BufWriter<Stdout>,
) -> 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

View file

@ -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]