diff options
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/process/pipes')
4 files changed, 148 insertions, 44 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs index d8ced43..5b85813 100644 --- a/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs +++ b/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs @@ -1,11 +1,11 @@ //! ref: composer/vendor/symfony/process/Pipes/AbstractPipes.php use indexmap::IndexMap; -use shirabe_php_shim::{self as php, PhpMixed}; +use shirabe_php_shim::{self as php, PhpMixed, PhpResource}; #[derive(Debug)] pub struct AbstractPipes { - pub pipes: PhpMixed, + pub pipes: IndexMap<i64, PhpResource>, input_buffer: String, input: PhpMixed, @@ -17,6 +17,8 @@ impl AbstractPipes { pub fn new(input: PhpMixed) -> Self { let mut input_buffer = String::new(); let stored_input; + // TODO(plugin): `$input instanceof \Iterator` is not modeled. `is_resource` on a PhpMixed is + // always false, so the resource branch never stores input here. if php::is_resource(&input) { stored_input = input; } else if let PhpMixed::String(s) = &input { @@ -28,7 +30,7 @@ impl AbstractPipes { } Self { - pipes: PhpMixed::List(Vec::new()), + pipes: IndexMap::new(), input_buffer, input: stored_input, blocked: true, @@ -37,9 +39,10 @@ impl AbstractPipes { } pub fn close(&mut self) { - // TODO(phase-d): each pipe is a PHP stream resource that should be fclose()d, but the pipe - // list is a PhpMixed that cannot hold a PhpResource; the handles are dropped instead. - self.pipes = PhpMixed::List(Vec::new()); + for (_, pipe) in &self.pipes { + php::fclose(pipe); + } + self.pipes = IndexMap::new(); } /// Returns true if a system call has been interrupted. @@ -54,14 +57,52 @@ impl AbstractPipes { /// Unblocks streams. pub(crate) fn unblock(&mut self) { - let _ = &self.input_buffer; - let _ = &self.blocked; - todo!() + if !self.blocked { + return; + } + + for (_, pipe) in &self.pipes { + php::stream_set_blocking(pipe, false); + } + // The `is_resource($this->input)` branch does not apply: `input` is never a resource in this + // port (is_resource on a PhpMixed is always false). + + self.blocked = false; } /// Writes input to stdin. - pub(crate) fn write(&mut self) -> Option<IndexMap<i64, PhpMixed>> { - todo!() + pub(crate) fn write(&mut self) -> Option<Vec<PhpResource>> { + let stdin = self.pipes.get(&0)?.clone(); + + // TODO(plugin): the `$input instanceof \Iterator` branch is not modeled. `input` is never a + // resource here, so the fread($input)/stream_set_blocking($input) paths do not apply and + // only the input buffer is written to stdin. + + let mut r: Vec<PhpResource> = Vec::new(); + let mut e: Vec<PhpResource> = Vec::new(); + let mut w: Vec<PhpResource> = vec![stdin.clone()]; + + // let's have a look if something changed in streams + if php::stream_select(&mut r, &mut w, &mut e, 0, Some(0)).is_none() { + return None; + } + + if !self.input_buffer.is_empty() { + let written = php::fwrite(&stdin, &self.input_buffer, None).unwrap_or(0) as usize; + self.input_buffer = self.input_buffer.get(written..).unwrap_or("").to_string(); + if !self.input_buffer.is_empty() { + return Some(vec![stdin]); + } + } + + // no input to read on resource, buffer is empty + if self.input_buffer.is_empty() && !php::php_truthy(&self.input) { + self.input = PhpMixed::Null; + php::fclose(&stdin); + self.pipes.shift_remove(&0); + } + + None } pub fn handle_error(&mut self, _type: i64, msg: String) { diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs index e10b3b3..92d584d 100644 --- a/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs +++ b/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs @@ -1,14 +1,14 @@ //! ref: composer/vendor/symfony/process/Pipes/PipesInterface.php use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{Descriptor, PhpResource}; pub const CHUNK_SIZE: i64 = 16384; /// PipesInterface manages descriptors and pipes for the use of proc_open. pub trait PipesInterface: std::fmt::Debug { /// Returns an array of descriptors for the use of proc_open. - fn get_descriptors(&mut self) -> Vec<PhpMixed>; + fn get_descriptors(&mut self) -> Vec<Descriptor>; /// Returns an array of filenames indexed by their related stream in case these pipes use temporary files. fn get_files(&self) -> IndexMap<i64, String>; @@ -25,7 +25,7 @@ pub trait PipesInterface: std::fmt::Debug { /// Closes file handles and pipes. fn close(&mut self); - /// Accessor for the `pipes` property populated by proc_open. - fn pipes(&self) -> &PhpMixed; - fn pipes_mut(&mut self) -> &mut PhpMixed; + /// Accessor for the `pipes` property populated by proc_open, keyed by fd index. + fn pipes(&self) -> &IndexMap<i64, PhpResource>; + fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource>; } diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs index 983ed07..795bb64 100644 --- a/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs +++ b/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs @@ -1,10 +1,10 @@ //! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{self as php, Descriptor, PhpMixed, PhpResource}; use crate::symfony::process::pipes::abstract_pipes::AbstractPipes; -use crate::symfony::process::pipes::pipes_interface::PipesInterface; +use crate::symfony::process::pipes::pipes_interface::{CHUNK_SIZE, PipesInterface}; use crate::symfony::process::process::Process; /// UnixPipes implementation uses unix pipes as handles. @@ -32,24 +32,24 @@ impl UnixPipes { } } -fn descriptor(items: &[&str]) -> PhpMixed { - PhpMixed::List( - items - .iter() - .map(|s| PhpMixed::String(s.to_string())) - .collect(), - ) +fn descriptor(items: &[&str]) -> Descriptor { + match items { + ["pipe", mode] => Descriptor::Pipe(mode.to_string()), + ["file", path, mode] => Descriptor::File(path.to_string(), mode.to_string()), + ["pty"] => Descriptor::Pty, + _ => panic!("unsupported descriptor spec: {:?}", items), + } } impl PipesInterface for UnixPipes { - fn get_descriptors(&mut self) -> Vec<PhpMixed> { + fn get_descriptors(&mut self) -> Vec<Descriptor> { if !self.have_read_support { - // TODO(phase-d): /dev/null is opened as a stream resource and placed directly into the - // proc_open descriptor spec, but the descriptor list is a Vec<PhpMixed> that cannot - // carry a PhpResource. - todo!( - "UnixPipes::get_descriptors: the /dev/null resource cannot be represented in a PhpMixed descriptor list" - ); + let nullstream = php::fopen("/dev/null", "c").expect("fopen('/dev/null') failed"); + return vec![ + descriptor(&["pipe", "r"]), + Descriptor::Resource(nullstream.clone()), + Descriptor::Resource(nullstream), + ]; } if self.tty_mode == Some(true) { @@ -79,8 +79,71 @@ impl PipesInterface for UnixPipes { IndexMap::new() } - fn read_and_write(&mut self, _blocking: bool, _close: bool) -> IndexMap<i64, String> { - todo!() + fn read_and_write(&mut self, blocking: bool, close: bool) -> IndexMap<i64, String> { + self.inner.unblock(); + let w = self.inner.write(); + + let mut read: IndexMap<i64, String> = IndexMap::new(); + // $r = $this->pipes; unset($r[0]); + let r: Vec<(i64, PhpResource)> = self + .inner + .pipes + .iter() + .filter(|(fd, _)| **fd != 0) + .map(|(fd, pipe)| (*fd, pipe.clone())) + .collect(); + + // TODO(plugin): set_error_handler/restore_error_handler around stream_select is not modeled. + let mut r_sel: Vec<PhpResource> = r.iter().map(|(_, p)| p.clone()).collect(); + let mut w_sel: Vec<PhpResource> = w.clone().unwrap_or_default(); + let mut e_sel: Vec<PhpResource> = Vec::new(); + + // let's have a look if something changed in streams + if (!r_sel.is_empty() || w.is_some()) + && php::stream_select( + &mut r_sel, + &mut w_sel, + &mut e_sel, + 0, + Some(if blocking { + (Process::TIMEOUT_PRECISION * 1e6) as i64 + } else { + 0 + }), + ) + .is_none() + { + // if a system call has been interrupted, forget about it, let's try again + // otherwise, an error occurred, let's reset pipes + if !self.inner.has_system_call_been_interrupted() { + self.inner.pipes = IndexMap::new(); + } + + return read; + } + + for (fd, pipe) in &r { + let mut data = String::new(); + loop { + let chunk = php::fread(pipe, CHUNK_SIZE).unwrap_or_default(); + let len = chunk.len() as i64; + data.push_str(&chunk); + if !(len > 0 && (close || len >= CHUNK_SIZE)) { + break; + } + } + + if !data.is_empty() { + read.insert(*fd, data); + } + + if close && php::feof(pipe) { + php::fclose(pipe); + self.inner.pipes.shift_remove(fd); + } + } + + read } fn have_read_support(&self) -> bool { @@ -88,18 +151,18 @@ impl PipesInterface for UnixPipes { } fn are_open(&self) -> bool { - shirabe_php_shim::php_truthy(&self.inner.pipes) + !self.inner.pipes.is_empty() } fn close(&mut self) { self.inner.close(); } - fn pipes(&self) -> &PhpMixed { + fn pipes(&self) -> &IndexMap<i64, PhpResource> { &self.inner.pipes } - fn pipes_mut(&mut self) -> &mut PhpMixed { + fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> { &mut self.inner.pipes } } diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs index 7602e7d..a31f184 100644 --- a/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs +++ b/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs @@ -1,7 +1,7 @@ //! ref: composer/vendor/symfony/process/Pipes/WindowsPipes.php use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{Descriptor, PhpMixed, PhpResource}; use crate::symfony::process::pipes::abstract_pipes::AbstractPipes; use crate::symfony::process::pipes::pipes_interface::PipesInterface; @@ -11,8 +11,8 @@ use crate::symfony::process::pipes::pipes_interface::PipesInterface; pub struct WindowsPipes { inner: AbstractPipes, files: IndexMap<i64, String>, - file_handles: IndexMap<i64, PhpMixed>, - lock_handles: IndexMap<i64, PhpMixed>, + file_handles: IndexMap<i64, PhpResource>, + lock_handles: IndexMap<i64, PhpResource>, read_bytes: IndexMap<i64, i64>, have_read_support: bool, } @@ -25,7 +25,7 @@ impl WindowsPipes { } impl PipesInterface for WindowsPipes { - fn get_descriptors(&mut self) -> Vec<PhpMixed> { + fn get_descriptors(&mut self) -> Vec<Descriptor> { let _ = ( &self.files, &self.file_handles, @@ -48,7 +48,7 @@ impl PipesInterface for WindowsPipes { } fn are_open(&self) -> bool { - shirabe_php_shim::php_truthy(&self.inner.pipes) && !self.file_handles.is_empty() + !self.inner.pipes.is_empty() && !self.file_handles.is_empty() } fn close(&mut self) { @@ -56,11 +56,11 @@ impl PipesInterface for WindowsPipes { todo!() } - fn pipes(&self) -> &PhpMixed { + fn pipes(&self) -> &IndexMap<i64, PhpResource> { &self.inner.pipes } - fn pipes_mut(&mut self) -> &mut PhpMixed { + fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> { &mut self.inner.pipes } } |
