diff --git a/src/tox_client.cpp b/src/tox_client.cpp index 4dac527..3dc9cfe 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -10,12 +10,14 @@ #include #include +#include +#include #include #include static void eee(std::string& mod) { for (char& c : mod) { - c ^= 0x59; + c ^= 0x37; } } @@ -218,12 +220,29 @@ void ToxClient::saveToxProfile(void) { } eee(_tox_profile_password); } - std::ofstream ofile{_tox_profile_path, std::ios::binary}; - // TODO: improve - for (const auto& ch : data) { - ofile.put(ch); + + std::filesystem::path tmp_path = _tox_profile_path + ".tmp"; + tmp_path.replace_filename("." + tmp_path.filename().generic_u8string()); + + try { + std::ofstream ofile{tmp_path, std::ios::binary}; + ofile.write(reinterpret_cast(data.data()), data.size()); + if (!ofile.good()) { + // TODO: maybe enable fstream exceptions instead? + throw std::runtime_error("write error"); + } + } catch (...) { + std::filesystem::remove(tmp_path); + std::cerr << "TOX saving failed!\n"; + _save_heat = 10.f; + return; } + std::filesystem::rename( + tmp_path, + _tox_profile_path + ); + _tox_profile_dirty = false; _save_heat = 10.f; }