aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs
blob: 795bb64419931c372cddc945ae3230ac34d5e5cc (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php

use indexmap::IndexMap;
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::{CHUNK_SIZE, PipesInterface};
use crate::symfony::process::process::Process;

/// UnixPipes implementation uses unix pipes as handles.
#[derive(Debug)]
pub struct UnixPipes {
    inner: AbstractPipes,
    tty_mode: Option<bool>,
    pty_mode: bool,
    have_read_support: bool,
}

impl UnixPipes {
    pub fn new(
        tty_mode: Option<bool>,
        pty_mode: bool,
        input: PhpMixed,
        have_read_support: bool,
    ) -> Self {
        Self {
            inner: AbstractPipes::new(input),
            tty_mode,
            pty_mode,
            have_read_support,
        }
    }
}

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<Descriptor> {
        if !self.have_read_support {
            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) {
            return vec![
                descriptor(&["file", "/dev/tty", "r"]),
                descriptor(&["file", "/dev/tty", "w"]),
                descriptor(&["file", "/dev/tty", "w"]),
            ];
        }

        if self.pty_mode && Process::is_pty_supported() {
            return vec![
                descriptor(&["pty"]),
                descriptor(&["pty"]),
                descriptor(&["pty"]),
            ];
        }

        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())
            && 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 {
        self.have_read_support
    }

    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
    }
}