1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

refactor(ext/node): NodeFs - add back altered metadata method (#18613)

From https://github.com/denoland/deno/pull/18604/files#r1159992299

We should still have a `metadata` method because it's one system call
instead of two on most platforms.
This commit is contained in:
David Sherret 2023-04-06 12:53:53 -04:00 committed by GitHub
parent e51985ca74
commit 2d0a9ffbcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -50,8 +50,15 @@ pub trait NodePermissions {
fn check_read(&mut self, path: &Path) -> Result<(), AnyError>;
}
#[derive(Default, Clone)]
pub struct NodeFsMetadata {
pub is_file: bool,
pub is_dir: bool,
}
pub trait NodeFs {
fn current_dir() -> io::Result<PathBuf>;
fn metadata<P: AsRef<Path>>(path: P) -> io::Result<NodeFsMetadata>;
fn is_file<P: AsRef<Path>>(path: P) -> bool;
fn is_dir<P: AsRef<Path>>(path: P) -> bool;
fn exists<P: AsRef<Path>>(path: P) -> bool;
@ -66,6 +73,18 @@ impl NodeFs for RealFs {
std::env::current_dir()
}
fn metadata<P: AsRef<Path>>(path: P) -> io::Result<NodeFsMetadata> {
#[allow(clippy::disallowed_methods)]
std::fs::metadata(path).map(|metadata| {
// on most systems, calling is_file() and is_dir() is cheap
// and returns information already found in the metadata object
NodeFsMetadata {
is_file: metadata.is_file(),
is_dir: metadata.is_dir(),
}
})
}
fn exists<P: AsRef<Path>>(path: P) -> bool {
#[allow(clippy::disallowed_methods)]
std::fs::metadata(path).is_ok()

View file

@ -264,8 +264,8 @@ where
{
let path = PathBuf::from(path);
ensure_read_permission::<Env::P>(state, &path)?;
if Env::Fs::exists(&path) {
if Env::Fs::is_file(&path) {
if let Ok(metadata) = Env::Fs::metadata(&path) {
if metadata.is_file {
return Ok(0);
} else {
return Ok(1);