Skip to content

os.stat fails for block devices such as //./PhysicalDrive0 #82211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eryksun opened this issue Sep 4, 2019 · 3 comments
Closed

os.stat fails for block devices such as //./PhysicalDrive0 #82211

eryksun opened this issue Sep 4, 2019 · 3 comments
Assignees
Labels
3.8 (EOL) end of life 3.9 only security fixes OS-windows topic-IO type-bug An unexpected behavior, bug, or error

Comments

@eryksun
Copy link
Contributor

eryksun commented Sep 4, 2019

BPO 38030
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @miss-islington
PRs
  • bpo-38030: Fix os.stat failures on block devices on Windows #15681
  • [3.8] bpo-38030: Fix os.stat failures on block devices on Windows (GH-15681) #15682
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/zooba'
    closed_at = <Date 2019-09-04.22:18:27.827>
    created_at = <Date 2019-09-04.20:26:32.107>
    labels = ['type-bug', '3.8', 'expert-IO', 'OS-windows', '3.9']
    title = 'os.stat fails for block devices such as //./PhysicalDrive0'
    updated_at = <Date 2019-09-04.22:18:27.826>
    user = 'https://github.com/eryksun'

    bugs.python.org fields:

    activity = <Date 2019-09-04.22:18:27.826>
    actor = 'steve.dower'
    assignee = 'steve.dower'
    closed = True
    closed_date = <Date 2019-09-04.22:18:27.827>
    closer = 'steve.dower'
    components = ['Windows', 'IO']
    creation = <Date 2019-09-04.20:26:32.107>
    creator = 'eryksun'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38030
    keywords = ['patch']
    message_count = 3.0
    messages = ['351149', '351151', '351152']
    nosy_count = 6.0
    nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'miss-islington']
    pr_nums = ['15681', '15682']
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38030'
    versions = ['Python 3.8', 'Python 3.9']

    @eryksun
    Copy link
    Contributor Author

    eryksun commented Sep 4, 2019

    In bpo-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;
            }

    @eryksun eryksun added 3.8 (EOL) end of life 3.9 only security fixes OS-windows topic-IO type-bug An unexpected behavior, bug, or error labels Sep 4, 2019
    @zooba zooba self-assigned this Sep 4, 2019
    @zooba
    Copy link
    Member

    zooba commented Sep 4, 2019

    New changeset 772ec0f by Steve Dower in branch 'master':
    bpo-38030: Fix os.stat failures on block devices on Windows (GH-15681)
    772ec0f

    @miss-islington
    Copy link
    Contributor

    New changeset cad7abf by Miss Islington (bot) in branch '3.8':
    bpo-38030: Fix os.stat failures on block devices on Windows (GH-15681)
    cad7abf

    @zooba zooba closed this as completed Sep 4, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    thunze added a commit to thunze/diskfs that referenced this issue Aug 4, 2022
    - Not porting back to Python 3.7 for now because of python/cpython#82211
    - Also enable forward references for most modules via the according future statement
    - Also normalize optional types
    - Also bump dependencies and change version constraint for mypy
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life 3.9 only security fixes OS-windows topic-IO type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants