This commit is contained in:
Michael Zhang 2021-01-11 03:07:33 -06:00
parent a83bc49a39
commit afb3938c94
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -11,14 +11,32 @@ use crate::errors::Result;
const MASK: &AsciiSet = percent_encoding::CONTROLS; const MASK: &AsciiSet = percent_encoding::CONTROLS;
/// Converts a path to its absolute form WITHOUT resolving symlinks.
pub fn into_absolute(path: impl AsRef<Path>) -> Result<PathBuf> { pub fn into_absolute(path: impl AsRef<Path>) -> Result<PathBuf> {
let path = path.as_ref(); let path = path.as_ref();
Ok(if !path.is_absolute() { let mut new_path = if !path.is_absolute() {
env::current_dir()?.canonicalize()?.join(path) env::current_dir()?
} else { } else {
path.to_path_buf() path.to_path_buf()
}) };
for component in path.components() {
match component {
Component::Prefix(_) => unimplemented!("windows lol"),
Component::RootDir => {
}
Component::CurDir => {}
Component::ParentDir => {
new_path.pop();
}
Component::Normal(s) => {
new_path.push(s);
}
}
}
Ok(new_path)
} }
pub fn get_uid() -> u64 { pub fn get_uid() -> u64 {
@ -113,3 +131,18 @@ macro_rules! concat_os_str {
} }
}; };
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_absolute() {
let cwd = env::current_dir().unwrap();
let path = PathBuf::from("..");
let parent = cwd.parent().unwrap();
assert_eq!(into_absolute(&path).unwrap(), parent);
let path2 = path.join(cwd.file_name().unwrap());
assert_eq!(into_absolute(&path2).unwrap(), cwd);
}
}