Skip to content

Commit

Permalink
Follow symlinks for exe (denisidoro#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Mar 5, 2020
1 parent b3ab91a commit fe26b00
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -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<P>(filename: P) -> io::Result<Lines<BufReader<File>>>
where
Expand All @@ -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()
}

0 comments on commit fe26b00

Please sign in to comment.