Author eryksun
Recipients eryksun, paul.moore, ramson, steve.dower, tim.golden, zach.ware
Date 2016-09-11.12:03:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473595405.89.0.836499876285.issue28075@psf.upfronthosting.co.za>
In-reply-to
Content
Python 3's os.stat tries to open a handle for the file or directory in order to call GetFileInformationByHandle. Opening a file handle via CreateFile requests at least FILE_READ_ATTRIBUTES and SYNCHRONIZE access when it calls NtCreateFile. If access is denied, os.stat is supposed to fall back on the basic WIN32_FIND_DATA information from FindFirstFile. However, it's not working as intended. For example:

    >>> import os
    >>> os.mkdir('test')
    >>> os.system('icacls test /deny Users:(S,RA)')
    processed file: test
    Successfully processed 1 files; Failed processing 0 files
    0

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

The problem is that it's mistakenly checking for ERROR_SHARING_VIOLATION instead of ERROR_ACCESS_DENIED. Technically, getting a sharing violation should be impossible here. That only applies to requesting execute (traverse), read (list), write (add file), append (add subdirectory), or delete access for the contents of the file or directory, not its metadata.

After modifying the code to instead check for ERROR_ACCESS_DENIED, os.stat correctly falls back on using the file attributes from the WIN32_FIND_DATA:

    >>> os.stat('test')
    os.stat_result(st_mode=16895, st_ino=0, st_dev=0, st_nlink=0, 
    st_uid=0, st_gid=0, st_size=0, st_atime=1473589600, 
    st_mtime=1473589600, st_ctime=1473589600)
History
Date User Action Args
2016-09-11 12:03:25eryksunsetrecipients: + eryksun, paul.moore, tim.golden, ramson, zach.ware, steve.dower
2016-09-11 12:03:25eryksunsetmessageid: <1473595405.89.0.836499876285.issue28075@psf.upfronthosting.co.za>
2016-09-11 12:03:25eryksunlinkissue28075 messages
2016-09-11 12:03:25eryksuncreate