[#2567] Improve TOML Dump Safety

This commit is contained in:
David Sangrey committed 2025-12-26 11:22:31 -05:00
1 parent f61b319fcd
commit 4feb288506
1 file changed
+19 -2
+19 -2
View File
@@ -9,6 +9,7 @@ from __future__ import annotations
import pathlib
import sys
import os
import uuid
import winreg
import datetime
@@ -88,9 +89,25 @@ class WinConfigMinimal:
path_obj = pathlib.Path(toml_path)
path_obj.parent.mkdir(parents=True, exist_ok=True)
tmp_path = path_obj.with_suffix(path_obj.suffix + ".tmp")
config_logger.debug("Writing Config File")
with path_obj.open("wb") as f:
tomli_w.dump(config_data, f)
try:
# Validate serialization BEFORE touching the real file
tomli_w.dumps(config_data)
with tmp_path.open("wb") as f:
tomli_w.dump(config_data, f)
f.flush()
os.fsync(f.fileno())
tmp_path.replace(path_obj)
except Exception:
config_logger.exception("Failed to write TOML config file")
# Ensure temp file doesn't linger
try:
if tmp_path.exists():
tmp_path.unlink()
except Exception:
pass
raise
def close(self):
"""Close registry handle."""