diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-05-04 14:28:42 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-04 14:28:42 -0400 |
commit | 5270c43e412cc636cd9923182169d166d181f78a (patch) | |
tree | 640c90a70f7dd7bc91f5e942e1eaa5a7914ae46b /ext/fs/ops.rs | |
parent | 4b645676d62fd595ecac47e24be1b83a3ba636c6 (diff) |
refactor(ext/fs): boxed deno_fs::FileSystem (#18945)
1. Boxed `File` and `FileSystem` to allow more easily passing this
through the CLI code (as shown within this pr).
2. `StdFileResource` is now `FileResource`. `FileResource` now contains
an `Rc<dyn File>`.
Diffstat (limited to 'ext/fs/ops.rs')
-rw-r--r-- | ext/fs/ops.rs | 454 |
1 files changed, 162 insertions, 292 deletions
diff --git a/ext/fs/ops.rs b/ext/fs/ops.rs index 8c5d21201..c9996d8ce 100644 --- a/ext/fs/ops.rs +++ b/ext/fs/ops.rs @@ -7,65 +7,39 @@ use std::io::SeekFrom; use std::path::Path; use std::path::PathBuf; use std::rc::Rc; +use std::sync::Arc; use deno_core::error::custom_error; -use deno_core::error::not_supported; -use deno_core::error::resource_unavailable; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::OpState; -use deno_core::Resource; use deno_core::ResourceId; use deno_core::ZeroCopyBuf; +use deno_io::fs::FileResource; +use deno_io::fs::FsError; +use deno_io::fs::FsStat; use rand::rngs::ThreadRng; use rand::thread_rng; use rand::Rng; use serde::Serialize; -use tokio::task::JoinError; use crate::check_unstable; use crate::check_unstable2; use crate::interface::FsDirEntry; -use crate::interface::FsError; use crate::interface::FsFileType; -use crate::interface::FsStat; -use crate::File; use crate::FileSystem; use crate::FsPermissions; use crate::OpenOptions; -impl From<JoinError> for FsError { - fn from(err: JoinError) -> Self { - if err.is_cancelled() { - todo!("async tasks must not be cancelled") - } - if err.is_panic() { - std::panic::resume_unwind(err.into_panic()); // resume the panic on the main thread - } - unreachable!() - } -} - -impl From<FsError> for AnyError { - fn from(err: FsError) -> Self { - match err { - FsError::Io(err) => AnyError::from(err), - FsError::FileBusy => resource_unavailable(), - FsError::NotSupported => not_supported(), - } - } -} - #[op] -pub fn op_cwd<Fs, P>(state: &mut OpState) -> Result<String, AnyError> +pub fn op_cwd<P>(state: &mut OpState) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); let path = fs.cwd()?; state .borrow_mut::<P>() @@ -75,34 +49,36 @@ where } #[op] -fn op_chdir<Fs, P>(state: &mut OpState, directory: &str) -> Result<(), AnyError> +fn op_chdir<P>(state: &mut OpState, directory: &str) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let d = PathBuf::from(&directory); state.borrow_mut::<P>().check_read(&d, "Deno.chdir()")?; - state.borrow::<Fs>().chdir(&d).context_path("chdir", &d) + state + .borrow::<Arc<dyn FileSystem>>() + .chdir(&d) + .context_path("chdir", &d) } #[op] -fn op_umask<Fs>(state: &mut OpState, mask: Option<u32>) -> Result<u32, AnyError> +fn op_umask(state: &mut OpState, mask: Option<u32>) -> Result<u32, AnyError> where - Fs: FileSystem + 'static, { check_unstable(state, "Deno.umask"); - state.borrow::<Fs>().umask(mask).context("umask") + state + .borrow::<Arc<dyn FileSystem>>() + .umask(mask) + .context("umask") } #[op] -fn op_open_sync<Fs, P>( +fn op_open_sync<P>( state: &mut OpState, path: String, options: Option<OpenOptions>, ) -> Result<ResourceId, AnyError> where - Fs: FileSystem + 'static, - Fs::File: Resource, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -111,22 +87,22 @@ where let permissions = state.borrow_mut::<P>(); permissions.check(&options, &path, "Deno.openSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); let file = fs.open_sync(&path, options).context_path("open", &path)?; - let rid = state.resource_table.add(file); + let rid = state + .resource_table + .add(FileResource::new(file, "fsFile".to_string())); Ok(rid) } #[op] -async fn op_open_async<Fs, P>( +async fn op_open_async<P>( state: Rc<RefCell<OpState>>, path: String, options: Option<OpenOptions>, ) -> Result<ResourceId, AnyError> where - Fs: FileSystem + 'static, - Fs::File: Resource, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -136,26 +112,28 @@ where let mut state = state.borrow_mut(); let permissions = state.borrow_mut::<P>(); permissions.check(&options, &path, "Deno.open()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; let file = fs .open_async(path.clone(), options) .await .context_path("open", &path)?; - let rid = state.borrow_mut().resource_table.add(file); + let rid = state + .borrow_mut() + .resource_table + .add(FileResource::new(file, "fsFile".to_string())); Ok(rid) } #[op] -fn op_mkdir_sync<Fs, P>( +fn op_mkdir_sync<P>( state: &mut OpState, path: String, recursive: bool, mode: Option<u32>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -166,7 +144,7 @@ where .borrow_mut::<P>() .check_write(&path, "Deno.mkdirSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.mkdir_sync(&path, recursive, mode) .context_path("mkdir", &path)?; @@ -174,14 +152,13 @@ where } #[op] -async fn op_mkdir_async<Fs, P>( +async fn op_mkdir_async<P>( state: Rc<RefCell<OpState>>, path: String, recursive: bool, mode: Option<u32>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -191,7 +168,7 @@ where let fs = { let mut state = state.borrow_mut(); state.borrow_mut::<P>().check_write(&path, "Deno.mkdir()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.mkdir_async(path.clone(), recursive, mode) @@ -202,39 +179,37 @@ where } #[op] -fn op_chmod_sync<Fs, P>( +fn op_chmod_sync<P>( state: &mut OpState, path: String, mode: u32, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); state .borrow_mut::<P>() .check_write(&path, "Deno.chmodSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.chmod_sync(&path, mode).context_path("chmod", &path)?; Ok(()) } #[op] -async fn op_chmod_async<Fs, P>( +async fn op_chmod_async<P>( state: Rc<RefCell<OpState>>, path: String, mode: u32, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); let fs = { let mut state = state.borrow_mut(); state.borrow_mut::<P>().check_write(&path, "Deno.chmod()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.chmod_async(path.clone(), mode) .await @@ -243,42 +218,40 @@ where } #[op] -fn op_chown_sync<Fs, P>( +fn op_chown_sync<P>( state: &mut OpState, path: String, uid: Option<u32>, gid: Option<u32>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); state .borrow_mut::<P>() .check_write(&path, "Deno.chownSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.chown_sync(&path, uid, gid) .context_path("chown", &path)?; Ok(()) } #[op] -async fn op_chown_async<Fs, P>( +async fn op_chown_async<P>( state: Rc<RefCell<OpState>>, path: String, uid: Option<u32>, gid: Option<u32>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); let fs = { let mut state = state.borrow_mut(); state.borrow_mut::<P>().check_write(&path, "Deno.chown()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.chown_async(path.clone(), uid, gid) .await @@ -287,13 +260,12 @@ where } #[op] -fn op_remove_sync<Fs, P>( +fn op_remove_sync<P>( state: &mut OpState, path: &str, recursive: bool, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -302,7 +274,7 @@ where .borrow_mut::<P>() .check_write(&path, "Deno.removeSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.remove_sync(&path, recursive) .context_path("remove", &path)?; @@ -310,13 +282,12 @@ where } #[op] -async fn op_remove_async<Fs, P>( +async fn op_remove_async<P>( state: Rc<RefCell<OpState>>, path: String, recursive: bool, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -326,7 +297,7 @@ where state .borrow_mut::<P>() .check_write(&path, "Deno.remove()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.remove_async(path.clone(), recursive) @@ -337,13 +308,12 @@ where } #[op] -fn op_copy_file_sync<Fs, P>( +fn op_copy_file_sync<P>( state: &mut OpState, from: &str, to: &str, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let from = PathBuf::from(from); @@ -353,7 +323,7 @@ where permissions.check_read(&from, "Deno.copyFileSync()")?; permissions.check_write(&to, "Deno.copyFileSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.copy_file_sync(&from, &to) .context_two_path("copy", &from, &to)?; @@ -361,13 +331,12 @@ where } #[op] -async fn op_copy_file_async<Fs, P>( +async fn op_copy_file_async<P>( state: Rc<RefCell<OpState>>, from: String, to: String, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let from = PathBuf::from(from); @@ -378,7 +347,7 @@ where let permissions = state.borrow_mut::<P>(); permissions.check_read(&from, "Deno.copyFile()")?; permissions.check_write(&to, "Deno.copyFile()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.copy_file_async(from.clone(), to.clone()) @@ -389,20 +358,19 @@ where } #[op] -fn op_stat_sync<Fs, P>( +fn op_stat_sync<P>( state: &mut OpState, path: String, stat_out_buf: &mut [u32], ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); state .borrow_mut::<P>() .check_read(&path, "Deno.statSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); let stat = fs.stat_sync(&path).context_path("stat", &path)?; let serializable_stat = SerializableStat::from(stat); serializable_stat.write(stat_out_buf); @@ -410,12 +378,11 @@ where } #[op] -async fn op_stat_async<Fs, P>( +async fn op_stat_async<P>( state: Rc<RefCell<OpState>>, path: String, ) -> Result<SerializableStat, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -423,7 +390,7 @@ where let mut state = state.borrow_mut(); let permissions = state.borrow_mut::<P>(); permissions.check_read(&path, "Deno.stat()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; let stat = fs .stat_async(path.clone()) @@ -433,20 +400,19 @@ where } #[op] -fn op_lstat_sync<Fs, P>( +fn op_lstat_sync<P>( state: &mut OpState, path: String, stat_out_buf: &mut [u32], ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); state .borrow_mut::<P>() .check_read(&path, "Deno.lstatSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); let stat = fs.lstat_sync(&path).context_path("lstat", &path)?; let serializable_stat = SerializableStat::from(stat); serializable_stat.write(stat_out_buf); @@ -454,12 +420,11 @@ where } #[op] -async fn op_lstat_async<Fs, P>( +async fn op_lstat_async<P>( state: Rc<RefCell<OpState>>, path: String, ) -> Result<SerializableStat, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -467,7 +432,7 @@ where let mut state = state.borrow_mut(); let permissions = state.borrow_mut::<P>(); permissions.check_read(&path, "Deno.lstat()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; let stat = fs .lstat_async(path.clone()) @@ -477,17 +442,16 @@ where } #[op] -fn op_realpath_sync<Fs, P>( +fn op_realpath_sync<P>( state: &mut OpState, path: String, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); - let fs = state.borrow::<Fs>().clone(); + let fs = state.borrow::<Arc<dyn FileSystem>>().clone(); let permissions = state.borrow_mut::<P>(); permissions.check_read(&path, "Deno.realPathSync()")?; if path.is_relative() { @@ -502,12 +466,11 @@ where } #[op] -async fn op_realpath_async<Fs, P>( +async fn op_realpath_async<P>( state: Rc<RefCell<OpState>>, path: String, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -515,7 +478,7 @@ where let fs; { let mut state = state.borrow_mut(); - fs = state.borrow::<Fs>().clone(); + fs = state.borrow::<Arc<dyn FileSystem>>().clone(); let permissions = state.borrow_mut::<P>(); permissions.check_read(&path, "Deno.realPath()")?; if path.is_relative() { @@ -532,12 +495,11 @@ where } #[op] -fn op_read_dir_sync<Fs, P>( +fn op_read_dir_sync<P>( state: &mut OpState, path: String, ) -> Result<Vec<FsDirEntry>, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -546,19 +508,18 @@ where .borrow_mut::<P>() .check_read(&path, "Deno.readDirSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); let entries = fs.read_dir_sync(&path).context_path("readdir", &path)?; Ok(entries) } #[op] -async fn op_read_dir_async<Fs, P>( +async fn op_read_dir_async<P>( state: Rc<RefCell<OpState>>, path: String, ) -> Result<Vec<FsDirEntry>, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -568,7 +529,7 @@ where state .borrow_mut::<P>() .check_read(&path, "Deno.readDir()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; let entries = fs @@ -580,13 +541,12 @@ where } #[op] -fn op_rename_sync<Fs, P>( +fn op_rename_sync<P>( state: &mut OpState, oldpath: String, newpath: String, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let oldpath = PathBuf::from(oldpath); @@ -597,7 +557,7 @@ where permissions.check_write(&oldpath, "Deno.renameSync()")?; permissions.check_write(&newpath, "Deno.renameSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.rename_sync(&oldpath, &newpath) .context_two_path("rename", &oldpath, &newpath)?; @@ -605,13 +565,12 @@ where } #[op] -async fn op_rename_async<Fs, P>( +async fn op_rename_async<P>( state: Rc<RefCell<OpState>>, oldpath: String, newpath: String, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let oldpath = PathBuf::from(oldpath); @@ -623,7 +582,7 @@ where permissions.check_read(&oldpath, "Deno.rename()")?; permissions.check_write(&oldpath, "Deno.rename()")?; permissions.check_write(&newpath, "Deno.rename()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.rename_async(oldpath.clone(), newpath.clone()) @@ -634,13 +593,12 @@ where } #[op] -fn op_link_sync<Fs, P>( +fn op_link_sync<P>( state: &mut OpState, oldpath: &str, newpath: &str, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let oldpath = PathBuf::from(oldpath); @@ -652,7 +610,7 @@ where permissions.check_read(&newpath, "Deno.linkSync()")?; permissions.check_write(&newpath, "Deno.linkSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.link_sync(&oldpath, &newpath) .context_two_path("link", &oldpath, &newpath)?; @@ -660,13 +618,12 @@ where } #[op] -async fn op_link_async<Fs, P>( +async fn op_link_async<P>( state: Rc<RefCell<OpState>>, oldpath: String, newpath: String, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let oldpath = PathBuf::from(&oldpath); @@ -679,7 +636,7 @@ where permissions.check_write(&oldpath, "Deno.link()")?; permissions.check_read(&newpath, "Deno.link()")?; permissions.check_write(&newpath, "Deno.link()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.link_async(oldpath.clone(), newpath.clone()) @@ -690,14 +647,13 @@ where } #[op] -fn op_symlink_sync<Fs, P>( +fn op_symlink_sync<P>( state: &mut OpState, oldpath: &str, newpath: &str, file_type: Option<FsFileType>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let oldpath = PathBuf::from(oldpath); @@ -707,7 +663,7 @@ where permissions.check_write_all("Deno.symlinkSync()")?; permissions.check_read_all("Deno.symlinkSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.symlink_sync(&oldpath, &newpath, file_type) .context_two_path("symlink", &oldpath, &newpath)?; @@ -715,14 +671,13 @@ where } #[op] -async fn op_symlink_async<Fs, P>( +async fn op_symlink_async<P>( state: Rc<RefCell<OpState>>, oldpath: String, newpath: String, file_type: Option<FsFileType>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let oldpath = PathBuf::from(&oldpath); @@ -733,7 +688,7 @@ where let permissions = state.borrow_mut::<P>(); permissions.check_write_all("Deno.symlink()")?; permissions.check_read_all("Deno.symlink()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.symlink_async(oldpath.clone(), newpath.clone(), file_type) @@ -744,12 +699,11 @@ where } #[op] -fn op_read_link_sync<Fs, P>( +fn op_read_link_sync<P>( state: &mut OpState, path: String, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -758,7 +712,7 @@ where .borrow_mut::<P>() .check_read(&path, "Deno.readLink()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); let target = fs.read_link_sync(&path).context_path("readlink", &path)?; let target_string = path_into_string(target.into_os_string())?; @@ -766,12 +720,11 @@ where } #[op] -async fn op_read_link_async<Fs, P>( +async fn op_read_link_async<P>( state: Rc<RefCell<OpState>>, path: String, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -781,7 +734,7 @@ where state .borrow_mut::<P>() .check_read(&path, "Deno.readLink()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; let target = fs @@ -793,13 +746,12 @@ where } #[op] -fn op_truncate_sync<Fs, P>( +fn op_truncate_sync<P>( state: &mut OpState, path: &str, len: u64, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -808,7 +760,7 @@ where .borrow_mut::<P>() .check_write(&path, "Deno.truncateSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.truncate_sync(&path, len) .context_path("truncate", &path)?; @@ -816,13 +768,12 @@ where } #[op] -async fn op_truncate_async<Fs, P>( +async fn op_truncate_async<P>( state: Rc<RefCell<OpState>>, path: String, len: u64, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -832,7 +783,7 @@ where state .borrow_mut::<P>() .check_write(&path, "Deno.truncate()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.truncate_async(path.clone(), len) @@ -843,7 +794,7 @@ where } #[op] -fn op_utime_sync<Fs, P>( +fn op_utime_sync<P>( state: &mut OpState, path: &str, atime_secs: i64, @@ -852,14 +803,13 @@ fn op_utime_sync<Fs, P>( mtime_nanos: u32, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); state.borrow_mut::<P>().check_write(&path, "Deno.utime()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.utime_sync(&path, atime_secs, atime_nanos, mtime_secs, mtime_nanos) .context_path("utime", &path)?; @@ -867,7 +817,7 @@ where } #[op] -async fn op_utime_async<Fs, P>( +async fn op_utime_async<P>( state: Rc<RefCell<OpState>>, path: String, atime_secs: i64, @@ -876,7 +826,6 @@ async fn op_utime_async<Fs, P>( mtime_nanos: u32, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -884,7 +833,7 @@ where let fs = { let mut state = state.borrow_mut(); state.borrow_mut::<P>().check_write(&path, "Deno.utime()")?; - state.borrow::<Fs>().clone() + state.borrow::<Arc<dyn FileSystem>>().clone() }; fs.utime_async( @@ -901,17 +850,16 @@ where } #[op] -fn op_make_temp_dir_sync<Fs, P>( +fn op_make_temp_dir_sync<P>( state: &mut OpState, dir: Option<String>, prefix: Option<String>, suffix: Option<String>, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { - let (dir, fs) = make_temp_check_sync::<Fs, P>(state, dir)?; + let (dir, fs) = make_temp_check_sync::<P>(state, dir)?; let mut rng = thread_rng(); @@ -935,17 +883,16 @@ where } #[op] -async fn op_make_temp_dir_async<Fs, P>( +async fn op_make_temp_dir_async<P>( state: Rc<RefCell<OpState>>, dir: Option<String>, prefix: Option<String>, suffix: Option<String>, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { - let (dir, fs) = make_temp_check_async::<Fs, P>(state, dir)?; + let (dir, fs) = make_temp_check_async::<P>(state, dir)?; let mut rng = thread_rng(); @@ -969,17 +916,16 @@ where } #[op] -fn op_make_temp_file_sync<Fs, P>( +fn op_make_temp_file_sync<P>( state: &mut OpState, dir: Option<String>, prefix: Option<String>, suffix: Option<String>, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { - let (dir, fs) = make_temp_check_sync::<Fs, P>(state, dir)?; + let (dir, fs) = make_temp_check_sync::<P>(state, dir)?; let open_opts = OpenOptions { write: true, @@ -1010,17 +956,16 @@ where } #[op] -async fn op_make_temp_file_async<Fs, P>( +async fn op_make_temp_file_async<P>( state: Rc<RefCell<OpState>>, dir: Option<String>, prefix: Option<String>, suffix: Option<String>, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { - let (dir, fs) = make_temp_check_async::<Fs, P>(state, dir)?; + let (dir, fs) = make_temp_check_async::<P>(state, dir)?; let open_opts = OpenOptions { write: true, @@ -1049,15 +994,14 @@ where .context("tmpfile") } -fn make_temp_check_sync<Fs, P>( +fn make_temp_check_sync<P>( state: &mut OpState, dir: Option<String>, -) -> Result<(PathBuf, Fs), AnyError> +) -> Result<(PathBuf, Arc<dyn FileSystem>), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { - let fs = state.borrow::<Fs>().clone(); + let fs = state.borrow::<Arc<dyn FileSystem>>().clone(); let dir = match dir { Some(dir) => { let dir = PathBuf::from(dir); @@ -1079,16 +1023,15 @@ where Ok((dir, fs)) } -fn make_temp_check_async<Fs, P>( +fn make_temp_check_async<P>( state: Rc<RefCell<OpState>>, dir: Option<String>, -) -> Result<(PathBuf, Fs), AnyError> +) -> Result<(PathBuf, Arc<dyn FileSystem>), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let mut state = state.borrow_mut(); - let fs = state.borrow::<Fs>().clone(); + let fs = state.borrow::<Arc<dyn FileSystem>>().clone(); let dir = match dir { Some(dir) => { let dir = PathBuf::from(dir); @@ -1128,7 +1071,7 @@ fn tmp_name( } #[op] -fn op_write_file_sync<Fs, P>( +fn op_write_file_sync<P>( state: &mut OpState, path: String, mode: Option<u32>, @@ -1138,7 +1081,6 @@ fn op_write_file_sync<Fs, P>( data: ZeroCopyBuf, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -1147,7 +1089,7 @@ where let options = OpenOptions::write(create, append, create_new, mode); permissions.check(&options, &path, "Deno.writeFileSync()")?; - let fs = state.borrow::<Fs>(); + let fs = state.borrow::<Arc<dyn FileSystem>>(); fs.write_file_sync(&path, options, &data) .context_path("writefile", &path)?; @@ -1156,7 +1098,7 @@ where } #[op] -async fn op_write_file_async<Fs, P>( +async fn op_write_file_async<P>( state: Rc<RefCell<OpState>>, path: String, mode: Option<u32>, @@ -1167,7 +1109,6 @@ async fn op_write_file_async<Fs, P>( cancel_rid: Option<ResourceId>, ) -> Result<(), AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -1180,7 +1121,7 @@ where permissions.check(&options, &path, "Deno.writeFile()")?; let cancel_handle = cancel_rid .and_then(|rid| state.resource_table.get::<CancelHandle>(rid).ok()); - (state.borrow::<Fs>().clone(), cancel_handle) + (state.borrow::<Arc<dyn FileSystem>>().clone(), cancel_handle) }; let fut = fs.write_file_async(path.clone(), options, data.to_vec()); @@ -1201,12 +1142,11 @@ where } #[op] -fn op_read_file_sync<Fs, P>( +fn op_read_file_sync<P>( state: &mut OpState, path: String, ) -> Result<ZeroCopyBuf, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -1214,20 +1154,19 @@ where let permissions = state.borrow_mut::<P>(); permissions.check_read(&path, "Deno.readFileSync()")?; - let fs = state.borrow::<Fs>(); - let buf = fs.read_file_sync(path).context("readfile")?; + let fs = state.borrow::<Arc<dyn FileSystem>>(); + let buf = fs.read_file_sync(&path).context("readfile")?; Ok(buf.into()) } #[op] -async fn op_read_file_async<Fs, P>( +async fn op_read_file_async<P>( state: Rc<RefCell<OpState>>, path: String, cancel_rid: Option<ResourceId>, ) -> Result<ZeroCopyBuf, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -1238,7 +1177,7 @@ where permissions.check_read(&path, "Deno.readFile()")?; let cancel_handle = cancel_rid .and_then(|rid| state.resource_table.get::<CancelHandle>(rid).ok()); - (state.borrow::<Fs>().clone(), cancel_handle) + (state.borrow::<Arc<dyn FileSystem>>().clone(), cancel_handle) }; let fut = fs.read_file_async(path.clone()); @@ -1259,12 +1198,11 @@ where } #[op] -fn op_read_file_text_sync<Fs, P>( +fn op_read_file_text_sync<P>( state: &mut OpState, path: String, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -1272,20 +1210,19 @@ where let permissions = state.borrow_mut::<P>(); permissions.check_read(&path, "Deno.readFileSync()")?; - let fs = state.borrow::<Fs>(); - let buf = fs.read_file_sync(path).context("readfile")?; + let fs = state.borrow::<Arc<dyn FileSystem>>(); + let buf = fs.read_file_sync(&path).context("readfile")?; Ok(string_from_utf8_lossy(buf)) } #[op] -async fn op_read_file_text_async<Fs, P>( +async fn op_read_file_text_async<P>( state: Rc<RefCell<OpState>>, path: String, cancel_rid: Option<ResourceId>, ) -> Result<String, AnyError> where - Fs: FileSystem + 'static, P: FsPermissions + 'static, { let path = PathBuf::from(path); @@ -1296,7 +1233,7 @@ where permissions.check_read(&path, "Deno.readFile()")?; let cancel_handle = cancel_rid .and_then(|rid| state.resource_table.get::<CancelHandle>(rid).ok()); - (state.borrow::<Fs>().clone(), cancel_handle) + (state.borrow::<Arc<dyn FileSystem>>().clone(), cancel_handle) }; let fut = fs.read_file_async(path.clone()); @@ -1340,106 +1277,75 @@ fn to_seek_from(offset: i64, whence: i32) -> Result<SeekFrom, AnyError> { } #[op] -fn op_seek_sync<Fs>( +fn op_seek_sync( state: &mut OpState, rid: ResourceId, offset: i64, whence: i32, -) -> Result<u64, AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ +) -> Result<u64, AnyError> { let pos = to_seek_from(offset, whence)?; - let file = state.resource_table.get::<Fs::File>(rid)?; + let file = FileResource::get_file(state, rid)?; let cursor = file.seek_sync(pos)?; Ok(cursor) } #[op] -async fn op_seek_async<Fs>( +async fn op_seek_async( state: Rc<RefCell<OpState>>, rid: ResourceId, offset: i64, whence: i32, -) -> Result<u64, AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ +) -> Result<u64, AnyError> { let pos = to_seek_from(offset, whence)?; - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; + let file = FileResource::get_file(&state.borrow(), rid)?; let cursor = file.seek_async(pos).await?; Ok(cursor) } #[op] -fn op_fdatasync_sync<Fs>( +fn op_fdatasync_sync( state: &mut OpState, rid: ResourceId, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(state, rid)?; file.datasync_sync()?; Ok(()) } #[op] -async fn op_fdatasync_async<Fs>( +async fn op_fdatasync_async( state: Rc<RefCell<OpState>>, rid: ResourceId, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(&state.borrow(), rid)?; file.datasync_async().await?; Ok(()) } #[op] -fn op_fsync_sync<Fs>( - state: &mut OpState, - rid: ResourceId, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.resource_table.get::<Fs::File>(rid)?; +fn op_fsync_sync(state: &mut OpState, rid: ResourceId) -> Result<(), AnyError> { + let file = FileResource::get_file(state, rid)?; file.sync_sync()?; Ok(()) } #[op] -async fn op_fsync_async<Fs>( +async fn op_fsync_async( state: Rc<RefCell<OpState>>, rid: ResourceId, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(&state.borrow(), rid)?; file.sync_async().await?; Ok(()) } #[op] -fn op_fstat_sync<Fs>( +fn op_fstat_sync( state: &mut OpState, rid: ResourceId, stat_out_buf: &mut [u32], -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(state, rid)?; let stat = file.stat_sync()?; let serializable_stat = SerializableStat::from(stat); serializable_stat.write(stat_out_buf); @@ -1447,143 +1353,107 @@ where } #[op] -async fn op_fstat_async<Fs>( +async fn op_fstat_async( state: Rc<RefCell<OpState>>, rid: ResourceId, -) -> Result<SerializableStat, AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; +) -> Result<SerializableStat, AnyError> { + let file = FileResource::get_file(&state.borrow(), rid)?; let stat = file.stat_async().await?; Ok(stat.into()) } #[op] -fn op_flock_sync<Fs>( +fn op_flock_sync( state: &mut OpState, rid: ResourceId, exclusive: bool, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ +) -> Result<(), AnyError> { check_unstable(state, "Deno.flockSync"); - let file = state.resource_table.get::<Fs::File>(rid)?; + let file = FileResource::get_file(state, rid)?; file.lock_sync(exclusive)?; Ok(()) } #[op] -async fn op_flock_async<Fs>( +async fn op_flock_async( state: Rc<RefCell<OpState>>, rid: ResourceId, exclusive: bool, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ +) -> Result<(), AnyError> { check_unstable2(&state, "Deno.flock"); - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; + let file = FileResource::get_file(&state.borrow(), rid)?; file.lock_async(exclusive).await?; Ok(()) } #[op] -fn op_funlock_sync<Fs>( +fn op_funlock_sync( state: &mut OpState, rid: ResourceId, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ +) -> Result<(), AnyError> { check_unstable(state, "Deno.funlockSync"); - let file = state.resource_table.get::<Fs::File>(rid)?; + let file = FileResource::get_file(state, rid)?; file.unlock_sync()?; Ok(()) } #[op] -async fn op_funlock_async<Fs>( +async fn op_funlock_async( state: Rc<RefCell<OpState>>, rid: ResourceId, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ +) -> Result<(), AnyError> { check_unstable2(&state, "Deno.funlock"); - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; + let file = FileResource::get_file(&state.borrow(), rid)?; file.unlock_async().await?; Ok(()) } #[op] -fn op_ftruncate_sync<Fs>( +fn op_ftruncate_sync( state: &mut OpState, rid: ResourceId, len: u64, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(state, rid)?; file.truncate_sync(len)?; Ok(()) } #[op] -async fn op_ftruncate_async<Fs>( +async fn op_ftruncate_async( state: Rc<RefCell<OpState>>, rid: ResourceId, len: u64, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(&state.borrow(), rid)?; file.truncate_async(len).await?; Ok(()) } #[op] -fn op_futime_sync<Fs>( +fn op_futime_sync( state: &mut OpState, rid: ResourceId, atime_secs: i64, atime_nanos: u32, mtime_secs: i64, mtime_nanos: u32, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(state, rid)?; file.utime_sync(atime_secs, atime_nanos, mtime_secs, mtime_nanos)?; Ok(()) } #[op] -async fn op_futime_async<Fs>( +async fn op_futime_async( state: Rc<RefCell<OpState>>, rid: ResourceId, atime_secs: i64, atime_nanos: u32, mtime_secs: i64, mtime_nanos: u32, -) -> Result<(), AnyError> -where - Fs: FileSystem + 'static, - Fs::File: Resource, -{ - let file = state.borrow().resource_table.get::<Fs::File>(rid)?; +) -> Result<(), AnyError> { + let file = FileResource::get_file(&state.borrow(), rid)?; file .utime_async(atime_secs, atime_nanos, mtime_secs, mtime_nanos) .await?; |