diff options
Diffstat (limited to 'crates/shirabe-symfony-process/src/pipes')
4 files changed, 328 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-process/src/pipes/abstract_pipes.rs b/crates/shirabe-symfony-process/src/pipes/abstract_pipes.rs new file mode 100644 index 00000000..8741926c --- /dev/null +++ b/crates/shirabe-symfony-process/src/pipes/abstract_pipes.rs @@ -0,0 +1,104 @@ +//! ref: composer/vendor/symfony/process/Pipes/AbstractPipes.php + +use indexmap::IndexMap; +use shirabe_php_shim::{PhpMixed, PhpResource}; + +#[derive(Debug)] +pub struct AbstractPipes { + pub pipes: IndexMap<i64, PhpResource>, + + input_buffer: String, + input: PhpMixed, + blocked: bool, + last_error: Option<String>, +} + +impl AbstractPipes { + pub fn new(input: PhpMixed) -> Self { + let input_buffer; + let stored_input; + // TODO(plugin): `$input instanceof \Iterator` is not modeled. The PHP `is_resource($input)` + // branch never applies: a PhpMixed is never a resource, so input is never stored as-is here. + if let PhpMixed::String(s) = &input { + input_buffer = s.clone(); + stored_input = PhpMixed::Null; + } else { + input_buffer = input.as_string().map(|s| s.to_string()).unwrap_or_default(); + stored_input = PhpMixed::Null; + } + + Self { + pipes: IndexMap::new(), + input_buffer, + input: stored_input, + blocked: true, + last_error: None, + } + } + + pub fn close(&mut self) { + for (_, pipe) in &self.pipes { + shirabe_php_shim::fclose(pipe); + } + self.pipes = IndexMap::new(); + } + + /// Returns true if a system call has been interrupted. + pub(crate) fn has_system_call_been_interrupted(&mut self) -> bool { + let last_error = self.last_error.take(); + + // stream_select returns false when the `select` system call is interrupted by an incoming signal + last_error + .map(|e| e.to_lowercase().contains("interrupted system call")) + .unwrap_or(false) + } + + /// Unblocks streams. + pub(crate) fn unblock(&mut self) { + if !self.blocked { + return; + } + + for (_, pipe) in &self.pipes { + shirabe_php_shim::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<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 + shirabe_php_shim::stream_select(&mut r, &mut w, &mut e, 0, Some(0))?; + + if !self.input_buffer.is_empty() { + let written = + shirabe_php_shim::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() && !shirabe_php_shim::php_truthy(&self.input) { + self.input = PhpMixed::Null; + shirabe_php_shim::fclose(&stdin); + self.pipes.shift_remove(&0); + } + + None + } +} diff --git a/crates/shirabe-symfony-process/src/pipes/pipes_interface.rs b/crates/shirabe-symfony-process/src/pipes/pipes_interface.rs new file mode 100644 index 00000000..46609bb8 --- /dev/null +++ b/crates/shirabe-symfony-process/src/pipes/pipes_interface.rs @@ -0,0 +1,28 @@ +//! ref: composer/vendor/symfony/process/Pipes/PipesInterface.php + +use indexmap::IndexMap; +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<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>; + + /// Reads data in file handles and pipes. + fn read_and_write(&mut self, blocking: bool, close: bool) -> IndexMap<i64, String>; + + /// Returns if the current state has open file handles or pipes. + fn are_open(&self) -> bool; + + /// Closes file handles and pipes. + fn close(&mut self); + + /// 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-symfony-process/src/pipes/unix_pipes.rs b/crates/shirabe-symfony-process/src/pipes/unix_pipes.rs new file mode 100644 index 00000000..bfc93d44 --- /dev/null +++ b/crates/shirabe-symfony-process/src/pipes/unix_pipes.rs @@ -0,0 +1,136 @@ +//! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php + +use crate::pipes::abstract_pipes::AbstractPipes; +use crate::pipes::pipes_interface::{CHUNK_SIZE, PipesInterface}; +use crate::process::Process; +use indexmap::IndexMap; +use shirabe_php_shim::{Descriptor, PhpMixed, PhpResource}; + +/// UnixPipes implementation uses unix pipes as handles. +#[derive(Debug)] +pub struct UnixPipes { + inner: AbstractPipes, + tty_mode: Option<bool>, +} + +impl UnixPipes { + pub fn new(tty_mode: Option<bool>, input: PhpMixed) -> Self { + Self { + inner: AbstractPipes::new(input), + tty_mode, + } + } +} + +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()), + _ => panic!("unsupported descriptor spec: {:?}", items), + } +} + +impl PipesInterface for UnixPipes { + fn get_descriptors(&mut self) -> Vec<Descriptor> { + if self.tty_mode == Some(true) { + return vec![ + descriptor(&["file", "/dev/tty", "r"]), + descriptor(&["file", "/dev/tty", "w"]), + descriptor(&["file", "/dev/tty", "w"]), + ]; + } + + vec![ + descriptor(&["pipe", "r"]), + descriptor(&["pipe", "w"]), + descriptor(&["pipe", "w"]), + ] + } + + fn get_files(&self) -> IndexMap<i64, String> { + IndexMap::new() + } + + 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()) + && shirabe_php_shim::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 = shirabe_php_shim::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 && shirabe_php_shim::feof(pipe) { + shirabe_php_shim::fclose(pipe); + self.inner.pipes.shift_remove(fd); + } + } + + read + } + + fn are_open(&self) -> bool { + !self.inner.pipes.is_empty() + } + + fn close(&mut self) { + self.inner.close(); + } + + fn pipes(&self) -> &IndexMap<i64, PhpResource> { + &self.inner.pipes + } + + fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> { + &mut self.inner.pipes + } +} diff --git a/crates/shirabe-symfony-process/src/pipes/windows_pipes.rs b/crates/shirabe-symfony-process/src/pipes/windows_pipes.rs new file mode 100644 index 00000000..87bf5126 --- /dev/null +++ b/crates/shirabe-symfony-process/src/pipes/windows_pipes.rs @@ -0,0 +1,60 @@ +//! ref: composer/vendor/symfony/process/Pipes/WindowsPipes.php + +use crate::pipes::abstract_pipes::AbstractPipes; +use crate::pipes::pipes_interface::PipesInterface; +use indexmap::IndexMap; +use shirabe_php_shim::{Descriptor, PhpMixed, PhpResource}; + +/// WindowsPipes implementation uses temporary files as handles. +#[derive(Debug)] +pub struct WindowsPipes { + inner: AbstractPipes, + files: IndexMap<i64, String>, + file_handles: IndexMap<i64, PhpResource>, + lock_handles: IndexMap<i64, PhpResource>, + read_bytes: IndexMap<i64, i64>, +} + +impl WindowsPipes { + pub fn new(_input: PhpMixed) -> Self { + // Windows-only path: never constructed on non-Windows targets. + todo!() + } +} + +impl PipesInterface for WindowsPipes { + fn get_descriptors(&mut self) -> Vec<Descriptor> { + let _ = ( + &self.files, + &self.file_handles, + &self.lock_handles, + &self.read_bytes, + ); + todo!() + } + + fn get_files(&self) -> IndexMap<i64, String> { + self.files.clone() + } + + fn read_and_write(&mut self, _blocking: bool, _close: bool) -> IndexMap<i64, String> { + todo!() + } + + fn are_open(&self) -> bool { + !self.inner.pipes.is_empty() && !self.file_handles.is_empty() + } + + fn close(&mut self) { + self.inner.close(); + todo!() + } + + fn pipes(&self) -> &IndexMap<i64, PhpResource> { + &self.inner.pipes + } + + fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> { + &mut self.inner.pipes + } +} |
