From bcbdaac7e6399870b5b6fcdf765013c1682fe80c Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 17 Apr 2023 16:10:59 +0200 Subject: chore(ext/fs): decouple fs trait from deno_core (#18732) This commit removes the dependencies on `deno_core` for the Fs trait. This allows to move the trait into a different crate that does not depend on core in the limit. This adds a new `bounds` field to `deno_core::extension!` that expands to `where` clauses on the generated code. This allows to add bounds to the extension parameters, such as `Fs::File: Resource`. --- ext/fs/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'ext/fs/lib.rs') diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index c1418815c..464d84ade 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -9,7 +9,6 @@ pub use crate::interface::FileSystem; pub use crate::interface::FsDirEntry; pub use crate::interface::FsError; pub use crate::interface::FsFileType; -pub use crate::interface::FsPermissions; pub use crate::interface::FsResult; pub use crate::interface::FsStat; pub use crate::interface::OpenOptions; @@ -17,11 +16,48 @@ use crate::ops::*; pub use crate::std_fs::StdFs; +use deno_core::error::AnyError; use deno_core::OpState; +use deno_core::Resource; use std::cell::RefCell; use std::convert::From; +use std::path::Path; use std::rc::Rc; +pub trait FsPermissions { + fn check_read(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError>; + fn check_read_all(&mut self, api_name: &str) -> Result<(), AnyError>; + fn check_read_blind( + &mut self, + p: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError>; + fn check_write(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError>; + fn check_write_all(&mut self, api_name: &str) -> Result<(), AnyError>; + fn check_write_blind( + &mut self, + p: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError>; + + fn check( + &mut self, + open_options: &OpenOptions, + path: &Path, + api_name: &str, + ) -> Result<(), AnyError> { + if open_options.read { + self.check_read(path, api_name)?; + } + if open_options.write || open_options.append { + self.check_write(path, api_name)?; + } + Ok(()) + } +} + struct UnstableChecker { pub unstable: bool, } @@ -52,6 +88,7 @@ pub(crate) fn check_unstable2(state: &Rc>, api_name: &str) { deno_core::extension!(deno_fs, deps = [ deno_web ], parameters = [Fs: FileSystem, P: FsPermissions], + bounds = [Fs::File: Resource], ops = [ op_cwd, op_umask, -- cgit v1.2.3