mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-04-06 22:17:39 +03:00
Correct behaviors when saving database fails
* Mark database dirty if saving fails * Restore database file from backup if unsafe save fails between deleting database file and copying temporary file into place * Improve error message display for opening and saving database files * Do not automatically retry saving after failure. This prevents deletion of the backup database file and improves user awareness of issues.
This commit is contained in:
parent
ec82931573
commit
3b0b5d85e9
7 changed files with 50 additions and 12 deletions
|
@ -222,6 +222,7 @@ bool Database::save(const QString& filePath, QString* error, bool atomic, bool b
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
*error = saveFile.errorString();
|
||||
}
|
||||
|
@ -246,18 +247,25 @@ bool Database::save(const QString& filePath, QString* error, bool atomic, bool b
|
|||
// due to an undocumented difference in how the function handles
|
||||
// errors. This prevents errors when saving across file systems.
|
||||
if (tempFile.QFile::rename(filePath)) {
|
||||
// successfully saved database file
|
||||
// successfully saved the database
|
||||
tempFile.setAutoRemove(false);
|
||||
setFilePath(filePath);
|
||||
return true;
|
||||
} else {
|
||||
// restore the database from the backup
|
||||
if (backup) {
|
||||
restoreDatabase(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
*error = tempFile.errorString();
|
||||
}
|
||||
}
|
||||
|
||||
// Saving failed
|
||||
markAsModified();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -316,6 +324,24 @@ bool Database::backupDatabase(const QString& filePath)
|
|||
return QFile::copy(filePath, backupFilePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the database file from the backup file with
|
||||
* name <filename>.old.<extension> to filePath. This will
|
||||
* overwrite the existing file!
|
||||
*
|
||||
* @param filePath Path to the file to restore
|
||||
* @return true on success
|
||||
*/
|
||||
bool Database::restoreDatabase(const QString& filePath)
|
||||
{
|
||||
static auto re = QRegularExpression("^(.*?)(\\.[^.]+)?$");
|
||||
|
||||
auto match = re.match(filePath);
|
||||
auto backupFilePath = match.captured(1) + ".old" + match.captured(2);
|
||||
QFile::remove(filePath);
|
||||
return QFile::copy(backupFilePath, filePath);
|
||||
}
|
||||
|
||||
bool Database::isReadOnly() const
|
||||
{
|
||||
return m_data.isReadOnly;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue