Skip to main content

open_enumerated

Function open_enumerated 

Source
pub fn open_enumerated(path: &Path) -> Result<OpenedFile>
Expand description

Open an already-enumerated path once and take its metadata from the HANDLE.

This exists to DESIGN OUT the stat-then-open race rather than retry it. Code that calls fs::metadata(path) to decide a size or a file kind and then calls File::open(path) performs two independent path lookups, and another process can replace the inode in between. The second lookup can fail (a spurious error for a file that is perfectly readable), or worse, succeed against a DIFFERENT file, so the size that was checked against a cap is not the size that gets read.

File::open followed by File::metadata is one lookup plus an fstat on the resulting descriptor. There is no window: the metadata always describes the inode the handle holds open, and on Unix that inode stays readable through the handle even if the name is unlinked afterwards.

The bounded retry here covers only the remaining genuine race, the entry vanishing between enumeration and this single open.