1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(runtime): do not panic on irregular dir entries (#9579)

This commit is contained in:
Casper Beyer 2021-02-25 18:16:18 +08:00 committed by GitHub
parent d26bef21a5
commit 687ff2ab14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 11 deletions

View file

@ -78,3 +78,23 @@ unitTest({ perms: { read: false } }, async function readDirPerm(): Promise<
await Deno.readDir("tests/")[Symbol.asyncIterator]().next();
}, Deno.errors.PermissionDenied);
});
unitTest(
{ perms: { read: true }, ignore: Deno.build.os == "windows" },
async function readDirDevFd(): Promise<
void
> {
for await (const _ of Deno.readDir("/dev/fd")) {
// We don't actually care whats in here; just that we don't panic on non regular entries
}
},
);
unitTest(
{ perms: { read: true }, ignore: Deno.build.os == "windows" },
function readDirDevFdSync(): void {
for (const _ of Deno.readDirSync("/dev/fd")) {
// We don't actually care whats in here; just that we don't panic on non regular file entries
}
},
);

View file

@ -1022,20 +1022,19 @@ fn op_read_dir_sync(
let entries: Vec<_> = std::fs::read_dir(path)?
.filter_map(|entry| {
let entry = entry.unwrap();
let file_type = entry.file_type().unwrap();
// Not all filenames can be encoded as UTF-8. Skip those for now.
if let Ok(name) = into_string(entry.file_name()) {
Some(json!({
"name": name,
"isFile": file_type.is_file(),
"isDirectory": file_type.is_dir(),
"isSymlink": file_type.is_symlink()
"isFile": entry.file_type().map_or(false, |file_type| file_type.is_file()),
"isDirectory": entry.file_type().map_or(false, |file_type| file_type.is_dir()),
"isSymlink": entry.file_type().map_or(false, |file_type| file_type.is_symlink()),
}))
} else {
None
}
})
.collect();
.collect();
Ok(json!({ "entries": entries }))
}
@ -1056,25 +1055,24 @@ async fn op_read_dir_async(
let entries: Vec<_> = std::fs::read_dir(path)?
.filter_map(|entry| {
let entry = entry.unwrap();
let file_type = entry.file_type().unwrap();
// Not all filenames can be encoded as UTF-8. Skip those for now.
if let Ok(name) = into_string(entry.file_name()) {
Some(json!({
"name": name,
"isFile": file_type.is_file(),
"isDirectory": file_type.is_dir(),
"isSymlink": file_type.is_symlink()
"isFile": entry.file_type().map_or(false, |file_type| file_type.is_file()),
"isDirectory": entry.file_type().map_or(false, |file_type| file_type.is_dir()),
"isSymlink": entry.file_type().map_or(false, |file_type| file_type.is_symlink()),
}))
} else {
None
}
})
.collect();
.collect();
Ok(json!({ "entries": entries }))
})
.await
.unwrap()
.unwrap()
}
#[derive(Deserialize)]