Eryk Sun added the comment:

The access control list of a file may not grant or may deny the current user 
the right to read file attributes (i.e. lstat). Generally you don't have to 
worry about granted access to parent directories when attempting to stat the 
file or directory, since even standard users have SeChangeNotifyPrivilege to 
bypass path traversal access checks. In this case, ISTM that we at least know 
the file exists if lstat raises PermissionError instead of FileNotFoundError. 
Currently os.path.lexists returns False for any OSError, without distinguishing 
between these two cases.

You may also come across a similar issue when a file's delete disposition is 
set, since Windows doesn't allow opening a deleted file, even to read its 
attributes, and it doesn't actually unlink the file or directory until all 
references are closed. 

For example, create a directory, opened with delete sharing:

    >>> h = _winapi.CreateFile('testglob', 0x40000000, 7, 0, 1,
    ...                        0x2000000|0x1000000|0x80|0x10, 0)
    >>> os.path.isdir('testglob')
    True

Before removing the directory, it can be globbed directly:

    >>> glob.glob('testglob')
    ['testglob']

After removing the directory, it can only be globbed indirectly by listing its 
parent directory:

    >>> os.rmdir('testglob')
    >>> os.lstat('testglob')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [WinError 5] Access is denied: 'testglob'

    >>> glob.glob('testglob')
    []
    >>> glob.glob('testglob*')
    ['testglob']

Once the handle is closed, the directory is unlinked:

    >>> _winapi.CloseHandle(h)
    >>> os.lstat('testglob')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'testglob'
    >>> glob.glob('testglob*')
    []

----------
nosy: +eryksun

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

Reply via email to