summaryrefslogtreecommitdiff
path: root/ext/fs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fs')
-rw-r--r--ext/fs/interface.rs3
-rw-r--r--ext/fs/std_fs.rs35
2 files changed, 38 insertions, 0 deletions
diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs
index 2fdfa2186..09e16aff1 100644
--- a/ext/fs/interface.rs
+++ b/ext/fs/interface.rs
@@ -315,6 +315,9 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
fn exists_sync(&self, path: &Path) -> bool {
self.stat_sync(path).is_ok()
}
+ async fn exists_async(&self, path: PathBuf) -> FsResult<bool> {
+ Ok(self.stat_async(path).await.is_ok())
+ }
fn read_text_file_lossy_sync(
&self,
diff --git a/ext/fs/std_fs.rs b/ext/fs/std_fs.rs
index 7903700c3..7fc33a8ad 100644
--- a/ext/fs/std_fs.rs
+++ b/ext/fs/std_fs.rs
@@ -173,6 +173,15 @@ impl FileSystem for RealFs {
spawn_blocking(move || lstat(&path)).await?.map(Into::into)
}
+ fn exists_sync(&self, path: &Path) -> bool {
+ exists(path)
+ }
+ async fn exists_async(&self, path: PathBuf) -> FsResult<bool> {
+ spawn_blocking(move || exists(&path))
+ .await
+ .map_err(Into::into)
+ }
+
fn realpath_sync(&self, path: &Path) -> FsResult<PathBuf> {
realpath(path)
}
@@ -837,6 +846,32 @@ fn stat_extra(
}
}
+fn exists(path: &Path) -> bool {
+ #[cfg(unix)]
+ {
+ use nix::unistd::access;
+ use nix::unistd::AccessFlags;
+ access(path, AccessFlags::F_OK).is_ok()
+ }
+
+ #[cfg(windows)]
+ {
+ use std::os::windows::ffi::OsStrExt;
+ use winapi::um::fileapi::GetFileAttributesW;
+ use winapi::um::fileapi::INVALID_FILE_ATTRIBUTES;
+
+ let path = path
+ .as_os_str()
+ .encode_wide()
+ .chain(std::iter::once(0))
+ .collect::<Vec<_>>();
+ // Safety: `path` is a null-terminated string
+ let attrs = unsafe { GetFileAttributesW(path.as_ptr()) };
+
+ attrs != INVALID_FILE_ATTRIBUTES
+ }
+}
+
fn realpath(path: &Path) -> FsResult<PathBuf> {
Ok(deno_core::strip_unc_prefix(path.canonicalize()?))
}