MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / normalize_path

Function normalize_path

crates/openshell-core/src/paths.rs:137–156  ·  view source on GitHub ↗

Normalize a filesystem path by collapsing redundant separators and removing trailing slashes, without requiring the path to exist on disk. This is a lexical normalization only — it does NOT resolve symlinks or check the filesystem. `..` components are preserved verbatim; callers that need to reject parent traversal must validate separately.

(path: &str)

Source from the content-addressed store, hash-verified

135/// check the filesystem. `..` components are preserved verbatim; callers that
136/// need to reject parent traversal must validate separately.
137pub fn normalize_path(path: &str) -> String {
138 use std::path::Component;
139
140 let p = Path::new(path);
141 let mut normalized = PathBuf::new();
142 for component in p.components() {
143 match component {
144 Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
145 #[allow(clippy::path_buf_push_overwrite)]
146 Component::RootDir => normalized.push("/"),
147 Component::CurDir => {} // skip "."
148 Component::ParentDir => {
149 // Keep ".." — validation will catch it separately
150 normalized.push("..");
151 }
152 Component::Normal(c) => normalized.push(c),
153 }
154 }
155 normalized.to_string_lossy().to_string()
156}
157
158#[cfg(test)]
159mod tests {

Callers 1

fromMethod · 0.70

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected