From fed03a00698491688561043cd0f9ad83a1a650b9 Mon Sep 17 00:00:00 2001 From: Christin Date: Fri, 20 Nov 2026 03:40:51 +0100 Subject: [PATCH] fix(config): write a migrated config file in place if it is not replaceable Co-Authored-By: Claude Opus 5.5 Assisted-by: Claude Code (claude-opus-5-5) --- src/akkudoktoreos/config/configmigrate.py | 17 +++++++++++------ tests/test_configmigrate.py | 20 ++++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/akkudoktoreos/config/configmigrate.py b/src/akkudoktoreos/config/configmigrate.py index d44dfc9f..cc5944db 100644 --- a/src/akkudoktoreos/config/configmigrate.py +++ b/src/akkudoktoreos/config/configmigrate.py @@ -615,18 +615,19 @@ def remove_empty(obj: dict) -> dict: def _replace_config_file(config_file: Path, content: str) -> None: - """Replace the configuration file atomically. + """Replace the content of the configuration file, atomically where possible. The content is written to a temporary file next to the configuration file, which then replaces it. If writing fails, the configuration file is left untouched. The new file keeps - the permission bits of the old one. + the permission bits of the old one. A file that can not be replaced, e.g. a single-file + bind mount of a container, is written in place instead. Args: config_file (Path): The existing configuration file. content (str): The new content of the configuration file. Raises: - OSError: If the temporary file can not be written or can not replace the file. + OSError: If the content can not be written. """ fd, tmp_name = tempfile.mkstemp( dir=config_file.parent, prefix=f".{config_file.name}.", suffix=".tmp" @@ -637,10 +638,14 @@ def _replace_config_file(config_file: Path, content: str) -> None: f_out.flush() os.fsync(f_out.fileno()) os.chmod(tmp_name, stat.S_IMODE(config_file.stat().st_mode)) - os.replace(tmp_name, config_file) - except BaseException: + try: + os.replace(tmp_name, config_file) + except OSError as e: + logger.debug(f"Could not replace '{config_file}' ({e}), writing it in place.") + with config_file.open("w", encoding="utf-8") as f_out: + f_out.write(content) + finally: Path(tmp_name).unlink(missing_ok=True) - raise def migrate_config_file(config_file: Path, backup_file: Path) -> bool: diff --git a/tests/test_configmigrate.py b/tests/test_configmigrate.py index 241e6c89..a6ce6fa3 100644 --- a/tests/test_configmigrate.py +++ b/tests/test_configmigrate.py @@ -1,5 +1,6 @@ # Test data directory and known migration pairs import copy +import errno import json import os import shutil @@ -180,16 +181,31 @@ class TestConfigMigration: def test_migrate_config_file_write_failure_keeps_original( self, tmp_config_file: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - """A migrated file that can not replace the old one leaves the old file in place.""" + """A migrated configuration that can not be written leaves the old file in place.""" original = tmp_config_file.read_bytes() backup_file = tmp_config_file.with_suffix(".bak") - monkeypatch.setattr(os, "replace", Mock(side_effect=OSError("replace failed"))) + monkeypatch.setattr(os, "fsync", Mock(side_effect=OSError("disk full"))) assert configmigrate.migrate_config_file(tmp_config_file, backup_file) is False assert tmp_config_file.read_bytes() == original assert not list(tmp_config_file.parent.glob("*.tmp")) + def test_migrate_config_file_writes_in_place_if_not_replaceable( + self, tmp_config_file: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + """A file that can not be replaced, e.g. a single-file bind mount, is written in place.""" + original = tmp_config_file.read_bytes() + backup_file = tmp_config_file.with_suffix(".bak") + monkeypatch.setattr(os, "replace", Mock(side_effect=OSError(errno.EBUSY, "busy"))) + + assert configmigrate.migrate_config_file(tmp_config_file, backup_file) is True + + assert backup_file.read_bytes() == original + migrated_data = json.loads(tmp_config_file.read_text(encoding="utf-8")) + assert migrated_data["general"]["version"] == __version__ + assert not list(tmp_config_file.parent.glob("*.tmp")) + @pytest.mark.parametrize("old_file, expected_file", MIGRATION_PAIRS) def test_migrate_old_version_config(self, tmp_path: Path, old_file: Path, expected_file: Path):