Skip to content

Commit 1d25f27

Browse files
committed
rust: fix PATH manipulation methods
1 parent 1225542 commit 1d25f27

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

rust/bear/src/intercept/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,9 @@ impl InterceptEnvironment {
381381
let path_original = std::env::var("PATH").unwrap_or_else(|_| String::new());
382382
let path_updated = InterceptEnvironment::insert_to_path(
383383
&path_original,
384-
Self::path_to_string(bin_dir.path()),
385-
);
384+
bin_dir.path().to_path_buf(),
385+
)
386+
.unwrap_or_else(|_| path_original.clone());
386387
vec![
387388
("PATH".to_string(), path_updated),
388389
(KEY_DESTINATION.to_string(), address.clone()),
@@ -391,10 +392,9 @@ impl InterceptEnvironment {
391392
InterceptEnvironment::Preload { path, address, .. } => {
392393
let path_original =
393394
std::env::var(KEY_PRELOAD_PATH).unwrap_or_else(|_| String::new());
394-
let path_updated = InterceptEnvironment::insert_to_path(
395-
&path_original,
396-
Self::path_to_string(path),
397-
);
395+
let path_updated =
396+
InterceptEnvironment::insert_to_path(&path_original, path.clone())
397+
.unwrap_or_else(|_| path_original.clone());
398398
vec![
399399
(KEY_PRELOAD_PATH.to_string(), path_updated),
400400
(KEY_DESTINATION.to_string(), address.clone()),
@@ -403,17 +403,17 @@ impl InterceptEnvironment {
403403
}
404404
}
405405

406-
/// Manipulate a `PATH` like environment value by inserting the `first` path into
406+
/// Manipulate a `PATH`-like environment value by inserting the `first` path into
407407
/// the original value. It removes the `first` path if it already exists in the
408408
/// original value. And it inserts the `first` path at the beginning of the value.
409-
fn insert_to_path(original: &str, first: String) -> String {
410-
let mut paths: Vec<_> = original.split(':').filter(|it| it != &first).collect();
411-
paths.insert(0, first.as_str());
412-
paths.join(":")
413-
}
414-
415-
fn path_to_string(path: &Path) -> String {
416-
path.to_str().unwrap_or("").to_string()
409+
fn insert_to_path(original: &str, first: PathBuf) -> anyhow::Result<String> {
410+
let mut paths: Vec<_> = std::env::split_paths(original)
411+
.filter(|path| path != &first)
412+
.collect();
413+
paths.insert(0, first);
414+
std::env::join_paths(paths)
415+
.map(|os_string| os_string.into_string().unwrap_or_default())
416+
.map_err(|e| anyhow::anyhow!("Failed to join paths: {}", e))
417417
}
418418
}
419419

0 commit comments

Comments
 (0)