diff options
Diffstat (limited to 'cli/ops/fs.rs')
-rw-r--r-- | cli/ops/fs.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index 9ca4b31ae..9417c112d 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -22,6 +22,7 @@ use rand::{thread_rng, Rng}; pub fn init(i: &mut CoreIsolate, s: &State) { i.register_op("op_open", s.stateful_json_op2(op_open)); i.register_op("op_seek", s.stateful_json_op2(op_seek)); + i.register_op("op_fsync", s.stateful_json_op2(op_fsync)); i.register_op("op_umask", s.stateful_json_op(op_umask)); i.register_op("op_chdir", s.stateful_json_op(op_chdir)); i.register_op("op_mkdir", s.stateful_json_op(op_mkdir)); @@ -206,6 +207,50 @@ fn op_seek( } #[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct FsyncArgs { + promise_id: Option<u64>, + rid: i32, +} + +fn op_fsync( + isolate_state: &mut CoreIsolateState, + state: &State, + args: Value, + _zero_copy: &mut [ZeroCopyBuf], +) -> Result<JsonOp, OpError> { + state.check_unstable("Deno.fsync"); + let args: FsyncArgs = serde_json::from_value(args)?; + let rid = args.rid as u32; + + let resource_table = isolate_state.resource_table.clone(); + let is_sync = args.promise_id.is_none(); + + if is_sync { + let mut resource_table = resource_table.borrow_mut(); + std_file_resource(&mut resource_table, rid, |r| match r { + Ok(std_file) => std_file.sync_all().map_err(OpError::from), + Err(_) => Err(OpError::type_error( + "cannot sync this type of resource".to_string(), + )), + })?; + Ok(JsonOp::Sync(json!({}))) + } else { + let fut = async move { + let mut resource_table = resource_table.borrow_mut(); + std_file_resource(&mut resource_table, rid, |r| match r { + Ok(std_file) => std_file.sync_all().map_err(OpError::from), + Err(_) => Err(OpError::type_error( + "cannot sync this type of resource".to_string(), + )), + })?; + Ok(json!({})) + }; + Ok(JsonOp::Async(fut.boxed_local())) + } +} + +#[derive(Deserialize)] struct UmaskArgs { mask: Option<u32>, } |