diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-12 16:49:01 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-12 16:49:01 +1000 |
| commit | a53a2568ec6e4e53d32a48443f50eee9d9fb8fcd (patch) | |
| tree | 73a2ecf05724309738f783fc5ed20514768fc624 /src/caelestia/utils/paths.py | |
| parent | material: better mono scheme (diff) | |
| download | caelestia-cli-a53a2568ec6e4e53d32a48443f50eee9d9fb8fcd.tar.gz caelestia-cli-a53a2568ec6e4e53d32a48443f50eee9d9fb8fcd.tar.bz2 caelestia-cli-a53a2568ec6e4e53d32a48443f50eee9d9fb8fcd.zip | |
scheme: fix not saving atomically
Causes programs which rely on the save file (e.g. the shell) to fail occasionally as they try to read while the cli is writing
Diffstat (limited to 'src/caelestia/utils/paths.py')
| -rw-r--r-- | src/caelestia/utils/paths.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/caelestia/utils/paths.py b/src/caelestia/utils/paths.py index 6a6e0a8..3b5a7a6 100644 --- a/src/caelestia/utils/paths.py +++ b/src/caelestia/utils/paths.py @@ -1,5 +1,8 @@ import hashlib +import json import os +import shutil +import tempfile from pathlib import Path config_dir = Path(os.getenv("XDG_CONFIG_HOME", Path.home() / ".config")) @@ -31,3 +34,10 @@ def compute_hash(path: str) -> str: sha.update(chunk) return sha.hexdigest() + + +def atomic_dump(path: Path, content: dict[str, any]) -> None: + with tempfile.NamedTemporaryFile("w") as f: + json.dump(content, f) + f.flush() + shutil.move(f.name, path) |