Eryk Sun <eryk...@gmail.com> added the comment:

> As far as I know os.stat() resets d.stat() maybe should be added 
> some option to d.stat() to force update(). d.stat(nt_force_update=True).

It depends on the filesystem. NTFS will update the directory entry as soon as 
the link is accessed by CreateFileW. But that's relatively expensive, and 
actually one of the more expensive steps in an os.stat call.

> I am not sure if os.path.getmtime() can reset d.stat().

genericpath.getmtime calls os.stat:

https://github.com/python/cpython/blob/d0981e61a5869c48e0a70a512967558391272a93/Lib/genericpath.py#L53

lexists, exists, getctime, getatime, getmtime, getsize, isdir, and isfile could 
be modified to call WinAPI GetFileAttributesExW [1], which is implemented via 
NtQueryFullAttributesFile [2], an optimized system call to get a file's 
network-open information. This can be significantly faster than the sequence of 
system calls that are required by os.stat. Note that this does not update the 
NTFS directory entry for the accessed link, unlike CreateFileW, but it does 
return updated information.

The GetFileAttributesExW result would be used if the call succeeds and the file 
isn't a reparse point. Otherwise fall back on os.stat (win32_xstat_impl). If 
passed an fd, try GetFileInformationByHandleEx to get the FileBasicInfo and 
FileStandardInfo, or use a single system call via NTAPI NtQueryInformationFile: 
FileNetworkOpenInformation, which is the same info that GetFileAttributesExW 
returns.

This could be implemented in C as nt._basic_stat(filename, 
follow_symlinks=True), where follow_symlinks means the expanded set of Windows 
name-surrogate reparse points. The C implementation would fall back on 
win32_xstat_impl. Note that a basic stat would not guarantee to return the 
following fields: st_ino, st_dev, and st_nlink. 

Alternatively, it could be implemented as a keyword-only basic=True option for 
os.stat, which would be ignored by POSIX. This way the high-level functions 
could continue to have a common implementation in genericpath.py.

[1] 
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesexw
[2] 
https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-zwqueryfullattributesfile

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41106>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to