[issue38030] os.stat fails for block devices such as //./PhysicalDrive0
Eryk Sun
report at bugs.python.org
Wed Sep 4 16:26:32 EDT 2019
New submission from Eryk Sun <eryksun at gmail.com>:
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;
}
----------
components: IO, Windows
messages: 351149
nosy: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
priority: high
severity: normal
stage: needs patch
status: open
title: os.stat fails for block devices such as //./PhysicalDrive0
type: behavior
versions: Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38030>
_______________________________________
More information about the Python-bugs-list
mailing list