Output error when ls into a file without permission (#3218)

* Output error when ls into a file without permission

* added test to check fails when ls into prohibited dir

* fix lint

* trigger wasm build

* Update filesystem_shell.rs

Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
Luccas Mateus 2021-03-31 03:52:39 -03:00 committed by GitHub
parent 387098fc87
commit 419a0665c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View File

@ -244,6 +244,27 @@ fn lists_all_hidden_files_when_glob_does_not_contain_dot() {
})
}
#[test]
#[cfg(unix)]
fn fails_with_ls_to_dir_without_permission() {
Playground::setup("ls_test_1", |dirs, sandbox| {
sandbox.within("dir_a").with_files(vec![
EmptyFile("yehuda.11.txt"),
EmptyFile("jonathan.10.txt"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
chmod 000 dir_a; ls dir_a
"#
));
assert!(actual
.err
.contains("The permissions of 0 do not allow access for this user"));
})
}
#[test]
fn lists_files_including_starting_with_dot() {
Playground::setup("ls_test_9", |dirs, sandbox| {

View File

@ -125,6 +125,26 @@ impl Shell for FilesystemShell {
let p_tag = p.tag;
let mut p = p.item;
if p.is_dir() {
if permission_denied(&p) {
#[cfg(unix)]
let error_msg = format!(
"The permissions of {:o} do not allow access for this user",
p.metadata()
.expect(
"this shouldn't be called since we already know there is a dir"
)
.permissions()
.mode()
& 0o0777
);
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
return Err(ShellError::labeled_error(
"Permission denied",
error_msg,
&p_tag,
));
}
if is_empty_dir(&p) {
return Ok(OutputStream::empty());
}
@ -915,6 +935,13 @@ fn is_empty_dir(dir: impl AsRef<Path>) -> bool {
}
}
fn permission_denied(dir: impl AsRef<Path>) -> bool {
match dir.as_ref().read_dir() {
Err(e) => matches!(e.kind(), std::io::ErrorKind::PermissionDenied),
Ok(_) => false,
}
}
fn is_hidden_dir(dir: impl AsRef<Path>) -> bool {
#[cfg(windows)]
{