From 341cc1ea63b17e6acc6d3494f66feb96cd79fcb5 Mon Sep 17 00:00:00 2001 From: Jason Gedge Date: Sun, 13 Oct 2019 12:00:30 -0400 Subject: [PATCH] Ignore errors in `ls`. `std::fs::metadata` will attempt to follow symlinks, which results in a "No such file or directory" error if the path pointed to by the symlink does not exist. This shouldn't prevent `ls` from succeeding, so we ignore errors. Also, switching to use of `symlink_metadata` means we get stat info on the symlink itself, not what it points to. This means `ls` will now include broken symlinks in its listing. --- src/shell/filesystem_shell.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/shell/filesystem_shell.rs b/src/shell/filesystem_shell.rs index 72a0c241f3..f0adeebeb8 100644 --- a/src/shell/filesystem_shell.rs +++ b/src/shell/filesystem_shell.rs @@ -128,13 +128,16 @@ impl Shell for FilesystemShell { } if let Ok(entry) = entry { let filepath = entry.path(); - let filename = if let Ok(fname) = filepath.strip_prefix(&cwd) { - fname - } else { - Path::new(&filepath) - }; - let value = dir_entry_dict(filename, &entry.metadata().unwrap(), &name_tag)?; - yield ReturnSuccess::value(value); + if let Ok(metadata) = std::fs::symlink_metadata(&filepath) { + let filename = if let Ok(fname) = filepath.strip_prefix(&cwd) { + fname + } else { + Path::new(&filepath) + }; + + let value = dir_entry_dict(filename, &metadata, &name_tag)?; + yield ReturnSuccess::value(value); + } } } }; @@ -164,14 +167,16 @@ impl Shell for FilesystemShell { break; } if let Ok(entry) = entry { - let filename = if let Ok(fname) = entry.strip_prefix(&cwd) { - fname - } else { - Path::new(&entry) - }; - let metadata = std::fs::metadata(&entry).unwrap(); - if let Ok(value) = dir_entry_dict(filename, &metadata, &name_tag) { - yield ReturnSuccess::value(value); + if let Ok(metadata) = std::fs::symlink_metadata(&entry) { + let filename = if let Ok(fname) = entry.strip_prefix(&cwd) { + fname + } else { + Path::new(&entry) + }; + + if let Ok(value) = dir_entry_dict(filename, &metadata, &name_tag) { + yield ReturnSuccess::value(value); + } } } }