2017-10-18 20:04:34 +02:00
|
|
|
use std::env::current_dir;
|
2017-06-05 16:25:13 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-10-07 09:40:44 +02:00
|
|
|
use std::io;
|
2017-06-05 16:25:13 +02:00
|
|
|
|
2017-10-18 20:04:34 +02:00
|
|
|
pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
|
|
|
|
if path.is_absolute() {
|
|
|
|
Ok(path.to_path_buf())
|
2017-06-05 16:25:13 +02:00
|
|
|
} else {
|
2017-10-18 20:04:34 +02:00
|
|
|
let path = path.strip_prefix(".").unwrap_or(path);
|
|
|
|
current_dir().map(|path_buf| path_buf.join(path))
|
2017-06-05 16:25:13 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
|
2017-10-18 20:04:34 +02:00
|
|
|
let path_buf = path_absolute_form(path)?;
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2017-10-12 08:01:51 +02:00
|
|
|
let path_buf = Path::new(path_buf.as_path().to_string_lossy().trim_left_matches(
|
|
|
|
r"\\?\",
|
|
|
|
)).to_path_buf();
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
Ok(path_buf)
|
|
|
|
}
|
2017-10-18 20:04:34 +02:00
|
|
|
|
|
|
|
// Path::is_dir() is not guarandteed to be intuitively correct for "." and ".."
|
|
|
|
// See: https://github.com/rust-lang/rust/issues/45302
|
|
|
|
pub fn is_dir(path: &Path) -> bool {
|
|
|
|
if path.file_name().is_some() {
|
|
|
|
path.is_dir()
|
|
|
|
} else {
|
|
|
|
path.is_dir() && path.canonicalize().is_ok()
|
|
|
|
}
|
|
|
|
}
|