Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: Fix padding for dangling links in non-Long formats #2856

Merged
merged 8 commits into from
Jan 11, 2022
29 changes: 23 additions & 6 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down