support admin server restart --force

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-03 04:46:50 +00:00
parent 7658387a74
commit 5edd391e83
4 changed files with 62 additions and 6 deletions

View file

@ -34,3 +34,34 @@ pub fn available_parallelism() -> usize {
.expect("Unable to query for available parallelism.")
.get()
}
/// Return a possibly corrected std::env::current_exe() even if the path is
/// marked deleted.
///
/// # Safety
/// This function is declared unsafe because the original result was altered for
/// security purposes, and altering it back ignores those urposes and should be
/// understood by the user.
pub unsafe fn current_exe() -> Result<std::path::PathBuf> {
use std::path::PathBuf;
let exe = std::env::current_exe()?;
match exe.to_str() {
None => Ok(exe),
Some(str) => Ok(str
.strip_suffix(" (deleted)")
.map(PathBuf::from)
.unwrap_or(exe)),
}
}
/// Determine if the server's executable was removed or replaced. This is a
/// specific check; useful for successful restarts. May not be available or
/// accurate on all platforms; defaults to false.
#[must_use]
pub fn current_exe_deleted() -> bool {
std::env::current_exe().map_or(false, |exe| {
exe.to_str()
.map_or(false, |exe| exe.ends_with(" (deleted)"))
})
}