Skip to content

Commit

Permalink
Additional layer of protection for unsafe saves
Browse files Browse the repository at this point in the history
* Attempt to restore database, if that fails retain the temporary file and tell the user where it is located
  • Loading branch information
droidmonkey committed Apr 7, 2019
1 parent 3b0b5d8 commit 791b796
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/core/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 791b796

Please sign in to comment.