Atomically replace ``path`` with content written by ``write_fn(f)``. Writes a temp file in the SAME directory, then ``os.replace``s it into place (an atomic rename on one filesystem). A process kill (SIGKILL/Ctrl-C), OOM, or ENOSPC mid-write leaves the previous file intact — the destina
(path: "str | Path", write_fn)
| 193 | |
| 194 | |
| 195 | def _atomic_replace(path: "str | Path", write_fn) -> None: |
| 196 | """Atomically replace ``path`` with content written by ``write_fn(f)``. |
| 197 | |
| 198 | Writes a temp file in the SAME directory, then ``os.replace``s it into place |
| 199 | (an atomic rename on one filesystem). A process kill (SIGKILL/Ctrl-C), OOM, or |
| 200 | ENOSPC mid-write leaves the previous file intact — the destination is |
| 201 | untouched until the rename. This is NOT a power-loss durability guarantee: |
| 202 | there is no fsync (matching the rest of the codebase), so an OS/hardware crash |
| 203 | right after the rename can still expose unflushed bytes on some filesystems. |
| 204 | The temp file is removed if the write fails. |
| 205 | |
| 206 | A symlinked destination is resolved first so the write goes THROUGH the link |
| 207 | to its target (rather than replacing the link with a regular file), keeping |
| 208 | the shared-output/worktree symlink setups this module documents working. |
| 209 | """ |
| 210 | # Resolve symlinks so the temp lands on the target's filesystem (same-fs |
| 211 | # atomic rename) and the replace writes through the link, not over it. |
| 212 | real = Path(os.path.realpath(str(path))) |
| 213 | real.parent.mkdir(parents=True, exist_ok=True) |
| 214 | fd, tmp = tempfile.mkstemp(dir=str(real.parent), prefix=".gfy-", suffix=".tmp") |
| 215 | try: |
| 216 | with os.fdopen(fd, "w", encoding="utf-8") as f: |
| 217 | write_fn(f) |
| 218 | # mkstemp creates the temp file 0600; match the destination's existing |
| 219 | # mode (or the umask default for a new file) so an atomic replace never |
| 220 | # silently tightens a previously group/world-readable output to |
| 221 | # owner-only. Best-effort — a chmod failure must not fail the write. |
| 222 | try: |
| 223 | mode = stat.S_IMODE(os.stat(real).st_mode) |
| 224 | except OSError: |
| 225 | umask = os.umask(0) |
| 226 | os.umask(umask) |
| 227 | mode = 0o666 & ~umask |
| 228 | try: |
| 229 | os.chmod(tmp, mode) |
| 230 | except OSError: |
| 231 | pass |
| 232 | # On Windows os.replace refuses a read-only destination and the fallback |
| 233 | # would rename it aside and clobber it. Refuse here instead, for this |
| 234 | # generic write path only: install/cache callers of the fallback must |
| 235 | # still be able to replace their own (possibly read-only) copies. |
| 236 | if os.name == "nt" and real.is_file() and not os.access(real, os.W_OK): |
| 237 | raise PermissionError(errno.EACCES, "destination is read-only", str(real)) |
| 238 | os_replace_with_fallback(tmp, str(real)) |
| 239 | except BaseException: |
| 240 | try: |
| 241 | os.unlink(tmp) |
| 242 | except OSError: |
| 243 | # The temp was chmod'd to match the destination above, so when the |
| 244 | # destination is read-only the temp is too — and Windows refuses to |
| 245 | # unlink a read-only file. Clear the bit and retry, or every failed |
| 246 | # write leaks a `.gfy-*.tmp` into the output directory. |
| 247 | try: |
| 248 | os.chmod(tmp, stat.S_IWRITE) |
| 249 | os.unlink(tmp) |
| 250 | except OSError: |
| 251 | pass |
| 252 | raise |