| |
| |
| |
|
|
| use ctor::ctor; |
| use std::{ |
| io::{Error, ErrorKind, Result}, |
| path::{Path, PathBuf}, |
| }; |
|
|
| |
| #[ctor] |
| #[used] |
| pub(super) static STARTING_BINARY: StartingBinary = StartingBinary::new(); |
|
|
| |
| pub(super) struct StartingBinary(std::io::Result<PathBuf>); |
|
|
| impl StartingBinary { |
| |
| fn new() -> Self { |
| |
| let dangerous_path = match std::env::current_exe() { |
| Ok(dangerous_path) => dangerous_path, |
| error @ Err(_) => return Self(error), |
| }; |
|
|
| |
| if let Some(symlink) = Self::has_symlink(&dangerous_path) { |
| return Self(Err(Error::new( |
| ErrorKind::InvalidData, |
| format!("StartingBinary found current_exe() that contains a symlink on a non-allowed platform: {}", symlink.display()), |
| ))); |
| } |
|
|
| |
| Self(dangerous_path.canonicalize()) |
| } |
|
|
| |
| |
| |
| pub(super) fn cloned(&self) -> Result<PathBuf> { |
| |
| #[allow(clippy::useless_asref)] |
| self |
| .0 |
| .as_ref() |
| .map(Clone::clone) |
| .map_err(|e| Error::new(e.kind(), e.to_string())) |
| } |
|
|
| |
| #[cfg(any( |
| not(target_os = "macos"), |
| feature = "process-relaunch-dangerous-allow-symlink-macos" |
| ))] |
| fn has_symlink(_: &Path) -> Option<&Path> { |
| None |
| } |
|
|
| |
| #[cfg(all( |
| target_os = "macos", |
| not(feature = "process-relaunch-dangerous-allow-symlink-macos") |
| ))] |
| fn has_symlink(path: &Path) -> Option<&Path> { |
| path.ancestors().find(|ancestor| { |
| matches!( |
| ancestor |
| .symlink_metadata() |
| .as_ref() |
| .map(std::fs::Metadata::file_type) |
| .as_ref() |
| .map(std::fs::FileType::is_symlink), |
| Ok(true) |
| ) |
| }) |
| } |
| } |
|
|