From bbd65d9c1adcb9d8c76ba6fcb2b76a8064935648 Mon Sep 17 00:00:00 2001 From: Jelmer de Hen <18084450+jelmerdehen@users.noreply.github.com> Date: Sun, 26 Apr 2026 04:04:42 +0200 Subject: [PATCH] fix(config): write config atomically via tmp + rename writeFile overwrote the existing config in place. A crash, power loss, or out-of-disk during the write would leave a truncated or corrupt config behind. Write to .tmp and rename, so the destination either holds the previous contents or the full new contents. Clean up the tmp file on error. --- src/logid/Configuration.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/logid/Configuration.cpp b/src/logid/Configuration.cpp index e49b5eaf..2b90a73e 100644 --- a/src/logid/Configuration.cpp +++ b/src/logid/Configuration.cpp @@ -56,13 +56,19 @@ Configuration::Configuration() { void Configuration::save() { config::set(_config.getRoot(), *this); + const std::string tmp_path = _config_file + ".tmp"; try { - _config.writeFile(_config_file.c_str()); + _config.writeFile(tmp_path.c_str()); + std::filesystem::rename(tmp_path, _config_file); } catch (const FileIOException& e) { + std::error_code ec; + std::filesystem::remove(tmp_path, ec); logPrintf(ERROR, "I/O Error while writing %s: %s", _config_file.c_str(), e.what()); throw; } catch (const std::exception& e) { + std::error_code ec; + std::filesystem::remove(tmp_path, ec); logPrintf(ERROR, "Error while writing %s: %s", _config_file.c_str(), e.what()); throw;