aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs
blob: a702631896c926f76966df181ec43fce0d849bcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php

use crate::symfony::process::pipes::abstract_pipes::AbstractPipes;
use crate::symfony::process::pipes::pipes_interface::{CHUNK_SIZE, PipesInterface};
use crate::symfony::process::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
    }
}