From 791b796c234176646f43ce61da98fb1ec92e54f0 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sun, 7 Apr 2019 09:21:58 -0400 Subject: [PATCH] Additional layer of protection for unsafe saves * Attempt to restore database, if that fails retain the temporary file and tell the user where it is located --- src/core/Database.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 8c1f22138..ee888327b 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -251,11 +251,15 @@ bool Database::save(const QString& filePath, QString* error, bool atomic, bool b tempFile.setAutoRemove(false); setFilePath(filePath); return true; - } else { - // restore the database from the backup - if (backup) { - restoreDatabase(filePath); + } else if (!backup || !restoreDatabase(filePath)) { + // Failed to copy new database in place, and + // failed to restore from backup or backups disabled + tempFile.setAutoRemove(false); + if (error) { + *error = tr("%1\nBackup database located at %2").arg(tempFile.errorString(), tempFile.fileName()); } + markAsModified(); + return false; } } @@ -338,8 +342,12 @@ bool Database::restoreDatabase(const QString& filePath) auto match = re.match(filePath); auto backupFilePath = match.captured(1) + ".old" + match.captured(2); - QFile::remove(filePath); - return QFile::copy(backupFilePath, filePath); + // Only try to restore if the backup file actually exists + if (QFile::exists(backupFilePath)) { + QFile::remove(filePath); + return QFile::copy(backupFilePath, filePath); + } + return false; } bool Database::isReadOnly() const