Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,9 @@ fn list(locs: Vec<String>, config: Config) -> i32 {

for loc in &locs {
let p = PathBuf::from(&loc);
if !p.exists() {
let path_data = PathData::new(p, None, None, &config, true);

if path_data.md().is_none() {
show_error!("'{}': {}", &loc, "No such file or directory");
/*
We found an error, the return code of ls should not be 0
Expand All @@ -1206,8 +1208,6 @@ fn list(locs: Vec<String>, config: Config) -> i32 {
continue;
}

let path_data = PathData::new(p, None, None, &config, true);

let show_dir_contents = match path_data.file_type() {
Some(ft) => !config.directory && ft.is_dir(),
None => {
Expand Down Expand Up @@ -1270,7 +1270,8 @@ fn sort_entries(entries: &mut Vec<PathData>, config: &Config) {

#[cfg(windows)]
fn is_hidden(file_path: &DirEntry) -> bool {
let metadata = fs::metadata(file_path.path()).unwrap();
let path = file_path.path();
let metadata = fs::metadata(&path).unwrap_or_else(|_| fs::symlink_metadata(&path).unwrap());
let attr = metadata.file_attributes();
(attr & 0x2) > 0
}
Expand Down Expand Up @@ -1331,7 +1332,7 @@ fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter<Stdout>)

fn get_metadata(entry: &Path, dereference: bool) -> std::io::Result<Metadata> {
if dereference {
entry.metadata().or_else(|_| entry.symlink_metadata())
entry.metadata()
} else {
entry.symlink_metadata()
}
Expand Down Expand Up @@ -1733,7 +1734,11 @@ fn display_file_name(path: &PathData, config: &Config) -> Option<Cell> {
#[cfg(unix)]
{
if config.format != Format::Long && config.inode {
name = get_inode(path.md()?) + " " + &name;
name = path
.md()
.map_or_else(|| "?".to_string(), |md| get_inode(md))
+ " "
+ &name;
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,3 +2021,28 @@ fn test_ls_path() {
.run()
.stdout_is(expected_stdout);
}

#[test]
fn test_ls_dangling_symlinks() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.mkdir("temp_dir");
at.symlink_file("does_not_exist", "temp_dir/dangle");

scene.ucmd().arg("-L").arg("temp_dir/dangle").fails();
scene.ucmd().arg("-H").arg("temp_dir/dangle").fails();

scene
.ucmd()
.arg("temp_dir/dangle")
.succeeds()
.stdout_contains("dangle");

scene
.ucmd()
.arg("-Li")
.arg("temp_dir")
.succeeds() // this should fail, though at the moment, ls lacks a way to propagate errors encountered during display
.stdout_contains(if cfg!(windows) { "dangle" } else { "? dangle" });
}