[issue25639] open 'PhysicalDriveN' on windows fails (since python 3.5) with OSError: [WinError 1] Incorrect function

Eryk Sun report at bugs.python.org
Tue Nov 17 01:39:23 EST 2015


Eryk Sun added the comment:

As a workaround you can open a file descriptor via os.open:

    >>> import os
    >>> fd = os.open(r'\\.\PhysicalDrive0', os.O_RDONLY | os.O_BINARY)
    >>> os.read(fd, 512)[:8]
    b'3\xc0\x8e\xd0\xbc\x00|\x8e'

> _Py_fstat result not used on windows in this place. 

While it's pretty much useless, it is possible to open a CRT file descriptor for a directory handle:

    >>> h = _winapi.CreateFile('C:\\', 0x80000000, 3, 0, 3, 0x02000000, 0)
    >>> fd = msvcrt.open_osfhandle(h, os.O_RDONLY)
    >>> open(fd)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IsADirectoryError: [Errno 21] Is a directory: 3

So calling _Py_fstat shouldn't be skipped on Windows.

The issue is that a raw disk device doesn't support querying information (IRP_MJ_QUERY_INFORMATION), as shown in this local kernel debugging session:

    lkd> !object \GLOBAL??\PhysicalDrive0
    [...]
        Target String is '\Device\Harddisk0\DR0'

    lkd> !object \Device\Harddisk0\DR0
    Object: ffffe001c4fa4500  Type: (ffffe001c3d7bd30) Device
    [...]

    lkd> !devobj ffffe001c4fa4500
    Device object (ffffe001c4fa4500) is for:
     DR0 \Driver\disk DriverObject ffffe001c4fa3310
    [...]

    lkd> !drvobj \Driver\disk 2
    [...]
    Dispatch routines:
    [...]
    [05] IRP_MJ_QUERY_INFORMATION  fffff8037251a06c  nt!IopInvalidDeviceRequest
    [06] IRP_MJ_SET_INFORMATION    fffff8037251a06c  nt!IopInvalidDeviceRequest
    [...]

Specifically WinAPI GetFileInformationByHandle calls the system function NtQueryInformationFile. The system call makes an IRP_MJ_QUERY_INFORMATION I/O request of the driver, which for a raw disk device is handled by IopInvalidDeviceRequest. This fails the request with STATUS_INVALID_DEVICE_REQUEST (0xC0000010), which translates to WinAPI ERROR_INVALID_FUNCTION (0x0001). 

Possibly in the case of ERROR_INVALID_FUNCTION, FileIO can just call PyErr_Clear and skip the code that uses fdfstat.

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

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25639>
_______________________________________


More information about the Python-bugs-list mailing list