use std::path::PathBuf;
extern crate path_absolutize;
use path_absolutize::Absolutize;
#[no_mangle]
fn step_by_step_canonicalize(path: &mut PathBuf) {
let mut components = path.components().collect::<Vec<_>>();
let mut head = path.clone();
let mut tail = PathBuf::new();
let mut result = head.canonicalize();
let mut component;
let mut cx;
while result.is_err(){
head = match head.parent(){
Some(parent) => parent.to_path_buf(),
None => return,
};
result = head.canonicalize();
component = components.pop();
match component{
Some(component) => {
cx = PathBuf::from(component.as_os_str());
tail = cx.join(tail);
}
None => return,
}
}
let result = result.unwrap().join(tail);
let result = result.absolutize().unwrap().to_path_buf();
*path = result;
}
#[inline]
pub fn realpath_og(path: &PathBuf) -> Result<PathBuf, std::io::Error> {
let mut path = path.absolutize()?.to_path_buf();
step_by_step_canonicalize(&mut path);
Ok(path)
}
pub fn realpath(path : &PathBuf) -> Result<PathBuf, std::io::Error> {
#[cfg(target_os = "windows")]
{
realpath_win(path, true)
}
#[cfg(not(target_os = "windows"))]
{
realpath_og(path)
}
}
#[cfg(target_os = "windows")]
#[inline]
pub fn realpath_win(path : &PathBuf, dl : bool) -> Result<PathBuf, std::io::Error> {
let mut path = path.absolutize()?.to_path_buf();
step_by_step_canonicalize(&mut path);
println!("{}" , path.display());
if dl {
let conv = path.to_string_lossy().to_string();
match conv.strip_prefix(include_str!("../unc.txt")){
Some(res) => return Ok(PathBuf::from(res)),
None => (),
};
match conv.strip_prefix(include_str!("../straight.txt")){
Some(res) => return Ok(PathBuf::from(res)),
None => (),
};
}
Ok(path)
}