From c6f3e645f45c2f97634481e3b41428b98aeb637f Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 13 Feb 2026 13:06:19 +0100 Subject: [PATCH] app: save library.json atomically --- src/app.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index b9c771b..fff446d 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -936,10 +937,14 @@ void App::saveLibrary() QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QDir dir(dataPath); if (!dir.exists()) { - dir.mkpath(dataPath); + if (!dir.mkpath(dataPath)) { + qWarning() << "Failed to create data directory:" << dataPath; + return; + } } - QFile file(dataPath + QStringLiteral("/library.json")); + const QString libraryPath = dataPath + QStringLiteral("/library.json"); + QSaveFile file(libraryPath); if (!file.open(QIODevice::WriteOnly)) { qWarning() << "Failed to save library:" << file.errorString(); return; @@ -954,7 +959,16 @@ void App::saveLibrary() } QJsonDocument doc(gamesArray); - file.write(doc.toJson()); + const QByteArray payload = doc.toJson(); + if (file.write(payload) != payload.size()) { + qWarning() << "Failed to save library:" << file.errorString(); + return; + } + + if (!file.commit()) { + qWarning() << "Failed to save library:" << file.errorString(); + return; + } } void App::loadLibrary()