From fe26b00953f845a234763dbf133f9b4b0c8e0094 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Thu, 5 Mar 2020 16:29:49 -0300 Subject: [PATCH] Follow symlinks for exe (#209) --- src/filesystem.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index 58e9da6..f8bb872 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,6 +1,7 @@ +use std::fs; use std::fs::File; use std::io::{self, BufRead, BufReader, Lines}; -use std::path::Path; +use std::path::{Path, PathBuf}; pub fn read_lines

(filename: P) -> io::Result>> where @@ -10,24 +11,29 @@ where Ok(io::BufReader::new(file).lines()) } +fn follow_symlink(pathbuf: PathBuf) -> PathBuf { + let other = fs::read_link(pathbuf.clone()); + match other { + Ok(o) => follow_symlink(o), + Err(_) => pathbuf, + } +} + +fn exe_pathbuf() -> PathBuf { + let pathbuf = std::env::current_exe().unwrap().to_path_buf(); + follow_symlink(pathbuf) +} + pub fn exe_string() -> String { - String::from( - std::env::current_exe() - .unwrap() - .as_os_str() - .to_str() - .unwrap(), - ) + exe_pathbuf().as_os_str().to_str().unwrap().to_string() } pub fn exe_path_string() -> String { - String::from( - std::env::current_exe() - .unwrap() - .parent() - .unwrap() - .as_os_str() - .to_str() - .unwrap(), - ) + exe_pathbuf() + .parent() + .unwrap() + .as_os_str() + .to_str() + .unwrap() + .to_string() }