diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-04-06 12:53:53 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-06 16:53:53 +0000 |
commit | 2d0a9ffbccf9d8a4773eb6efa48ddc6978af6455 (patch) | |
tree | e6f8440c2858dd8a9d8b20620469942e9ca9ed77 /ext/node/lib.rs | |
parent | e51985ca749d80d40474489c739cf163b35843e1 (diff) |
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.
Diffstat (limited to 'ext/node/lib.rs')
-rw-r--r-- | ext/node/lib.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs index ec3e7ab25..478efaf27 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -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() |