From 03e659f96d8f3e0d914a9c36b44f34d79a40da6b Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 25 Aug 2023 22:02:21 +0200 Subject: [PATCH] Bring back "(deleted)" hack for status fish-path This is untested mostly because it is supremely awkward to test. Fixes #9925 --- fish-rust/src/common.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index 299bbd0b4..eb32e3925 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -23,7 +23,7 @@ use libc::{EINTR, EIO, O_WRONLY, SIGTTOU, SIG_IGN, STDERR_FILENO, STDIN_FILENO, use num_traits::ToPrimitive; use once_cell::sync::Lazy; use std::env; -use std::ffi::{CStr, CString, OsString}; +use std::ffi::{CStr, CString, OsStr, OsString}; use std::mem; use std::ops::{Deref, DerefMut}; use std::os::unix::prelude::*; @@ -1770,7 +1770,28 @@ pub fn valid_var_name(s: &wstr) -> bool { /// Get the absolute path to the fish executable itself pub fn get_executable_path(argv0: &str) -> PathBuf { - std::env::current_exe().unwrap_or_else(|_| PathBuf::from_str(argv0).unwrap()) + if let Ok(path) = std::env::current_exe() { + if path.exists() { + return path; + } + + // When /proc/self/exe points to a file that was deleted (or overwritten on update!) + // then linux adds a " (deleted)" suffix. + // If that's not a valid path, let's remove that awkward suffix. + let pathstr = path.to_str().unwrap_or(""); + if !pathstr.ends_with(" (deleted)") { + return path; + } + + if let (Some(filename), Some(parent)) = (path.file_name(), path.parent()) { + if let Some(filename) = filename.to_str() { + let corrected_filename = OsStr::new(filename.strip_suffix(" (deleted)").unwrap()); + return parent.join(corrected_filename); + } + } + return path; + } + PathBuf::from_str(argv0).unwrap() } /// A RAII cleanup object. Unlike in C++ where there is no borrow checker, we can't just provide a