3 ms·
> `filepath.Clean()` is not called `filepath.Canonicalize()` Canonicalization of a path is a different operation, as a name suggests it returns a canonical pat
by GlitchMr 4y ago
> `filepath.Clean()` is not called `filepath.Canonicalize()`
Canonicalization of a path is a different operation, as a name suggests it returns a canonical path to a resource. The idea being that if you happen to have two paths that refer to the same file (say, `/bin/sh` and `/usr/bin/sh`) then you should be able to pass those to path canonicalization function to get the same string for both. This is not what `filepath.Clean` does, so calling it `Canonicalize` would be confusing.
For example, in Rust the following program when run in Rust playground (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4a7942a60ea5d9088a1e50faed82c7dd https://play.rust-lang.org/?version=stable&mode=debug&editio...) will output /usr/bin/dash:
fn main() -> std::io::Result<()> {
println!("{}", std::fs::canonicalize("/bin/sh")?.display());
Ok(())
}
Meanwhile, Go's `Clean` function is only concerned about lexical processing and ignores the file system entirely and would return `/bin/sh` here, as none of rules `Clean` uses apply.
- typesanitizer 4y agoThis is a fair point. To be clear, I wasn't suggesting Canonicalize as _the_ name, but as one potential candidate. As you've shown, it has some shortcomings. Perhaps Normalize is a better alternative (and also another candidate that I suggest), since "Normalization" is a commonly used term for converting to a standard/normal form.