diff options
Diffstat (limited to 'runtime/ops/fs.rs')
-rw-r--r-- | runtime/ops/fs.rs | 430 |
1 files changed, 206 insertions, 224 deletions
diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs index bc166b4ad..d965f768d 100644 --- a/runtime/ops/fs.rs +++ b/runtime/ops/fs.rs @@ -7,9 +7,6 @@ use deno_core::error::bad_resource_id; use deno_core::error::custom_error; use deno_core::error::type_error; use deno_core::error::AnyError; -use deno_core::serde_json; -use deno_core::serde_json::json; -use deno_core::serde_json::Value; use deno_core::OpState; use deno_core::RcRef; use deno_core::ResourceId; @@ -18,6 +15,7 @@ use deno_crypto::rand::thread_rng; use deno_crypto::rand::Rng; use log::debug; use serde::Deserialize; +use serde::Serialize; use std::cell::RefCell; use std::convert::From; use std::env::{current_dir, set_current_dir, temp_dir}; @@ -183,27 +181,27 @@ fn op_open_sync( state: &mut OpState, args: OpenArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<ResourceId, AnyError> { let (path, open_options) = open_helper(state, args)?; let std_file = open_options.open(path)?; let tokio_file = tokio::fs::File::from_std(std_file); let resource = StdFileResource::fs_file(tokio_file); let rid = state.resource_table.add(resource); - Ok(json!(rid)) + Ok(rid) } async fn op_open_async( state: Rc<RefCell<OpState>>, args: OpenArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<ResourceId, AnyError> { let (path, open_options) = open_helper(&mut state.borrow_mut(), args)?; let tokio_file = tokio::fs::OpenOptions::from(open_options) .open(path) .await?; let resource = StdFileResource::fs_file(tokio_file); let rid = state.borrow_mut().resource_table.add(resource); - Ok(json!(rid)) + Ok(rid) } #[derive(Deserialize)] @@ -235,7 +233,7 @@ fn op_seek_sync( state: &mut OpState, args: SeekArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<u64, AnyError> { let (rid, seek_from) = seek_helper(args)?; let pos = StdFileResource::with(state, rid, |r| match r { Ok(std_file) => std_file.seek(seek_from).map_err(AnyError::from), @@ -243,14 +241,14 @@ fn op_seek_sync( "cannot seek on this type of resource".to_string(), )), })?; - Ok(json!(pos)) + Ok(pos) } async fn op_seek_async( state: Rc<RefCell<OpState>>, args: SeekArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<u64, AnyError> { let (rid, seek_from) = seek_helper(args)?; let resource = state @@ -268,35 +266,26 @@ async fn op_seek_async( .await; let pos = (*fs_file).0.as_mut().unwrap().seek(seek_from).await?; - Ok(json!(pos)) -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct FdatasyncArgs { - rid: ResourceId, + Ok(pos) } fn op_fdatasync_sync( state: &mut OpState, - args: FdatasyncArgs, + rid: ResourceId, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let rid = args.rid; +) -> Result<(), AnyError> { StdFileResource::with(state, rid, |r| match r { Ok(std_file) => std_file.sync_data().map_err(AnyError::from), Err(_) => Err(type_error("cannot sync this type of resource".to_string())), })?; - Ok(json!({})) + Ok(()) } async fn op_fdatasync_async( state: Rc<RefCell<OpState>>, - args: FdatasyncArgs, + rid: ResourceId, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let rid = args.rid; - +) -> Result<(), AnyError> { let resource = state .borrow_mut() .resource_table @@ -312,35 +301,26 @@ async fn op_fdatasync_async( .await; (*fs_file).0.as_mut().unwrap().sync_data().await?; - Ok(json!({})) -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct FsyncArgs { - rid: ResourceId, + Ok(()) } fn op_fsync_sync( state: &mut OpState, - args: FsyncArgs, + rid: ResourceId, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let rid = args.rid; +) -> Result<(), AnyError> { StdFileResource::with(state, rid, |r| match r { Ok(std_file) => std_file.sync_all().map_err(AnyError::from), Err(_) => Err(type_error("cannot sync this type of resource".to_string())), })?; - Ok(json!({})) + Ok(()) } async fn op_fsync_async( state: Rc<RefCell<OpState>>, - args: FsyncArgs, + rid: ResourceId, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let rid = args.rid; - +) -> Result<(), AnyError> { let resource = state .borrow_mut() .resource_table @@ -356,37 +336,28 @@ async fn op_fsync_async( .await; (*fs_file).0.as_mut().unwrap().sync_all().await?; - Ok(json!({})) -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct FstatArgs { - rid: ResourceId, + Ok(()) } fn op_fstat_sync( state: &mut OpState, - args: FstatArgs, + rid: ResourceId, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<FsStat, AnyError> { super::check_unstable(state, "Deno.fstat"); - let metadata = StdFileResource::with(state, args.rid, |r| match r { + let metadata = StdFileResource::with(state, rid, |r| match r { Ok(std_file) => std_file.metadata().map_err(AnyError::from), Err(_) => Err(type_error("cannot stat this type of resource".to_string())), })?; - Ok(get_stat_json(metadata)) + Ok(get_stat(metadata)) } async fn op_fstat_async( state: Rc<RefCell<OpState>>, - args: FstatArgs, + rid: ResourceId, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<FsStat, AnyError> { super::check_unstable2(&state, "Deno.fstat"); - - let rid = args.rid; - let resource = state .borrow_mut() .resource_table @@ -402,27 +373,22 @@ async fn op_fstat_async( .await; let metadata = (*fs_file).0.as_mut().unwrap().metadata().await?; - Ok(get_stat_json(metadata)) -} - -#[derive(Deserialize)] -pub struct UmaskArgs { - mask: Option<u32>, + Ok(get_stat(metadata)) } #[allow(clippy::unnecessary_wraps)] fn op_umask( state: &mut OpState, - args: UmaskArgs, + mask: Option<u32>, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<u32, AnyError> { super::check_unstable(state, "Deno.umask"); // TODO implement umask for Windows // see https://github.com/nodejs/node/blob/master/src/node_process_methods.cc // and https://docs.microsoft.com/fr-fr/cpp/c-runtime-library/reference/umask?view=vs-2019 #[cfg(not(unix))] { - let _ = args.mask; // avoid unused warning. + let _ = mask; // avoid unused warning. Err(not_supported()) } #[cfg(unix)] @@ -430,7 +396,7 @@ fn op_umask( use nix::sys::stat::mode_t; use nix::sys::stat::umask; use nix::sys::stat::Mode; - let r = if let Some(mask) = args.mask { + let r = if let Some(mask) = mask { // If mask provided, return previous. umask(Mode::from_bits_truncate(mask as mode_t)) } else { @@ -439,24 +405,19 @@ fn op_umask( let _ = umask(prev); prev }; - Ok(json!(r.bits() as u32)) + Ok(r.bits() as u32) } } -#[derive(Deserialize)] -pub struct ChdirArgs { - directory: String, -} - fn op_chdir( state: &mut OpState, - args: ChdirArgs, + directory: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let d = PathBuf::from(&args.directory); +) -> Result<(), AnyError> { + let d = PathBuf::from(&directory); state.borrow::<Permissions>().read.check(&d)?; set_current_dir(&d)?; - Ok(json!({})) + Ok(()) } #[derive(Deserialize)] @@ -471,7 +432,7 @@ fn op_mkdir_sync( state: &mut OpState, args: MkdirArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode.unwrap_or(0o777) & 0o777; state.borrow::<Permissions>().write.check(&path)?; @@ -484,14 +445,14 @@ fn op_mkdir_sync( builder.mode(mode); } builder.create(path)?; - Ok(json!({})) + Ok(()) } async fn op_mkdir_async( state: Rc<RefCell<OpState>>, args: MkdirArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode.unwrap_or(0o777) & 0o777; @@ -510,7 +471,7 @@ async fn op_mkdir_async( builder.mode(mode); } builder.create(path)?; - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -527,7 +488,7 @@ fn op_chmod_sync( state: &mut OpState, args: ChmodArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode & 0o777; @@ -538,7 +499,7 @@ fn op_chmod_sync( use std::os::unix::fs::PermissionsExt; let permissions = PermissionsExt::from_mode(mode); std::fs::set_permissions(&path, permissions)?; - Ok(json!({})) + Ok(()) } // TODO Implement chmod for Windows (#4357) #[cfg(not(unix))] @@ -553,7 +514,7 @@ async fn op_chmod_async( state: Rc<RefCell<OpState>>, args: ChmodArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode & 0o777; @@ -569,7 +530,7 @@ async fn op_chmod_async( use std::os::unix::fs::PermissionsExt; let permissions = PermissionsExt::from_mode(mode); std::fs::set_permissions(&path, permissions)?; - Ok(json!({})) + Ok(()) } // TODO Implement chmod for Windows (#4357) #[cfg(not(unix))] @@ -595,7 +556,7 @@ fn op_chown_sync( state: &mut OpState, args: ChownArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = Path::new(&args.path).to_path_buf(); state.borrow::<Permissions>().write.check(&path)?; debug!( @@ -610,7 +571,7 @@ fn op_chown_sync( let nix_uid = args.uid.map(Uid::from_raw); let nix_gid = args.gid.map(Gid::from_raw); chown(&path, nix_uid, nix_gid)?; - Ok(json!({})) + Ok(()) } // TODO Implement chown for Windows #[cfg(not(unix))] @@ -623,7 +584,7 @@ async fn op_chown_async( state: Rc<RefCell<OpState>>, args: ChownArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = Path::new(&args.path).to_path_buf(); { @@ -644,7 +605,7 @@ async fn op_chown_async( let nix_uid = args.uid.map(Uid::from_raw); let nix_gid = args.gid.map(Gid::from_raw); chown(&path, nix_uid, nix_gid)?; - Ok(json!({})) + Ok(()) } // TODO Implement chown for Windows #[cfg(not(unix))] @@ -665,7 +626,7 @@ fn op_remove_sync( state: &mut OpState, args: RemoveArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = PathBuf::from(&args.path); let recursive = args.recursive; @@ -700,14 +661,14 @@ fn op_remove_sync( // pipes, sockets, etc... std::fs::remove_file(&path)?; } - Ok(json!({})) + Ok(()) } async fn op_remove_async( state: Rc<RefCell<OpState>>, args: RemoveArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = PathBuf::from(&args.path); let recursive = args.recursive; @@ -746,7 +707,7 @@ async fn op_remove_async( // pipes, sockets, etc... std::fs::remove_file(&path)?; } - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -763,7 +724,7 @@ fn op_copy_file_sync( state: &mut OpState, args: CopyFileArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let from = PathBuf::from(&args.from); let to = PathBuf::from(&args.to); @@ -781,14 +742,14 @@ fn op_copy_file_sync( // returns size of from as u64 (we ignore) std::fs::copy(&from, &to)?; - Ok(json!({})) + Ok(()) } async fn op_copy_file_async( state: Rc<RefCell<OpState>>, args: CopyFileArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let from = PathBuf::from(&args.from); let to = PathBuf::from(&args.to); @@ -810,29 +771,50 @@ async fn op_copy_file_async( // returns size of from as u64 (we ignore) std::fs::copy(&from, &to)?; - Ok(json!({})) + Ok(()) }) .await .unwrap() } -fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> Value { +fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> Option<u64> { match maybe_time { Ok(time) => { let msec = time .duration_since(UNIX_EPOCH) - .map(|t| t.as_secs_f64() * 1000f64) - .unwrap_or_else(|err| err.duration().as_secs_f64() * -1000f64); - serde_json::Number::from_f64(msec) - .map(Value::Number) - .unwrap_or(Value::Null) + .map(|t| t.as_millis() as u64) + .unwrap_or_else(|err| err.duration().as_millis() as u64); + Some(msec) } - Err(_) => Value::Null, + Err(_) => None, } } +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct FsStat { + is_file: bool, + is_directory: bool, + is_symlink: bool, + size: u64, + // In milliseconds, like JavaScript. Available on both Unix or Windows. + mtime: Option<u64>, + atime: Option<u64>, + birthtime: Option<u64>, + // Following are only valid under Unix. + dev: u64, + ino: u64, + mode: u32, + nlink: u64, + uid: u32, + gid: u32, + rdev: u64, + blksize: u64, + blocks: u64, +} + #[inline(always)] -fn get_stat_json(metadata: std::fs::Metadata) -> Value { +fn get_stat(metadata: std::fs::Metadata) -> FsStat { // Unix stat member (number types only). 0 if not on unix. macro_rules! usm { ($member:ident) => {{ @@ -849,29 +831,26 @@ fn get_stat_json(metadata: std::fs::Metadata) -> Value { #[cfg(unix)] use std::os::unix::fs::MetadataExt; - let json_val = json!({ - "isFile": metadata.is_file(), - "isDirectory": metadata.is_dir(), - "isSymlink": metadata.file_type().is_symlink(), - "size": metadata.len(), + FsStat { + is_file: metadata.is_file(), + is_directory: metadata.is_dir(), + is_symlink: metadata.file_type().is_symlink(), + size: metadata.len(), // In milliseconds, like JavaScript. Available on both Unix or Windows. - "mtime": to_msec(metadata.modified()), - "atime": to_msec(metadata.accessed()), - "birthtime": to_msec(metadata.created()), + mtime: to_msec(metadata.modified()), + atime: to_msec(metadata.accessed()), + birthtime: to_msec(metadata.created()), // Following are only valid under Unix. - "dev": usm!(dev), - "ino": usm!(ino), - "mode": usm!(mode), - "nlink": usm!(nlink), - "uid": usm!(uid), - "gid": usm!(gid), - "rdev": usm!(rdev), - // TODO(kevinkassimo): *time_nsec requires BigInt. - // Probably should be treated as String if we need to add them. - "blksize": usm!(blksize), - "blocks": usm!(blocks), - }); - json_val + dev: usm!(dev), + ino: usm!(ino), + mode: usm!(mode), + nlink: usm!(nlink), + uid: usm!(uid), + gid: usm!(gid), + rdev: usm!(rdev), + blksize: usm!(blksize), + blocks: usm!(blocks), + } } #[derive(Deserialize)] @@ -885,7 +864,7 @@ fn op_stat_sync( state: &mut OpState, args: StatArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<FsStat, AnyError> { let path = PathBuf::from(&args.path); let lstat = args.lstat; state.borrow::<Permissions>().read.check(&path)?; @@ -895,14 +874,14 @@ fn op_stat_sync( } else { std::fs::metadata(&path)? }; - Ok(get_stat_json(metadata)) + Ok(get_stat(metadata)) } async fn op_stat_async( state: Rc<RefCell<OpState>>, args: StatArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<FsStat, AnyError> { let path = PathBuf::from(&args.path); let lstat = args.lstat; @@ -918,24 +897,18 @@ async fn op_stat_async( } else { std::fs::metadata(&path)? }; - Ok(get_stat_json(metadata)) + Ok(get_stat(metadata)) }) .await .unwrap() } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct RealpathArgs { - path: String, -} - fn op_realpath_sync( state: &mut OpState, - args: RealpathArgs, + path: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let path = PathBuf::from(&args.path); +) -> Result<String, AnyError> { + let path = PathBuf::from(&path); let permissions = state.borrow::<Permissions>(); permissions.read.check(&path)?; @@ -948,15 +921,15 @@ fn op_realpath_sync( // CreateFile and GetFinalPathNameByHandle on Windows let realpath = canonicalize_path(&path)?; let realpath_str = into_string(realpath.into_os_string())?; - Ok(json!(realpath_str)) + Ok(realpath_str) } async fn op_realpath_async( state: Rc<RefCell<OpState>>, - args: RealpathArgs, + path: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let path = PathBuf::from(&args.path); +) -> Result<String, AnyError> { + let path = PathBuf::from(&path); { let state = state.borrow(); @@ -973,24 +946,27 @@ async fn op_realpath_async( // CreateFile and GetFinalPathNameByHandle on Windows let realpath = canonicalize_path(&path)?; let realpath_str = into_string(realpath.into_os_string())?; - Ok(json!(realpath_str)) + Ok(realpath_str) }) .await .unwrap() } -#[derive(Deserialize)] +#[derive(Serialize)] #[serde(rename_all = "camelCase")] -pub struct ReadDirArgs { - path: String, +pub struct DirEntry { + name: String, + is_file: bool, + is_directory: bool, + is_symlink: bool, } fn op_read_dir_sync( state: &mut OpState, - args: ReadDirArgs, + path: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let path = PathBuf::from(&args.path); +) -> Result<Vec<DirEntry>, AnyError> { + let path = PathBuf::from(&path); state.borrow::<Permissions>().read.check(&path)?; @@ -1000,27 +976,33 @@ fn op_read_dir_sync( let entry = entry.unwrap(); // Not all filenames can be encoded as UTF-8. Skip those for now. if let Ok(name) = into_string(entry.file_name()) { - Some(json!({ - "name": name, - "isFile": entry.file_type().map_or(false, |file_type| file_type.is_file()), - "isDirectory": entry.file_type().map_or(false, |file_type| file_type.is_dir()), - "isSymlink": entry.file_type().map_or(false, |file_type| file_type.is_symlink()), - })) + Some(DirEntry { + name, + is_file: entry + .file_type() + .map_or(false, |file_type| file_type.is_file()), + is_directory: entry + .file_type() + .map_or(false, |file_type| file_type.is_dir()), + is_symlink: entry + .file_type() + .map_or(false, |file_type| file_type.is_symlink()), + }) } else { None } }) - .collect(); + .collect(); - Ok(json!({ "entries": entries })) + Ok(entries) } async fn op_read_dir_async( state: Rc<RefCell<OpState>>, - args: ReadDirArgs, + path: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let path = PathBuf::from(&args.path); +) -> Result<Vec<DirEntry>, AnyError> { + let path = PathBuf::from(&path); { let state = state.borrow(); state.borrow::<Permissions>().read.check(&path)?; @@ -1032,22 +1014,28 @@ async fn op_read_dir_async( let entry = entry.unwrap(); // Not all filenames can be encoded as UTF-8. Skip those for now. if let Ok(name) = into_string(entry.file_name()) { - Some(json!({ - "name": name, - "isFile": entry.file_type().map_or(false, |file_type| file_type.is_file()), - "isDirectory": entry.file_type().map_or(false, |file_type| file_type.is_dir()), - "isSymlink": entry.file_type().map_or(false, |file_type| file_type.is_symlink()), - })) + Some(DirEntry { + name, + is_file: entry + .file_type() + .map_or(false, |file_type| file_type.is_file()), + is_directory: entry + .file_type() + .map_or(false, |file_type| file_type.is_dir()), + is_symlink: entry + .file_type() + .map_or(false, |file_type| file_type.is_symlink()), + }) } else { None } }) - .collect(); + .collect(); - Ok(json!({ "entries": entries })) + Ok(entries) }) .await - .unwrap() + .unwrap() } #[derive(Deserialize)] @@ -1061,7 +1049,7 @@ fn op_rename_sync( state: &mut OpState, args: RenameArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1071,14 +1059,14 @@ fn op_rename_sync( permissions.write.check(&newpath)?; debug!("op_rename_sync {} {}", oldpath.display(), newpath.display()); std::fs::rename(&oldpath, &newpath)?; - Ok(json!({})) + Ok(()) } async fn op_rename_async( state: Rc<RefCell<OpState>>, args: RenameArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); { @@ -1095,7 +1083,7 @@ async fn op_rename_async( newpath.display() ); std::fs::rename(&oldpath, &newpath)?; - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -1112,7 +1100,7 @@ fn op_link_sync( state: &mut OpState, args: LinkArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1124,14 +1112,14 @@ fn op_link_sync( debug!("op_link_sync {} {}", oldpath.display(), newpath.display()); std::fs::hard_link(&oldpath, &newpath)?; - Ok(json!({})) + Ok(()) } async fn op_link_async( state: Rc<RefCell<OpState>>, args: LinkArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1147,7 +1135,7 @@ async fn op_link_async( tokio::task::spawn_blocking(move || { debug!("op_link_async {} {}", oldpath.display(), newpath.display()); std::fs::hard_link(&oldpath, &newpath)?; - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -1173,7 +1161,7 @@ fn op_symlink_sync( state: &mut OpState, args: SymlinkArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1188,7 +1176,7 @@ fn op_symlink_sync( { use std::os::unix::fs::symlink; symlink(&oldpath, &newpath)?; - Ok(json!({})) + Ok(()) } #[cfg(not(unix))] { @@ -1214,7 +1202,7 @@ fn op_symlink_sync( } } }; - Ok(json!({})) + Ok(()) } } @@ -1222,7 +1210,7 @@ async fn op_symlink_async( state: Rc<RefCell<OpState>>, args: SymlinkArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1237,7 +1225,7 @@ async fn op_symlink_async( { use std::os::unix::fs::symlink; symlink(&oldpath, &newpath)?; - Ok(json!({})) + Ok(()) } #[cfg(not(unix))] { @@ -1263,40 +1251,34 @@ async fn op_symlink_async( } } }; - Ok(json!({})) + Ok(()) } }) .await .unwrap() } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ReadLinkArgs { - path: String, -} - fn op_read_link_sync( state: &mut OpState, - args: ReadLinkArgs, + path: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let path = PathBuf::from(&args.path); +) -> Result<String, AnyError> { + let path = PathBuf::from(&path); state.borrow::<Permissions>().read.check(&path)?; debug!("op_read_link_value {}", path.display()); let target = std::fs::read_link(&path)?.into_os_string(); let targetstr = into_string(target)?; - Ok(json!(targetstr)) + Ok(targetstr) } async fn op_read_link_async( state: Rc<RefCell<OpState>>, - args: ReadLinkArgs, + path: String, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { - let path = PathBuf::from(&args.path); +) -> Result<String, AnyError> { + let path = PathBuf::from(&path); { let state = state.borrow(); state.borrow::<Permissions>().read.check(&path)?; @@ -1305,7 +1287,7 @@ async fn op_read_link_async( debug!("op_read_link_async {}", path.display()); let target = std::fs::read_link(&path)?.into_os_string(); let targetstr = into_string(target)?; - Ok(json!(targetstr)) + Ok(targetstr) }) .await .unwrap() @@ -1322,7 +1304,7 @@ fn op_ftruncate_sync( state: &mut OpState, args: FtruncateArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { super::check_unstable(state, "Deno.ftruncate"); let rid = args.rid; let len = args.len as u64; @@ -1330,14 +1312,14 @@ fn op_ftruncate_sync( Ok(std_file) => std_file.set_len(len).map_err(AnyError::from), Err(_) => Err(type_error("cannot truncate this type of resource")), })?; - Ok(json!({})) + Ok(()) } async fn op_ftruncate_async( state: Rc<RefCell<OpState>>, args: FtruncateArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { super::check_unstable2(&state, "Deno.ftruncate"); let rid = args.rid; let len = args.len as u64; @@ -1357,7 +1339,7 @@ async fn op_ftruncate_async( .await; (*fs_file).0.as_mut().unwrap().set_len(len).await?; - Ok(json!({})) + Ok(()) } #[derive(Deserialize)] @@ -1371,7 +1353,7 @@ fn op_truncate_sync( state: &mut OpState, args: TruncateArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = PathBuf::from(&args.path); let len = args.len; @@ -1380,14 +1362,14 @@ fn op_truncate_sync( debug!("op_truncate_sync {} {}", path.display(), len); let f = std::fs::OpenOptions::new().write(true).open(&path)?; f.set_len(len)?; - Ok(json!({})) + Ok(()) } async fn op_truncate_async( state: Rc<RefCell<OpState>>, args: TruncateArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { let path = PathBuf::from(&args.path); let len = args.len; { @@ -1398,7 +1380,7 @@ async fn op_truncate_async( debug!("op_truncate_async {} {}", path.display(), len); let f = std::fs::OpenOptions::new().write(true).open(&path)?; f.set_len(len)?; - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -1461,7 +1443,7 @@ fn op_make_temp_dir_sync( state: &mut OpState, args: MakeTempArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<String, AnyError> { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); let suffix = args.suffix.map(String::from); @@ -1483,14 +1465,14 @@ fn op_make_temp_dir_sync( )?; let path_str = into_string(path.into_os_string())?; - Ok(json!(path_str)) + Ok(path_str) } async fn op_make_temp_dir_async( state: Rc<RefCell<OpState>>, args: MakeTempArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<String, AnyError> { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); let suffix = args.suffix.map(String::from); @@ -1514,7 +1496,7 @@ async fn op_make_temp_dir_async( )?; let path_str = into_string(path.into_os_string())?; - Ok(json!(path_str)) + Ok(path_str) }) .await .unwrap() @@ -1524,7 +1506,7 @@ fn op_make_temp_file_sync( state: &mut OpState, args: MakeTempArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<String, AnyError> { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); let suffix = args.suffix.map(String::from); @@ -1546,14 +1528,14 @@ fn op_make_temp_file_sync( )?; let path_str = into_string(path.into_os_string())?; - Ok(json!(path_str)) + Ok(path_str) } async fn op_make_temp_file_async( state: Rc<RefCell<OpState>>, args: MakeTempArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<String, AnyError> { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); let suffix = args.suffix.map(String::from); @@ -1577,7 +1559,7 @@ async fn op_make_temp_file_async( )?; let path_str = into_string(path.into_os_string())?; - Ok(json!(path_str)) + Ok(path_str) }) .await .unwrap() @@ -1595,7 +1577,7 @@ fn op_futime_sync( state: &mut OpState, args: FutimeArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { super::check_unstable(state, "Deno.futimeSync"); let rid = args.rid; let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1); @@ -1611,14 +1593,14 @@ fn op_futime_sync( )), })?; - Ok(json!({})) + Ok(()) } async fn op_futime_async( state: Rc<RefCell<OpState>>, args: FutimeArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { super::check_unstable2(&state, "Deno.futime"); let rid = args.rid; let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1); @@ -1649,7 +1631,7 @@ async fn op_futime_async( tokio::task::spawn_blocking(move || { filetime::set_file_handle_times(&std_file, Some(atime), Some(mtime))?; - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -1667,7 +1649,7 @@ fn op_utime_sync( state: &mut OpState, args: UtimeArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { super::check_unstable(state, "Deno.utime"); let path = PathBuf::from(&args.path); @@ -1676,14 +1658,14 @@ fn op_utime_sync( state.borrow::<Permissions>().write.check(&path)?; filetime::set_file_times(path, atime, mtime)?; - Ok(json!({})) + Ok(()) } async fn op_utime_async( state: Rc<RefCell<OpState>>, args: UtimeArgs, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<(), AnyError> { super::check_unstable(&state.borrow(), "Deno.utime"); let path = PathBuf::from(&args.path); @@ -1694,7 +1676,7 @@ async fn op_utime_async( tokio::task::spawn_blocking(move || { filetime::set_file_times(path, atime, mtime)?; - Ok(json!({})) + Ok(()) }) .await .unwrap() @@ -1702,14 +1684,14 @@ async fn op_utime_async( fn op_cwd( state: &mut OpState, - _args: Value, + _args: (), _zero_copy: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<String, AnyError> { let path = current_dir()?; state .borrow::<Permissions>() .read .check_blind(&path, "CWD")?; let path_str = into_string(path.into_os_string())?; - Ok(json!(path_str)) + Ok(path_str) } |