fix bug
This commit is contained in:
parent
a83bc49a39
commit
afb3938c94
1 changed files with 36 additions and 3 deletions
39
src/utils.rs
39
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<Path>) -> Result<PathBuf> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue