From afb3938c9434d1359d44f5a1a04506118abc46bd Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 11 Jan 2021 03:07:33 -0600 Subject: [PATCH] fix bug --- src/utils.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 57ab530..8f444de 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -11,14 +11,32 @@ use crate::errors::Result; const MASK: &AsciiSet = percent_encoding::CONTROLS; +/// Converts a path to its absolute form WITHOUT resolving symlinks. pub fn into_absolute(path: impl AsRef) -> Result { let path = path.as_ref(); - Ok(if !path.is_absolute() { - env::current_dir()?.canonicalize()?.join(path) + let mut new_path = if !path.is_absolute() { + env::current_dir()? } else { 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 { @@ -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); + } +}