Skip to content
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

Add "follow_symlinks=False" support for "os.utime()" on Windows #90648

Open
Delgan mannequin opened this issue Jan 23, 2022 · 3 comments
Open

Add "follow_symlinks=False" support for "os.utime()" on Windows #90648

Delgan mannequin opened this issue Jan 23, 2022 · 3 comments
Labels
3.11 only security fixes OS-windows stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@Delgan
Copy link
Mannequin

Delgan mannequin commented Jan 23, 2022

BPO 46490
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @Delgan
Dependencies
  • bpo-46506: [Windows] wrap CreateFile to support follow_symlinks
  • 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 = None
    closed_at = None
    created_at = <Date 2022-01-23.18:41:48.831>
    labels = ['type-feature', 'library', 'OS-windows', '3.11']
    title = 'Add "follow_symlinks=False" support for "os.utime()" on Windows'
    updated_at = <Date 2022-02-03.03:48:03.335>
    user = 'https://github.com/Delgan'

    bugs.python.org fields:

    activity = <Date 2022-02-03.03:48:03.335>
    actor = 'eryksun'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2022-01-23.18:41:48.831>
    creator = 'Delgan'
    dependencies = ['46506']
    files = []
    hgrepos = []
    issue_num = 46490
    keywords = []
    message_count = 3.0
    messages = ['411399', '411437', '412414']
    nosy_count = 6.0
    nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'Delgan']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46490'
    versions = ['Python 3.11']

    @Delgan
    Copy link
    Mannequin Author

    Delgan mannequin commented Jan 23, 2022

    Hi.

    Currently, trying to use "os.utime(path, timestamps, follow_symlinks=False)" raises a exception on Windows: "NotImplementedError: utime: follow_symlinks unavailable on this platform".

    Looking at the Win32 API it seems possible to open a symbolic link by specifying the "FILE_FLAG_OPEN_REPARSE_POINT" flag: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew#symbolic-link-behavior

    Do you think it would be possible to update "os.utime()" implementation and optionally pass the flag here:

    FILE_FLAG_BACKUP_SEMANTICS, NULL);
    ?

    @Delgan Delgan mannequin added 3.11 only security fixes stdlib Python modules in the Lib dir labels Jan 23, 2022
    @eryksun
    Copy link
    Contributor

    eryksun commented Jan 24, 2022

    The Windows API doesn't directly support opening a 'symlink' as Python defines it for the follow_symlinks parameter. That problem should be resolved in a separate issue. Then updating os.utime() would be relatively trivial.

    @eryksun eryksun added OS-windows type-feature A feature request or enhancement labels Jan 24, 2022
    @eryksun
    Copy link
    Contributor

    eryksun commented Feb 3, 2022

    In case you missed it, I implemented _Py_CreateFile2() in bpo-46506 and rewrote os.stat() based on it. Check it out in case you're interested in moving forward with a PR in bpo-46506.

    For this issue, follow_symlinks is fairly simple to support with _Py_CreateFile2(). We may as well add fd support, since that's trivial to add. For example:

        if (path->fd != -1) {
            hFile = _Py_get_osfhandle(path->fd);
        } else {
            Py_BEGIN_ALLOW_THREADS
            hFile = _Py_CreateFile2(path->wide, FILE_WRITE_ATTRIBUTES, 0,
                        OPEN_EXISTING, NULL, follow_symlinks, NULL);
            Py_END_ALLOW_THREADS
        }
        if (hFile == INVALID_HANDLE_VALUE) {
            if (path->fd == -1) {
                path_error(path);
            }
            return NULL;
        }

    One also has to define the following macros to declare follow_symlinks and fd support: UTIME_HAVE_NOFOLLOW_SYMLINKS and PATH_UTIME_HAVE_FD.

    To announce support in os.supports_follow_symlinks and os.supports_fd, it should be conditioned on MS_WINDOWS, i.e. _add("MS_WINDOWS", "utime"). The os module is frozen, so changing these two sets requires rebuilding Python.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes OS-windows stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant