diff options
author | Luca Casonato <hello@lcas.dev> | 2023-04-12 15:13:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-12 15:13:32 +0200 |
commit | f90caa821c5a4acf28f7dec4071994ecf6f26e57 (patch) | |
tree | fa4af65399a16a9f2d17fa2b249f21be2c00b976 /runtime | |
parent | 0e3f62d4446ae7b9a64dacf7befcaecede118222 (diff) |
refactor(ext/fs): abstract FS via FileSystem trait (#18599)
This commit abstracts out the specifics of the underlying system calls
FS operations behind a new `FileSystem` and `File` trait in the
`ext/fs` extension.
This allows other embedders to re-use ext/fs, but substituting in a
different FS backend.
This is likely not the final form of these traits. Eventually they will
be entirely `deno_core::Resource` agnostic, and will live in a seperate
crate.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/build.rs | 16 | ||||
-rw-r--r-- | runtime/permissions/mod.rs | 53 | ||||
-rw-r--r-- | runtime/web_worker.rs | 3 | ||||
-rw-r--r-- | runtime/worker.rs | 3 |
4 files changed, 70 insertions, 5 deletions
diff --git a/runtime/build.rs b/runtime/build.rs index eb8cc34a6..d47bee941 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -18,6 +18,7 @@ mod startup_snapshot { use deno_core::Extension; use deno_core::ExtensionFileSource; use deno_core::ModuleCode; + use deno_fs::StdFs; use std::path::Path; fn transpile_ts_for_snapshotting( @@ -164,6 +165,10 @@ mod startup_snapshot { unreachable!("snapshotting!") } + fn check_read_all(&mut self, _api_name: &str) -> Result<(), AnyError> { + unreachable!("snapshotting!") + } + fn check_read_blind( &mut self, _path: &Path, @@ -181,11 +186,16 @@ mod startup_snapshot { unreachable!("snapshotting!") } - fn check_read_all(&mut self, _api_name: &str) -> Result<(), AnyError> { + fn check_write_all(&mut self, _api_name: &str) -> Result<(), AnyError> { unreachable!("snapshotting!") } - fn check_write_all(&mut self, _api_name: &str) -> Result<(), AnyError> { + fn check_write_blind( + &mut self, + _path: &Path, + _display: &str, + _api_name: &str, + ) -> Result<(), AnyError> { unreachable!("snapshotting!") } } @@ -310,7 +320,7 @@ mod startup_snapshot { deno_napi::deno_napi::init_ops_and_esm::<Permissions>(), deno_http::deno_http::init_ops_and_esm(), deno_io::deno_io::init_ops_and_esm(Default::default()), - deno_fs::deno_fs::init_ops_and_esm::<Permissions>(false), + deno_fs::deno_fs::init_ops_and_esm::<_, Permissions>(false, StdFs), runtime::init_ops_and_esm(), // FIXME(bartlomieju): these extensions are specified last, because they // depend on `runtime`, even though it should be other way around diff --git a/runtime/permissions/mod.rs b/runtime/permissions/mod.rs index 7e1772ee3..b15750313 100644 --- a/runtime/permissions/mod.rs +++ b/runtime/permissions/mod.rs @@ -667,6 +667,40 @@ impl UnaryPermission<WriteDescriptor> { } result } + + /// As `check()`, but permission error messages will anonymize the path + /// by replacing it with the given `display`. + pub fn check_blind( + &mut self, + path: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError> { + let resolved_path = resolve_from_cwd(path)?; + let (result, prompted, is_allow_all) = + self.query(Some(&resolved_path)).check( + self.name, + Some(api_name), + Some(&format!("<{display}>")), + self.prompt, + ); + if prompted { + if result.is_ok() { + if is_allow_all { + self.granted_list.clear(); + self.global_state = PermissionState::Granted; + } else { + self.granted_list.insert(WriteDescriptor(resolved_path)); + } + } else { + self.global_state = PermissionState::Denied; + if !is_allow_all { + self.denied_list.insert(WriteDescriptor(resolved_path)); + } + } + } + result + } } impl Default for UnaryPermission<WriteDescriptor> { @@ -1793,6 +1827,16 @@ impl PermissionsContainer { } #[inline(always)] + pub fn check_write_blind( + &mut self, + path: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError> { + self.0.lock().write.check_blind(path, display, api_name) + } + + #[inline(always)] pub fn check_run( &mut self, cmd: &str, @@ -1931,6 +1975,15 @@ impl deno_fs::FsPermissions for PermissionsContainer { self.0.lock().write.check(path, Some(api_name)) } + fn check_write_blind( + &mut self, + p: &Path, + display: &str, + api_name: &str, + ) -> Result<(), AnyError> { + self.0.lock().write.check_blind(p, display, api_name) + } + fn check_read_all(&mut self, api_name: &str) -> Result<(), AnyError> { self.0.lock().read.check_all(Some(api_name)) } diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 4be40c9b0..0d743cfc6 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -34,6 +34,7 @@ use deno_core::RuntimeOptions; use deno_core::SharedArrayBufferStore; use deno_core::Snapshot; use deno_core::SourceMapGetter; +use deno_fs::StdFs; use deno_io::Stdio; use deno_kv::sqlite::SqliteDbHandler; use deno_node::RequireNpmResolver; @@ -440,7 +441,7 @@ impl WebWorker { deno_napi::deno_napi::init_ops::<PermissionsContainer>(), deno_http::deno_http::init_ops(), deno_io::deno_io::init_ops(Some(options.stdio)), - deno_fs::deno_fs::init_ops::<PermissionsContainer>(unstable), + deno_fs::deno_fs::init_ops::<_, PermissionsContainer>(unstable, StdFs), deno_node::deno_node::init_ops::<crate::RuntimeNodeEnv>( options.npm_resolver, ), diff --git a/runtime/worker.rs b/runtime/worker.rs index ea1e5e046..14abd12b5 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -30,6 +30,7 @@ use deno_core::RuntimeOptions; use deno_core::SharedArrayBufferStore; use deno_core::Snapshot; use deno_core::SourceMapGetter; +use deno_fs::StdFs; use deno_io::Stdio; use deno_kv::sqlite::SqliteDbHandler; use deno_node::RequireNpmResolver; @@ -264,7 +265,7 @@ impl MainWorker { deno_napi::deno_napi::init_ops::<PermissionsContainer>(), deno_http::deno_http::init_ops(), deno_io::deno_io::init_ops(Some(options.stdio)), - deno_fs::deno_fs::init_ops::<PermissionsContainer>(unstable), + deno_fs::deno_fs::init_ops::<_, PermissionsContainer>(unstable, StdFs), deno_node::deno_node::init_ops::<crate::RuntimeNodeEnv>( options.npm_resolver, ), |