[issue37074] os.stat() does not work for NUL and CON

Eryk Sun report at bugs.python.org
Tue May 28 10:10:09 EDT 2019


Eryk Sun <eryksun at gmail.com> added the comment:

Opening "CON" (i.e. r"\\.\CON") fails with ERROR_INVALID_PARAMETER (87) because it has to be opened with either GENERIC_READ or GENERIC_WRITE data access in order to map it to either the console input buffer or active screen buffer. The CreateFileW call in stat() necessarily requests no data access.

Calling stat on "NUL" (among others such as "CONIN$", "//./C:", and "//./PhysicalDrive0") fails with ERROR_INVALID_FUNCTION (1) -- or possibly ERROR_INVALID_PARAMETER or ERROR_NOT_SUPPORTED (50), depending on the device. stat() calls GetFileInformationByHandle. This requests FileFsVolumeInformation and FileAllInformation, which commonly fail as unsupported or invalid requests for devices other than filesystem devices. Even volume and raw disk devices fail a FileAllInformation request. 

If we have a valid file handle, we can get the file type via GetFileType(hFile), as _Py_fstat_noraise does in Python/fileutils.c. If opening a handle fails with ERROR_INVALID_PARAMETER for a path that resolves to r"\\.\CON" or r"\\?\CON" via GetFullPathNameW, we can simply set st_mode to _S_IFCHR and return.

For example:

    if (hFile == INVALID_HANDLE_VALUE) {
        DWORD lastError = GetLastError();
        if (lastError == ERROR_INVALID_PARAMETER) {
            WCHAR fullPath[8];
            if (GetFullPathNameW(path, sizeof(fullPath),
                    fullPath, NULL) == 7 && (
                _wcsicmp(fullPath, L"\\\\.\\CON") == 0 ||
                _wcsicmp(fullPath, L"\\\\?\\CON") == 0)) {
                memset(result, 0, sizeof(*result));
                result->st_mode = _S_IFCHR;
                return 0;
            }
        }
        /*
            Existing error handling code.
        */
    } else {
        DWORD type = GetFileType(hFile);
        if (type != FILE_TYPE_DISK) {
            CloseHandle(hFile);
            if (type == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
                return -1;
            }
            memset(result, 0, sizeof(*result));
            if (type == FILE_TYPE_CHAR) { /* e.g. "//./NUL" */
                result->st_mode = _S_IFCHR;
            } else if (type == FILE_TYPE_PIPE) { /* e.g. "//./PIPE/Spam" */
                result->st_mode = _S_IFIFO;
            }
            return 0;
        } else if (!GetFileInformationByHandle(hFile, &info)) {
            DWORD lastError = GetLastError();
            CloseHandle(hFile);
            /* e.g. "//./C:" or "//./PhysicalDrive0" */
            if (lastError == ERROR_INVALID_FUNCTION ||
                lastError == ERROR_INVALID_PARAMETER ||
                lastError == ERROR_NOT_SUPPORTED) {
                memset(result, 0, sizeof(*result));
                result->st_mode = _S_IFREG;
                return 0;
            }
            return -1;
        }
    }

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

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37074>
_______________________________________


More information about the Python-bugs-list mailing list