#Path Normalisation
6 messages · Page 1 of 1 (latest)
I found https://docs.rs/normpath/latest/normpath/trait.PathExt.html#tymethod.normalize but the issue with that is that it also canonicalises the path
we do something similar to sanitize paths here:
https://github.com/actix/actix-web/blob/master/actix-files/src/path_buf.rs#L26
not a copy-paste solution but easily customizable, i expect
This seems to work
pub(crate) fn normalise_path<P: AsRef<Path>>(path: P) -> PathBuf {
let mut buf = PathBuf::new();
let segments = path.as_ref().components();
for segment in segments {
match segment {
Component::ParentDir => { buf.pop(); },
Component::CurDir => {},
segment => buf.push(segment)
}
}
buf
}
A little warning: foo/../bar is not the same as bar, since foo can be a symlink in which case it would visit the parent directory of the folder foo links to
I know symlinks are an issue but for now, I'm going to ignore those