diff options
Diffstat (limited to 'ext/fs')
-rw-r--r-- | ext/fs/Cargo.toml | 3 | ||||
-rw-r--r-- | ext/fs/interface.rs | 29 | ||||
-rw-r--r-- | ext/fs/lib.rs | 1 |
3 files changed, 32 insertions, 1 deletions
diff --git a/ext/fs/Cargo.toml b/ext/fs/Cargo.toml index 5248f3d87..ecdebdf1b 100644 --- a/ext/fs/Cargo.toml +++ b/ext/fs/Cargo.toml @@ -14,11 +14,12 @@ description = "Ops for interacting with the file system" path = "lib.rs" [features] -sync_fs = [] +sync_fs = ["deno_config/sync"] [dependencies] async-trait.workspace = true base32.workspace = true +deno_config = { workspace = true, default-features = false } deno_core.workspace = true deno_io.workspace = true deno_permissions.workspace = true diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs index 833f362bf..6e3c359bb 100644 --- a/ext/fs/interface.rs +++ b/ext/fs/interface.rs @@ -304,6 +304,35 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync { } } +pub struct DenoConfigFsAdapter<'a>(&'a dyn FileSystem); + +impl<'a> DenoConfigFsAdapter<'a> { + pub fn new(fs: &'a dyn FileSystem) -> Self { + Self(fs) + } +} + +impl<'a> deno_config::fs::DenoConfigFs for DenoConfigFsAdapter<'a> { + fn read_to_string(&self, path: &Path) -> Result<String, std::io::Error> { + use deno_io::fs::FsError; + use std::io::ErrorKind; + self + .0 + .read_text_file_lossy_sync(path, None) + .map_err(|err| match err { + FsError::Io(io) => io, + FsError::FileBusy => std::io::Error::new(ErrorKind::Other, "file busy"), + FsError::NotSupported => { + std::io::Error::new(ErrorKind::Other, "not supported") + } + FsError::PermissionDenied(name) => std::io::Error::new( + ErrorKind::PermissionDenied, + format!("requires {}", name), + ), + }) + } +} + // Like String::from_utf8_lossy but operates on owned values #[inline(always)] fn string_from_utf8_lossy(buf: Vec<u8>) -> String { diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 2dce04b32..a60408f9b 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -9,6 +9,7 @@ pub mod sync; pub use crate::in_memory_fs::InMemoryFs; pub use crate::interface::AccessCheckCb; pub use crate::interface::AccessCheckFn; +pub use crate::interface::DenoConfigFsAdapter; pub use crate::interface::FileSystem; pub use crate::interface::FileSystemRc; pub use crate::interface::FsDirEntry; |