diff options
Diffstat (limited to 'cli/ops')
-rw-r--r-- | cli/ops/process.rs | 20 | ||||
-rw-r--r-- | cli/ops/tty.rs | 47 |
2 files changed, 37 insertions, 30 deletions
diff --git a/cli/ops/process.rs b/cli/ops/process.rs index 980147832..e84418a68 100644 --- a/cli/ops/process.rs +++ b/cli/ops/process.rs @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; -use super::io::{StreamResource, StreamResourceHolder}; +use super::io::{std_file_resource, StreamResource, StreamResourceHolder}; use crate::op_error::OpError; use crate::signal::kill; use crate::state::State; @@ -22,19 +22,11 @@ pub fn init(i: &mut Isolate, s: &State) { fn clone_file(rid: u32, state: &State) -> Result<std::fs::File, OpError> { let mut state = state.borrow_mut(); - let repr_holder = state - .resource_table - .get_mut::<StreamResourceHolder>(rid) - .ok_or_else(OpError::bad_resource_id)?; - match repr_holder.resource { - StreamResource::FsFile(Some((ref mut file, _))) => { - let tokio_file = futures::executor::block_on(file.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - Ok(std_file) - } - StreamResource::FsFile(None) => Err(OpError::resource_unavailable()), - _ => Err(OpError::bad_resource_id()), - } + + std_file_resource(&mut state.resource_table, rid, move |r| match r { + Ok(std_file) => std_file.try_clone().map_err(OpError::from), + Err(_) => Err(OpError::bad_resource_id()), + }) } fn subprocess_stdio_map(s: &str) -> std::process::Stdio { diff --git a/cli/ops/tty.rs b/cli/ops/tty.rs index 461c1ca30..90fa2bde3 100644 --- a/cli/ops/tty.rs +++ b/cli/ops/tty.rs @@ -65,22 +65,41 @@ pub fn op_set_raw( use winapi::shared::minwindef::FALSE; use winapi::um::{consoleapi, handleapi}; - let state = state_.borrow_mut(); - let resource_holder = state.resource_table.get::<StreamResourceHolder>(rid); + let mut state = state_.borrow_mut(); + let resource_holder = + state.resource_table.get_mut::<StreamResourceHolder>(rid); if resource_holder.is_none() { return Err(OpError::bad_resource_id()); } + let resource_holder = resource_holder.unwrap(); // For now, only stdin. - let handle = match &resource_holder.unwrap().resource { + let handle = match &mut resource_holder.resource { StreamResource::Stdin(_, _) => std::io::stdin().as_raw_handle(), - StreamResource::FsFile(None) => { - return Err(OpError::resource_unavailable()) - } - StreamResource::FsFile(Some((f, _))) => { - let tokio_file = futures::executor::block_on(f.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - std_file.as_raw_handle() + StreamResource::FsFile(ref mut option_file_metadata) => { + if let Some((tokio_file, metadata)) = option_file_metadata.take() { + match tokio_file.try_into_std() { + Ok(std_file) => { + let raw_handle = std_file.as_raw_handle(); + // Turn the std_file handle back into a tokio file, put it back + // in the resource table. + let tokio_file = tokio::fs::File::from_std(std_file); + resource_holder.resource = + StreamResource::FsFile(Some((tokio_file, metadata))); + // return the result. + raw_handle + } + Err(tokio_file) => { + // This function will return an error containing the file if + // some operation is in-flight. + resource_holder.resource = + StreamResource::FsFile(Some((tokio_file, metadata))); + return Err(OpError::resource_unavailable()); + } + } + } else { + return Err(OpError::resource_unavailable()); + } } _ => { return Err(OpError::bad_resource_id()); @@ -127,9 +146,7 @@ pub fn op_set_raw( (std::io::stdin().as_raw_fd(), &mut metadata.mode) } StreamResource::FsFile(Some((f, ref mut metadata))) => { - let tokio_file = futures::executor::block_on(f.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - (std_file.as_raw_fd(), &mut metadata.tty.mode) + (f.as_raw_fd(), &mut metadata.tty.mode) } StreamResource::FsFile(None) => { return Err(OpError::resource_unavailable()) @@ -173,9 +190,7 @@ pub fn op_set_raw( (std::io::stdin().as_raw_fd(), &mut metadata.mode) } StreamResource::FsFile(Some((f, ref mut metadata))) => { - let tokio_file = futures::executor::block_on(f.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - (std_file.as_raw_fd(), &mut metadata.tty.mode) + (f.as_raw_fd(), &mut metadata.tty.mode) } StreamResource::FsFile(None) => { return Err(OpError::resource_unavailable()); |