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()