summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/rust/mozrunner/src/path.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /testing/mozbase/rust/mozrunner/src/path.rs
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/mozbase/rust/mozrunner/src/path.rs')
-rw-r--r--testing/mozbase/rust/mozrunner/src/path.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/testing/mozbase/rust/mozrunner/src/path.rs b/testing/mozbase/rust/mozrunner/src/path.rs
new file mode 100644
index 0000000000..0bf1eda9ed
--- /dev/null
+++ b/testing/mozbase/rust/mozrunner/src/path.rs
@@ -0,0 +1,48 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+//! Provides utilities for searching the system path.
+
+use std::env;
+use std::path::{Path, PathBuf};
+
+#[cfg(unix)]
+fn is_executable(path: &Path) -> bool {
+ use std::fs;
+ use std::os::unix::fs::PermissionsExt;
+
+ // Permissions are a set of four 4-bit bitflags, represented by a single octal
+ // digit. The lowest bit of each of the last three values represents the
+ // executable permission for all, group and user, repsectively. We assume the
+ // file is executable if any of these are set.
+ match fs::metadata(path).ok() {
+ Some(meta) => meta.permissions().mode() & 0o111 != 0,
+ None => false,
+ }
+}
+
+#[cfg(not(unix))]
+fn is_executable(_: &Path) -> bool {
+ true
+}
+
+/// Determines if the path is an executable binary. That is, if it exists, is
+/// a file, and is executable where applicable.
+pub fn is_binary(path: &Path) -> bool {
+ path.exists() && path.is_file() && is_executable(path)
+}
+
+/// Searches the system path (`PATH`) for an executable binary and returns the
+/// first match, or `None` if not found.
+pub fn find_binary(binary_name: &str) -> Option<PathBuf> {
+ env::var_os("PATH").and_then(|path_env| {
+ for mut path in env::split_paths(&path_env) {
+ path.push(binary_name);
+ if is_binary(&path) {
+ return Some(path);
+ }
+ }
+ None
+ })
+}