gh-99726: Add 'fast' argument to os.[l]stat for faster calculation by zooba · Pull Request #99727 · python/cpython (original) (raw)
This is problematic. Instead, open a handle via CreateFileW()
and call GetVolumeInformationByHandleW()
. It's no more expensive than calling GetVolumeInformationW()
, which also has to open a path, and it's reliable. That said, we're only here because it isn't a 'fast' stat, so we may as well take the slow path from the start.
PathCchSkipRoot()
only supports backslash as the path separator. That's an irrelevant problem, however, considering the fundamental problem that the path may contain mount points, symlinks, and other reparse points. In this case the drive/device of the opened path is probably irrelevant to the file.
GetVolumeInformationW()
will open any path that has a trailing slash. However, after opening the path it queries the FileNameInformation
to verify that the final path ends in a backslash, i.e. that a filesystem root directory was opened. The problem is finding the relevant root directory in the path without resolving the final path. GetVolumePathNameW()
is supposed to handle this, but I find that it's buggy with substitute drives, for which it returns ridiculous results. It also returns the wrong result for a symlink to a UNC path. And it's more work than simply opening the path.
Actually, the requirement for a root directory is irrelevant to the underlying system call that queries the FileFsVolumeInformation (volume name and serial number) and FileFsAttributeInformation (filesystem name, flags, and max component length). Any open file on the volume is sufficient. Hence, just open the path and call GetVolumeInformationByHandleW()
.