| 310 | } |
| 311 | |
| 312 | void ConfigManager::SaveConfig() const |
| 313 | { |
| 314 | if (m_Config.DisableSaving) |
| 315 | { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | std::filesystem::path tempFile = m_ConfigPath; |
| 320 | tempFile.replace_extension(L".tmp"); |
| 321 | |
| 322 | wil::unique_file file; |
| 323 | const errno_t err = _wfopen_s(file.put(), tempFile.c_str(), L"wbS"); |
| 324 | if (err == 0) |
| 325 | { |
| 326 | SaveToFile(file.get()); |
| 327 | |
| 328 | file.reset(); |
| 329 | if (!ReplaceFile(m_ConfigPath.c_str(), tempFile.c_str(), nullptr, REPLACEFILE_WRITE_THROUGH | REPLACEFILE_IGNORE_MERGE_ERRORS | REPLACEFILE_IGNORE_ACL_ERRORS, nullptr, nullptr)) |
| 330 | { |
| 331 | // If the target file doesn't exist (e.g. brand new installation of TranslucentTB), ReplaceFile fails. |
| 332 | if (const auto lastErr = GetLastError(); lastErr == ERROR_FILE_NOT_FOUND) |
| 333 | { |
| 334 | if (!MoveFileEx(tempFile.c_str(), m_ConfigPath.c_str(), MOVEFILE_WRITE_THROUGH)) |
| 335 | { |
| 336 | LastErrorHandle(spdlog::level::err, L"Failed to move temporary configuration file"); |
| 337 | } |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | HresultHandle(HRESULT_FROM_WIN32(lastErr), spdlog::level::err, L"Failed to replace configuration file"); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | ErrnoTHandle(err, spdlog::level::err, L"Failed to save configuration!"); |
| 348 | } |
| 349 | } |
no test coverage detected