How do i read a file without following symlinks? (including symlinks further up the path)
⚓ Rust 📅 2026-07-16 👤 surdeus 👁️ 1the obvious answer would be something like this:
(ignore the atrocious error handling, it makes the code shorter)
...
let path = ...;
let canonical = path.canonicalize();
if path != canonical {
panic!();
}
// the path can't contain symlinks as that would make it differ from canonical.
let file = File::open(path).unwrap();
let mut buffer = String::new();
file.read_to_string(&mut buffer).unwrap();
...
but that is fundementally flawed, some entry in path could be replaced by a symlink after the check, but before the file is opened and read.
4 posts - 3 participants
🏷️ Rust_feed