diff options
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs')
| -rw-r--r-- | crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs | 105 |
1 files changed, 84 insertions, 21 deletions
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 } } |
