classification
Title: os.stat fails for block devices such as //./PhysicalDrive0
Type: behavior Stage: resolved
Components: IO, Windows Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: steve.dower Nosy List: eryksun, miss-islington, paul.moore, steve.dower, tim.golden, zach.ware
Priority: high Keywords: patch

Created on 2019-09-04 20:26 by eryksun, last changed 2019-09-04 22:18 by steve.dower. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15681 closed steve.dower, 2019-09-04 21:10
PR 15682 merged miss-islington, 2019-09-04 21:43
Messages (3)
msg351149 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-09-04 20:26
In issue 37834, I posted a suggest implementation of win32_xstat_impl that included the following snippet of code:

        if (!GetFileInformationByHandle(hFile, &fileInfo)) {
            error = GetLastError();
            if (error != ERROR_INVALID_PARAMETER &&
                error != ERROR_INVALID_FUNCTION &&
                error != ERROR_NOT_SUPPORTED) {
                retval = -1;
                goto cleanup;
            }
            /* Volumes and physical disks are block devices, e.g.
               \\.\C: and \\.\PhysicalDrive0. */
            memset(result, 0, sizeof(*result));
            result->st_mode = 0x6000; /* S_IFBLK */
            goto cleanup;
        }

This is meant to handle the above errors. We know we have FILE_TYPE_DISK. If GetFileInformationByHandle fails with one of the above error codes, then we must have a disk or volume device without a mounted file-system device. This should be reported as a block device.

However it was changed in the final version as follows:

        if (!GetFileInformationByHandle(hFile, &fileInfo)) {
            switch (GetLastError()) {
            case ERROR_INVALID_PARAMETER:
            case ERROR_INVALID_FUNCTION:
            case ERROR_NOT_SUPPORTED:
                retval = -1;
                goto cleanup;
            }
            /* Volumes and physical disks are block devices, e.g.
               \\.\C: and \\.\PhysicalDrive0. */
            memset(result, 0, sizeof(*result));
            result->st_mode = 0x6000; /* S_IFBLK */
            goto cleanup;
        }

Now it's failing on the errors that should be handled, and succeeding on all other errors that should fail. If we want to use a switch statement here, I suggest the following:

        if (!GetFileInformationByHandle(hFile, &fileInfo)) {
            switch (GetLastError()) {
            case ERROR_INVALID_PARAMETER:
            case ERROR_INVALID_FUNCTION:
            case ERROR_NOT_SUPPORTED:
                /* Volumes and physical disks are block devices, e.g.
                   \\.\C: and \\.\PhysicalDrive0. */
                memset(result, 0, sizeof(*result));
                result->st_mode = 0x6000; /* S_IFBLK */
                goto cleanup;
            }
            retval = -1;
            goto cleanup;
        }
msg351151 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-09-04 21:43
New changeset 772ec0fad57412daa53d16d7019b6b2fe6e94942 by Steve Dower in branch 'master':
bpo-38030: Fix os.stat failures on block devices on Windows (GH-15681)
https://github.com/python/cpython/commit/772ec0fad57412daa53d16d7019b6b2fe6e94942
msg351152 - (view) Author: miss-islington (miss-islington) Date: 2019-09-04 22:18
New changeset cad7abf8abe657b696b9c8deb4b727e0cefaf36d by Miss Islington (bot) in branch '3.8':
bpo-38030: Fix os.stat failures on block devices on Windows (GH-15681)
https://github.com/python/cpython/commit/cad7abf8abe657b696b9c8deb4b727e0cefaf36d
History
Date User Action Args
2019-09-04 22:18:27steve.dowersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-04 22:18:08miss-islingtonsetnosy: + miss-islington
messages: + msg351152
2019-09-04 21:43:18miss-islingtonsetpull_requests: + pull_request15342
2019-09-04 21:43:03steve.dowersetmessages: + msg351151
2019-09-04 21:10:12steve.dowersetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request15341
2019-09-04 20:55:09steve.dowersetassignee: steve.dower
2019-09-04 20:26:32eryksuncreate