Rollup merge of #56525 - udoprog:linux-current-exe, r=alexcrichton

Avoid extra copy and syscall in std::env::current_exe
This commit is contained in:
Pietro Albini 2018-12-06 07:48:58 +01:00 committed by GitHub
commit bd8dd11d4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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,
}
}