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

Windows filesystems disallow new opens for a file that has its delete 
disposition set (i.e. the file is marked for deletion). For example, 
CreateFileW() fails with ERROR_ACCESS_DENIED (5) in this case. A file with its 
delete disposition set is still visibly linked in its parent directory. 

The trigger to unlink a file that's marked for deletion depends on the delete 
mode. In Windows 10, DeleteFileW() uses a POSIX delete if it's supported by the 
filesystem (e.g. NTFS). This delete mode unlinks the file as soon the file 
object that was used to set the delete disposition is closed. There's still a 
small window of time, however, in which attempts to open the file will fail 
with an access-denied error.

In os.stat(), if CreateFileW() fails with access denied, FindFirstFileW() is 
called to try to get the stat data from the file's parent directory. But in 
this case it's likely that the file has been unlinked from the directory by the 
time FindFirstFileW() is called.

The original error code from CreateFileW() gets restored if FindFirstFileW() 
fails. This is generally the right thing to do. However, if FindFirstFileW() 
fails with ERROR_FILE_NOT_FOUND (2) or ERROR_PATH_NOT_FOUND (3), then I suggest 
that the previous error code should not be restored.

For example:

        switch (error) {
        case ERROR_ACCESS_DENIED:     /* Cannot sync or read attributes. */
        case ERROR_SHARING_VIOLATION: /* It's a paging file. */
            /* Try reading the parent directory. */
            if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
                /* Cannot read the parent directory. */
                DWORD dir_error = GetLastError();
                if (dir_error != ERROR_FILE_NOT_FOUND &&
                      dir_error != ERROR_PATH_NOT_FOUND) {
                    SetLastError(error);
                }
                return -1;
            }

----------
nosy: +eryksun
stage:  -> needs patch
type:  -> behavior
versions: +Python 3.11, Python 3.9

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

Reply via email to