aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/platform.rs
blob: 96e564f42b794ec1a966ec4dff83546841be6e5a (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! ref: composer/src/Composer/Util/Platform.php

use std::sync::Mutex;

use anyhow::Result;
use shirabe_external_packages::composer::pcre::preg::Preg;
use shirabe_php_shim::{
    PhpMixed, RuntimeException, defined, env_contains_key, env_get, env_set, env_unset,
    file_exists, file_get_contents, fopen, fstat, function_exists, getcwd, getenv, in_array,
    ini_get, is_array, is_readable, mb_strlen, posix_geteuid, posix_getpwuid, posix_getuid,
    posix_isatty, putenv, realpath, server_argv, server_contains_key, server_get, server_set,
    server_unset, stream_isatty, stripos, strlen, strtoupper, substr, usleep,
};

use crate::util::process_executor::ProcessExecutor;
use crate::util::silencer::Silencer;

/// Platform helper for uniform platform-specific tests.
pub struct Platform;

static IS_VIRTUAL_BOX_GUEST: Mutex<Option<bool>> = Mutex::new(None);
static IS_WINDOWS_SUBSYSTEM_FOR_LINUX: Mutex<Option<bool>> = Mutex::new(None);
static IS_DOCKER: Mutex<Option<bool>> = Mutex::new(None);

impl Platform {
    /// getcwd() equivalent which always returns a string
    ///
    /// @throws \RuntimeException
    pub fn get_cwd(allow_empty: bool) -> Result<String> {
        let mut cwd = getcwd();

        // fallback to realpath('') just in case this works but odds are it would break as well if we are in a case where getcwd fails
        if cwd.is_none() {
            cwd = realpath("");
        }

        // crappy state, assume '' and hopefully relative paths allow things to continue
        if cwd.is_none() {
            if allow_empty {
                return Ok(String::new());
            }

            return Err(RuntimeException {
                message: "Could not determine the current working directory".to_string(),
                code: 0,
            }
            .into());
        }

        Ok(cwd.unwrap())
    }

    /// Infallible realpath version that falls back on the given $path if realpath is not working
    pub fn realpath(path: &str) -> String {
        let real_path = realpath(path);
        if real_path.is_none() {
            return path.to_string();
        }

        real_path.unwrap()
    }

    /// getenv() equivalent but reads from the runtime global variables first
    ///
    /// @param non-empty-string $name
    ///
    /// @return string|false
    pub fn get_env(name: &str) -> Option<String> {
        if server_contains_key(name) {
            return Some(server_get(name).unwrap_or_default());
        }
        if env_contains_key(name) {
            return Some(env_get(name).unwrap_or_default());
        }

        getenv(name)
    }

    /// putenv() equivalent but updates the runtime global variables too
    pub fn put_env(name: &str, value: &str) {
        putenv(&format!("{}={}", name, value));
        server_set(name, value.to_string());
        env_set(name, value.to_string());
    }

    /// putenv('X') equivalent but updates the runtime global variables too
    pub fn clear_env(name: &str) {
        putenv(name);
        server_unset(name);
        env_unset(name);
    }

    /// Parses tildes and environment variables in paths.
    pub fn expand_path(path: &str) -> String {
        if Preg::is_match(r"#^~[\\/]#", path) {
            return format!(
                "{}{}",
                Self::get_user_directory().unwrap(),
                substr(path, 1, None)
            );
        }

        Preg::replace_callback(
            r"#^(\$|(?P<percent>%))(?P<var>\w++)(?(percent)%)(?P<path>.*)#",
            |matches| -> String {
                // Treat HOME as an alias for USERPROFILE on Windows for legacy reasons
                if Platform::is_windows()
                    && matches.get("var").map(|s| s.as_str()).unwrap_or("") == "HOME"
                {
                    if Platform::get_env("HOME").is_some() {
                        return format!(
                            "{}{}",
                            Platform::get_env("HOME").unwrap_or_default(),
                            matches.get("path").map(|s| s.as_str()).unwrap_or(""),
                        );
                    }

                    return format!(
                        "{}{}",
                        Platform::get_env("USERPROFILE").unwrap_or_default(),
                        matches.get("path").map(|s| s.as_str()).unwrap_or(""),
                    );
                }

                format!(
                    "{}{}",
                    Platform::get_env(matches.get("var").map(|s| s.as_str()).unwrap_or(""))
                        .unwrap_or_default(),
                    matches.get("path").map(|s| s.as_str()).unwrap_or(""),
                )
            },
            path,
        )
    }

    /// @throws \RuntimeException If the user home could not reliably be determined
    /// @return string            The formal user home as detected from environment parameters
    pub fn get_user_directory() -> Result<String> {
        if let Some(home) = Self::get_env("HOME") {
            return Ok(home);
        }

        if Self::is_windows() {
            if let Some(home) = Self::get_env("USERPROFILE") {
                return Ok(home);
            }
        }

        if function_exists("posix_getuid") && function_exists("posix_getpwuid") {
            let info = posix_getpwuid(posix_getuid());

            if is_array(&info) {
                if let Some(arr) = info.as_array() {
                    if let Some(dir) = arr.get("dir") {
                        if let Some(s) = dir.as_string() {
                            return Ok(s.to_string());
                        }
                    }
                }
            }
        }

        Err(RuntimeException {
            message: "Could not determine user directory".to_string(),
            code: 0,
        }
        .into())
    }

    /// @return bool Whether the host machine is running on the Windows Subsystem for Linux (WSL)
    pub fn is_windows_subsystem_for_linux() -> bool {
        let mut cached = IS_WINDOWS_SUBSYSTEM_FOR_LINUX.lock().unwrap();
        if cached.is_none() {
            *cached = Some(false);

            // while WSL will be hosted within windows, WSL itself cannot be windows based itself.
            if Self::is_windows() {
                *cached = Some(false);
                return false;
            }

            // TODO(phase-b): Silencer::call returns Result; PHP returns the value or false on error
            let file_contents = Silencer::call(|| Ok(file_get_contents("/proc/version")))
                .ok()
                .flatten()
                .unwrap_or_default();
            if !(ini_get("open_basedir")
                .map(|s| !s.is_empty())
                .unwrap_or(false))
                && is_readable("/proc/version")
                && stripos(&file_contents, "microsoft").is_some()
                && !Self::is_docker()
            // Docker and Podman running inside WSL should not be seen as WSL
            {
                *cached = Some(true);
                return true;
            }
        }

        cached.unwrap()
    }

    /// @return bool Whether the host machine is running a Windows OS
    pub fn is_windows() -> bool {
        defined("PHP_WINDOWS_VERSION_BUILD")
    }

    pub fn is_docker() -> bool {
        let mut cached = IS_DOCKER.lock().unwrap();
        if let Some(v) = *cached {
            return v;
        }

        // cannot check so assume no
        if ini_get("open_basedir")
            .map(|s| !s.is_empty())
            .unwrap_or(false)
        {
            *cached = Some(false);
            return false;
        }

        // .dockerenv and .containerenv are present in some cases but not reliably
        if file_exists("/.dockerenv")
            || file_exists("/run/.containerenv")
            || file_exists("/var/run/.containerenv")
        {
            *cached = Some(true);
            return true;
        }

        // see https://www.baeldung.com/linux/is-process-running-inside-container
        let cgroups = vec![
            "/proc/self/mountinfo", // cgroup v2
            "/proc/1/cgroup",       // cgroup v1
        ];
        for cgroup in cgroups {
            if !is_readable(cgroup) {
                continue;
            }
            // suppress errors as some environments have these files as readable but system restrictions prevent the read from succeeding
            // see https://github.com/composer/composer/issues/12095
            let data = match Silencer::call(|| Ok(file_get_contents(cgroup))) {
                Ok(d) => d,
                Err(_) => break,
            };
            let data = match data {
                Some(d) => d,
                None => continue,
            };
            // detect default mount points created by Docker/containerd
            if shirabe_php_shim::str_contains(&data, "/var/lib/docker/")
                || shirabe_php_shim::str_contains(&data, "/io.containerd.snapshotter")
            {
                *cached = Some(true);
                return true;
            }
        }

        *cached = Some(false);
        false
    }

    /// @return int    return a guaranteed binary length of the string, regardless of silly mbstring configs
    pub fn strlen(str: &str) -> i64 {
        // TODO(phase-b): function-local static; collapse to a Mutex<Option<bool>> in Phase B
        static USE_MB_STRING: Mutex<Option<bool>> = Mutex::new(None);
        let mut use_mb_string = USE_MB_STRING.lock().unwrap();
        if use_mb_string.is_none() {
            *use_mb_string = Some(
                function_exists("mb_strlen")
                    && ini_get("mbstring.func_overload")
                        .map(|s| !s.is_empty())
                        .unwrap_or(false),
            );
        }

        if use_mb_string.unwrap() {
            return mb_strlen(str, "8bit");
        }

        strlen(str)
    }

    /// @param  ?resource $fd Open file descriptor or null to default to STDOUT
    pub fn is_tty(fd: Option<PhpMixed>) -> bool {
        let fd = match fd {
            Some(f) => f,
            None => {
                if defined("STDOUT") {
                    // TODO(phase-b): map STDOUT to the runtime stdout resource
                    todo!("STDOUT constant")
                } else {
                    let fd = fopen("php://stdout", "w");
                    if matches!(fd, PhpMixed::Bool(false)) {
                        return false;
                    }
                    fd
                }
            }
        };

        // detect msysgit/mingw and assume this is a tty because detection
        // does not work correctly, see https://github.com/composer/composer/issues/9690
        if in_array(
            PhpMixed::String(strtoupper(&Self::get_env("MSYSTEM").unwrap_or_default())),
            &PhpMixed::List(vec![
                Box::new(PhpMixed::String("MINGW32".to_string())),
                Box::new(PhpMixed::String("MINGW64".to_string())),
            ]),
            true,
        ) {
            return true;
        }

        // modern cross-platform function, includes the fstat
        // fallback so if it is present we trust it
        if function_exists("stream_isatty") {
            return stream_isatty(fd);
        }

        // only trusting this if it is positive, otherwise prefer fstat fallback
        if function_exists("posix_isatty") && posix_isatty(fd.clone()) {
            return true;
        }

        // TODO(phase-b): Silencer::call wraps the fstat call (`@fstat($fd)`)
        let stat = Silencer::call(|| Ok(fstat(fd)));
        let stat = match stat {
            Ok(s) => s,
            Err(_) => return false,
        };
        if matches!(stat, PhpMixed::Bool(false)) {
            return false;
        }

        // Check if formatted mode is S_IFCHR
        if let Some(arr) = stat.as_array() {
            if let Some(mode) = arr.get("mode").and_then(|v| v.as_int()) {
                return 0o020000 == (mode & 0o170000);
            }
        }

        false
    }

    /// @return bool Whether the current command is for bash completion
    pub fn is_input_completion_process() -> bool {
        // PHP: $_SERVER['argv'][1] ?? null
        let argv = server_argv();
        argv.get(1).map(|s| s.as_str()) == Some("_complete")
    }

    pub fn workaround_filesystem_issues() {
        if Self::is_virtual_box_guest() {
            usleep(200_000);
        }
    }

    /// Attempts detection of VirtualBox guest VMs
    ///
    /// This works based on the process' user being "vagrant", the COMPOSER_RUNTIME_ENV env var being set to "virtualbox", or lsmod showing the virtualbox guest additions are loaded
    fn is_virtual_box_guest() -> bool {
        let mut cached = IS_VIRTUAL_BOX_GUEST.lock().unwrap();
        if cached.is_none() {
            *cached = Some(false);
            if Self::is_windows() {
                return cached.unwrap();
            }

            if function_exists("posix_getpwuid") && function_exists("posix_geteuid") {
                let process_user = posix_getpwuid(posix_geteuid());
                if is_array(&process_user) {
                    if let Some(arr) = process_user.as_array() {
                        if arr
                            .get("name")
                            .and_then(|v| v.as_string())
                            .map(|s| s == "vagrant")
                            .unwrap_or(false)
                        {
                            *cached = Some(true);
                            return true;
                        }
                    }
                }
            }

            if Self::get_env("COMPOSER_RUNTIME_ENV").as_deref() == Some("virtualbox") {
                *cached = Some(true);
                return true;
            }

            if defined("PHP_OS_FAMILY")
                // TODO(phase-b): PHP_OS_FAMILY constant comparison
                && true
            {
                let process = ProcessExecutor::new();
                // TODO(phase-b): inner Result for catch(\Exception); use anyhow::Result<Result<_, _>>
                let mut output = String::new();
                let result: Result<()> = (|| {
                    if process.execute(&["lsmod"], &mut output)? == 0
                        && shirabe_php_shim::str_contains(&output, "vboxguest")
                    {
                        *cached = Some(true);
                        return Ok(());
                    }
                    Ok(())
                })();
                if result.is_ok() && cached.unwrap_or(false) {
                    return true;
                }
                // noop on error
            }
        }

        cached.unwrap_or(false)
    }

    /// @return 'NUL'|'/dev/null'
    pub fn get_dev_null() -> String {
        if Self::is_windows() {
            return "NUL".to_string();
        }

        "/dev/null".to_string()
    }
}