Normalize a path by resolving `.` and `..` components. Unlike `canonicalize()`, this doesn't require the path to exist.
(path: &Path)
| 38 | /// |
| 39 | /// Unlike `canonicalize()`, this doesn't require the path to exist. |
| 40 | pub(crate) fn normalize_path(path: &Path) -> PathBuf { |
| 41 | use std::path::Component; |
| 42 | |
| 43 | let mut normalized = PathBuf::new(); |
| 44 | |
| 45 | for component in path.components() { |
| 46 | match component { |
| 47 | Component::Prefix(p) => normalized.push(p.as_os_str()), |
| 48 | Component::RootDir => normalized.push(component.as_os_str()), |
| 49 | Component::CurDir => {} // Skip `.` |
| 50 | Component::ParentDir => { |
| 51 | // Go up one level, but don't go above root |
| 52 | normalized.pop(); |
| 53 | } |
| 54 | Component::Normal(name) => normalized.push(name), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | normalized |
| 59 | } |
| 60 | |
| 61 | // PERMISSION HELPERS |
| 62 |