From 7ff859cd0502fedc2b101777df6d6ba160450976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 28 Nov 2026 22:21:08 +0300 Subject: [PATCH] fix(rotation): lab7 replace read-only files on WSL mounts --- scripts/rotation_common.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/scripts/rotation_common.py b/scripts/rotation_common.py index 51085a8..08c730d 100644 --- a/scripts/rotation_common.py +++ b/scripts/rotation_common.py @@ -3,6 +3,7 @@ import fcntl import getpass import json import os +import stat import ssl import subprocess import tempfile @@ -22,14 +23,30 @@ def compose(*args, **kwargs): def write_file(path, data, mode): with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as tmp: + staged = Path(tmp.name) + try: + with staged.open("wb") as stream: + stream.write(data) + stream.flush() + os.fchmod(stream.fileno(), mode) + os.fsync(stream.fileno()) try: - tmp.write(data) - tmp.flush() - os.fchmod(tmp.fileno(), mode) - os.fsync(tmp.fileno()) - os.replace(tmp.name, path) - finally: - Path(tmp.name).unlink(missing_ok=True) + os.replace(staged, path) + except PermissionError: + previous = stat.S_IMODE(path.stat().st_mode) + if previous & stat.S_IWUSR: + raise + path.chmod(previous | stat.S_IWUSR) + try: + os.replace(staged, path) + except OSError: + path.chmod(previous) + raise + finally: + if staged.exists(): + staged.chmod(0o600) + staged.unlink() + class Operator: