improve tox file saving, making it resiliant to save file corruptions
Some checks failed
ContinuousDelivery / linux-ubuntu (push) Has been cancelled
ContinuousDelivery / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android-23]) (push) Has been cancelled
ContinuousDelivery / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android-23]) (push) Has been cancelled
ContinuousDelivery / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android-23]) (push) Has been cancelled
ContinuousDelivery / windows (push) Has been cancelled
ContinuousDelivery / windows-asan (push) Has been cancelled
ContinuousDelivery / dumpsyms (push) Has been cancelled
ContinuousDelivery / release (push) Has been cancelled
ContinuousIntegration / linux (push) Has been cancelled
ContinuousIntegration / linux-arm (push) Has been cancelled
ContinuousIntegration / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android-23]) (push) Has been cancelled
ContinuousIntegration / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android-23]) (push) Has been cancelled
ContinuousIntegration / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android-23]) (push) Has been cancelled
ContinuousIntegration / macos (push) Has been cancelled
ContinuousIntegration / windows (push) Has been cancelled

This commit is contained in:
Green Sky
2025-08-01 15:19:44 +02:00
parent bc039d19cb
commit 8fb6f40709

View File

@@ -10,12 +10,14 @@
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <filesystem>
#include <string>
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
static void eee(std::string& mod) { static void eee(std::string& mod) {
for (char& c : mod) { for (char& c : mod) {
c ^= 0x59; c ^= 0x37;
} }
} }
@@ -218,12 +220,29 @@ void ToxClient::saveToxProfile(void) {
} }
eee(_tox_profile_password); eee(_tox_profile_password);
} }
std::ofstream ofile{_tox_profile_path, std::ios::binary};
// TODO: improve std::filesystem::path tmp_path = _tox_profile_path + ".tmp";
for (const auto& ch : data) { tmp_path.replace_filename("." + tmp_path.filename().generic_u8string());
ofile.put(ch);
try {
std::ofstream ofile{tmp_path, std::ios::binary};
ofile.write(reinterpret_cast<const char*>(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; _tox_profile_dirty = false;
_save_heat = 10.f; _save_heat = 10.f;
} }