This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add "follow_symlinks=False" support for "os.utime()" on Windows
Type: enhancement Stage: needs patch
Components: Library (Lib), Windows Versions: Python 3.11
process
Status: open Resolution:
Dependencies: 46506 Superseder:
Assigned To: Nosy List: Delgan, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2022-01-23 18:41 by Delgan, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg411399 - (view) Author: Delgan (Delgan) * Date: 2022-01-23 18:41
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: https://github.com/python/cpython/blob/ca78130d7eb5265759697639e42487ec6d0a4caf/Modules/posixmodule.c#L5516 ?
msg411437 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-01-24 02:28
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.
msg412414 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-02-03 03:48
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.
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90648
2022-02-03 03:48:03eryksunsetmessages: + msg412414
2022-01-24 21:25:36eryksunsetdependencies: + [Windows] wrap CreateFile to support follow_symlinks
2022-01-24 02:28:22eryksunsetnosy: + paul.moore, tim.golden, eryksun, zach.ware, steve.dower
messages: + msg411437

components: + Windows
type: enhancement
stage: needs patch
2022-01-23 18:41:48Delgancreate