``os.replace(src, dst)``, falling back to a copy for a known set of Windows quirks (#3508) that raise even when ``src``/``dst`` are the same directory on the same drive: ``PermissionError`` (WinError 5/32 -- destination briefly locked by another handle, antivirus, an open reader) and
(src: "str | Path", dst: "str | Path")
| 121 | |
| 122 | |
| 123 | def os_replace_with_fallback(src: "str | Path", dst: "str | Path") -> None: |
| 124 | """``os.replace(src, dst)``, falling back to a copy for a known set of |
| 125 | Windows quirks (#3508) that raise even when ``src``/``dst`` are the same |
| 126 | directory on the same drive: ``PermissionError`` (WinError 5/32 -- |
| 127 | destination briefly locked by another handle, antivirus, an open reader) |
| 128 | and WinError 17 ("cannot move to a different disk drive", observed on some |
| 129 | Windows/filesystem combinations despite textbook same-volume semantics). |
| 130 | WinError 17 maps to a plain ``OSError`` in Python, not ``PermissionError``, |
| 131 | so it's checked via ``winerror`` rather than the exception type. Any other |
| 132 | failure is a real one and is re-raised. |
| 133 | |
| 134 | ``os.replace`` atomically swaps whatever sits at ``dst`` -- including a |
| 135 | symlink, which it REPLACES in place rather than following (some callers, |
| 136 | e.g. install.py's managed skill symlinks, #3286, rely on exactly this). |
| 137 | A naive ``shutil.copy2(src, dst)`` does the opposite when ``dst`` is a |
| 138 | symlink: opening it for writing follows the link and overwrites its |
| 139 | TARGET's content instead. So the fallback copies to a fresh temp file in |
| 140 | ``dst``'s directory first, renames whatever is currently at ``dst`` (link |
| 141 | or file) aside as a backup rather than deleting it outright, and only |
| 142 | then renames the temp copy into ``dst``'s place -- matching replace's |
| 143 | "whatever was there is gone, a plain file replaces it" semantics. If that |
| 144 | final rename fails, the backup is renamed straight back so a mid-swap |
| 145 | failure leaves the original in place rather than leaving ``dst`` missing. |
| 146 | """ |
| 147 | try: |
| 148 | os.replace(src, dst) |
| 149 | return |
| 150 | except OSError as exc: |
| 151 | if not isinstance(exc, PermissionError) and getattr(exc, "winerror", None) != 17: |
| 152 | raise |
| 153 | import shutil |
| 154 | dst = os.fspath(dst) |
| 155 | if os.path.normcase(os.path.abspath(os.fspath(src))) == os.path.normcase(os.path.abspath(dst)): |
| 156 | # Replacing a path with itself needs no swap at all; the rename-aside- |
| 157 | # then-back sequence below would rename src out from under itself via |
| 158 | # the "back up dst" step and then crash unlinking a path that no |
| 159 | # longer exists at the end. |
| 160 | return |
| 161 | dst_dir = os.path.dirname(dst) or "." |
| 162 | fd, tmp_copy = tempfile.mkstemp(dir=dst_dir, prefix=".gfy-replace-", suffix=".tmp") |
| 163 | os.close(fd) |
| 164 | try: |
| 165 | shutil.copy2(src, tmp_copy) |
| 166 | backup = None |
| 167 | if os.path.lexists(dst): |
| 168 | bfd, backup = tempfile.mkstemp(dir=dst_dir, prefix=".gfy-replace-bak-", suffix=".tmp") |
| 169 | os.close(bfd) |
| 170 | os.unlink(backup) # reserve the name only; rename needs it free on Windows |
| 171 | os.rename(dst, backup) # a plain rename moves a symlink itself, never its target |
| 172 | try: |
| 173 | os.rename(tmp_copy, dst) |
| 174 | except BaseException: |
| 175 | if backup is not None: |
| 176 | try: |
| 177 | os.rename(backup, dst) |
| 178 | except OSError: |
| 179 | pass # best-effort restore; the swap failure below still propagates |
| 180 | raise |