2020-04-11 19:05:29 +00:00
|
|
|
use std::io;
|
|
|
|
use std::path::{Component, Path, PathBuf};
|
|
|
|
|
|
|
|
pub fn normalize(path: impl AsRef<Path>) -> PathBuf {
|
|
|
|
let mut normalized = PathBuf::new();
|
|
|
|
for component in path.as_ref().components() {
|
|
|
|
match component {
|
|
|
|
Component::Normal(normal) => {
|
|
|
|
if let Some(normal) = normal.to_str() {
|
|
|
|
if normal.chars().all(|c| c == '.') {
|
|
|
|
for _ in 0..(normal.len() - 1) {
|
|
|
|
normalized.push("..");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
normalized.push(normal);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
normalized.push(normal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c => normalized.push(c.as_os_str()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalized
|
|
|
|
}
|
|
|
|
|
2020-04-12 08:33:38 +00:00
|
|
|
fn canonicalize_core<P, Q>(relative_to: P, path: Q) -> PathBuf
|
2020-04-11 19:05:29 +00:00
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
Q: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let path = normalize(path);
|
|
|
|
let (relative_to, path) = if path.is_absolute() {
|
|
|
|
let components: Vec<_> = path.components().collect();
|
|
|
|
let separator = components
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, c)| c == &&Component::CurDir || c == &&Component::ParentDir);
|
|
|
|
|
|
|
|
if let Some((index, _)) = separator {
|
|
|
|
let (absolute, relative) = components.split_at(index);
|
|
|
|
let absolute: PathBuf = absolute.iter().collect();
|
|
|
|
let relative: PathBuf = relative.iter().collect();
|
|
|
|
|
|
|
|
(absolute, relative)
|
|
|
|
} else {
|
|
|
|
(relative_to.as_ref().to_path_buf(), path)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(relative_to.as_ref().to_path_buf(), path)
|
|
|
|
};
|
|
|
|
|
2020-04-12 08:33:38 +00:00
|
|
|
if path.is_relative() {
|
2020-04-11 19:05:29 +00:00
|
|
|
let mut result = relative_to;
|
|
|
|
path.components().for_each(|component| match component {
|
|
|
|
Component::ParentDir => {
|
|
|
|
result.pop();
|
|
|
|
}
|
|
|
|
Component::Normal(normal) => result.push(normal),
|
|
|
|
_ => {}
|
|
|
|
});
|
|
|
|
|
|
|
|
result
|
|
|
|
} else {
|
|
|
|
path
|
2020-04-12 08:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-11 19:05:29 +00:00
|
|
|
|
2020-04-12 08:33:38 +00:00
|
|
|
pub fn canonicalize_existing<P, Q>(relative_to: P, path: Q) -> io::Result<PathBuf>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
Q: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let canonicalized = canonicalize_core(relative_to, path);
|
|
|
|
let path = match std::fs::read_link(&canonicalized) {
|
2020-04-11 19:05:29 +00:00
|
|
|
Ok(resolved) => resolved,
|
|
|
|
Err(e) => {
|
2020-04-12 08:33:38 +00:00
|
|
|
if canonicalized.exists() {
|
|
|
|
canonicalized
|
2020-04-11 19:05:29 +00:00
|
|
|
} else {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(dunce::simplified(&path).to_path_buf())
|
|
|
|
}
|
|
|
|
|
2020-04-12 08:33:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn canonicalize_missing<P, Q>(relative_to: P, path: Q) -> PathBuf
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
Q: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let canonicalized = canonicalize_core(relative_to, path);
|
|
|
|
let path = match std::fs::read_link(&canonicalized) {
|
|
|
|
Ok(resolved) => resolved,
|
|
|
|
Err(_) => canonicalized,
|
|
|
|
};
|
|
|
|
|
|
|
|
dunce::simplified(&path).to_path_buf()
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:05:29 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize_three_dots() {
|
|
|
|
assert_eq!(PathBuf::from("../.."), normalize("..."));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize_three_dots_with_redundant_dot() {
|
|
|
|
assert_eq!(PathBuf::from("./../.."), normalize("./..."));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-12 08:33:38 +00:00
|
|
|
fn canonicalize_missing_two_dots() {
|
|
|
|
let relative_to = Path::new("/foo/bar");
|
2020-04-11 19:05:29 +00:00
|
|
|
let path = Path::new("..");
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-12 08:33:38 +00:00
|
|
|
PathBuf::from("/foo"), // missing path
|
|
|
|
canonicalize_missing(relative_to, path)
|
2020-04-11 19:05:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-12 08:33:38 +00:00
|
|
|
fn canonicalize_missing_three_dots() {
|
|
|
|
let relative_to = Path::new("/foo/bar/baz");
|
2020-04-11 19:05:29 +00:00
|
|
|
let path = Path::new("...");
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-12 08:33:38 +00:00
|
|
|
PathBuf::from("/foo"), // missing path
|
|
|
|
canonicalize_missing(relative_to, path)
|
2020-04-11 19:05:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-12 08:33:38 +00:00
|
|
|
fn canonicalize_missing_three_dots_with_redundant_dot() {
|
|
|
|
let relative_to = Path::new("/foo/bar/baz");
|
2020-04-11 19:05:29 +00:00
|
|
|
let path = Path::new("./...");
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-12 08:33:38 +00:00
|
|
|
PathBuf::from("/foo"), // missing path
|
|
|
|
canonicalize_missing(relative_to, path)
|
2020-04-11 19:05:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-12 08:33:38 +00:00
|
|
|
fn canonicalize_existing_three_dots() -> io::Result<()> {
|
|
|
|
let relative_to = Path::new("/foo/bar/");
|
2020-04-11 19:05:29 +00:00
|
|
|
let path = Path::new("...");
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-12 08:33:38 +00:00
|
|
|
PathBuf::from("/"), // existing path
|
|
|
|
canonicalize_existing(relative_to, path)?
|
2020-04-11 19:05:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-12 08:33:38 +00:00
|
|
|
fn canonicalize_existing_three_dots_should_fail() {
|
|
|
|
let relative_to = Path::new("/foo/bar/baz"); // '/foo' is missing
|
2020-04-11 19:05:29 +00:00
|
|
|
let path = Path::new("...");
|
|
|
|
|
2020-04-12 08:33:38 +00:00
|
|
|
assert!(canonicalize_existing(relative_to, path).is_err());
|
2020-04-11 19:05:29 +00:00
|
|
|
}
|
|
|
|
}
|