diff options
Diffstat (limited to 'runtime/ops/fs.rs')
-rw-r--r-- | runtime/ops/fs.rs | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs index f6b9a58eb..3cac56443 100644 --- a/runtime/ops/fs.rs +++ b/runtime/ops/fs.rs @@ -267,14 +267,6 @@ async fn op_write_file_async( data: ZeroCopyBuf, cancel_rid: Option<ResourceId>, ) -> Result<(), AnyError> { - let cancel_handle = match cancel_rid { - Some(cancel_rid) => state - .borrow_mut() - .resource_table - .get::<CancelHandle>(cancel_rid) - .ok(), - None => None, - }; let (path, open_options) = open_helper( &mut state.borrow_mut(), &path, @@ -282,15 +274,30 @@ async fn op_write_file_async( Some(&write_open_options(create, append, create_new)), "Deno.writeFile()", )?; + let write_future = tokio::task::spawn_blocking(move || { write_file(&path, open_options, mode, data) }); + + let cancel_handle = cancel_rid.and_then(|rid| { + state + .borrow_mut() + .resource_table + .get::<CancelHandle>(rid) + .ok() + }); + if let Some(cancel_handle) = cancel_handle { - write_future.or_cancel(cancel_handle).await???; - } else { - write_future.await??; + let write_future_rv = write_future.or_cancel(cancel_handle).await; + + if let Some(cancel_rid) = cancel_rid { + state.borrow_mut().resource_table.close(cancel_rid).ok(); + }; + + return write_future_rv??; } - Ok(()) + + write_future.await? } fn write_file( @@ -2046,20 +2053,31 @@ async fn op_readfile_async( .borrow_mut::<PermissionsContainer>() .check_read(path, "Deno.readFile()")?; } - let fut = tokio::task::spawn_blocking(move || { + + let read_future = tokio::task::spawn_blocking(move || { let path = Path::new(&path); Ok(std::fs::read(path).map(ZeroCopyBuf::from)?) }); - if let Some(cancel_rid) = cancel_rid { - let cancel_handle = state + + let cancel_handle = cancel_rid.and_then(|rid| { + state .borrow_mut() .resource_table - .get::<CancelHandle>(cancel_rid); - if let Ok(cancel_handle) = cancel_handle { - return fut.or_cancel(cancel_handle).await??; - } + .get::<CancelHandle>(rid) + .ok() + }); + + if let Some(cancel_handle) = cancel_handle { + let read_future_rv = read_future.or_cancel(cancel_handle).await; + + if let Some(cancel_rid) = cancel_rid { + state.borrow_mut().resource_table.close(cancel_rid).ok(); + }; + + return read_future_rv??; } - fut.await? + + read_future.await? } #[op] @@ -2075,20 +2093,31 @@ async fn op_readfile_text_async( .borrow_mut::<PermissionsContainer>() .check_read(path, "Deno.readTextFile()")?; } - let fut = tokio::task::spawn_blocking(move || { + + let read_future = tokio::task::spawn_blocking(move || { let path = Path::new(&path); Ok(string_from_utf8_lossy(std::fs::read(path)?)) }); - if let Some(cancel_rid) = cancel_rid { - let cancel_handle = state + + let cancel_handle = cancel_rid.and_then(|rid| { + state .borrow_mut() .resource_table - .get::<CancelHandle>(cancel_rid); - if let Ok(cancel_handle) = cancel_handle { - return fut.or_cancel(cancel_handle).await??; - } + .get::<CancelHandle>(rid) + .ok() + }); + + if let Some(cancel_handle) = cancel_handle { + let read_future_rv = read_future.or_cancel(cancel_handle).await; + + if let Some(cancel_rid) = cancel_rid { + state.borrow_mut().resource_table.close(cancel_rid).ok(); + }; + + return read_future_rv??; } - fut.await? + + read_future.await? } // Like String::from_utf8_lossy but operates on owned values |