Avoid extra copy and syscall in std::env::current_exe

This commit is contained in:
John-John Tedro 2018-12-05 02:48:18 +01:00
parent af7554d3a2
commit 3512fb0467

View file

@ -283,11 +283,14 @@ pub fn current_exe() -> io::Result<PathBuf> {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
pub fn current_exe() -> io::Result<PathBuf> {
let selfexe = PathBuf::from("/proc/self/exe");
if selfexe.exists() {
::fs::read_link(selfexe)
} else {
Err(io::Error::new(io::ErrorKind::Other, "no /proc/self/exe available. Is /proc mounted?"))
match ::fs::read_link("/proc/self/exe") {
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
Err(io::Error::new(
io::ErrorKind::Other,
"no /proc/self/exe available. Is /proc mounted?"
))
},
other => other,
}
}