Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Geyer <debfx@fobos.de>2015-07-18 23:45:53 +0300
committerFelix Geyer <debfx@fobos.de>2015-07-18 23:46:45 +0300
commitdf5da2fcef128f35fad0c26a6d692bf581bf203c (patch)
tree021e2e208bf33f5d7dc329a2fe171f0e3dc68fef
parent1226d1dbd59c003eabdcca33417ee295e9a27c94 (diff)
Fix lock file handling in saveDatabaseAs().
-rw-r--r--src/gui/DatabaseTabWidget.cpp65
1 files changed, 43 insertions, 22 deletions
diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp
index 1bf014214..3f4cbcf42 100644
--- a/src/gui/DatabaseTabWidget.cpp
+++ b/src/gui/DatabaseTabWidget.cpp
@@ -329,36 +329,57 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db)
QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"),
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"));
if (!fileName.isEmpty()) {
- bool result = false;
-
QSaveFile saveFile(fileName);
- if (saveFile.open(QIODevice::WriteOnly)) {
- m_writer.writeDatabase(&saveFile, db);
- result = saveFile.commit();
+ if (!saveFile.open(QIODevice::WriteOnly)) {
+ MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ + saveFile.errorString());
+ return false;
}
- if (result) {
- dbStruct.modified = false;
- dbStruct.saveToFilename = true;
- QFileInfo fileInfo(fileName);
- dbStruct.filePath = fileInfo.absoluteFilePath();
- dbStruct.canonicalFilePath = fileInfo.canonicalFilePath();
- dbStruct.fileName = fileInfo.fileName();
- dbStruct.dbWidget->updateFilename(dbStruct.filePath);
- QString lockFileName = QString("%1/.%2.lock")
- .arg(fileInfo.canonicalPath(), fileInfo.fileName());
- dbStruct.lockFile = new QLockFile(lockFileName);
- dbStruct.lockFile->setStaleLockTime(0);
- dbStruct.lockFile->tryLock();
- updateTabName(db);
- updateLastDatabases(dbStruct.filePath);
- return true;
+ QFileInfo fileInfo(fileName);
+ QString lockFileName = QString("%1/.%2.lock").arg(fileInfo.canonicalPath(), fileInfo.fileName());
+ QScopedPointer<QLockFile> lockFile(new QLockFile(lockFileName));
+ lockFile->setStaleLockTime(0);
+ if (!lockFile->tryLock()) {
+ // for now silently ignore if we can't create a lock file
+ // due to lack of permissions
+ if (lockFile->error() != QLockFile::PermissionError) {
+ QMessageBox::StandardButton result = MessageBox::question(this, tr("Save database as"),
+ tr("The database you are trying to save as is locked by another instance of KeePassX.\n"
+ "Do you want to save it anyway?"),
+ QMessageBox::Yes | QMessageBox::No);
+
+ if (result == QMessageBox::No) {
+ return false;
+ }
+ else {
+ // take over the lock file if possible
+ if (lockFile->removeStaleLockFile()) {
+ lockFile->tryLock();
+ }
+ }
+ }
}
- else {
+
+ m_writer.writeDatabase(&saveFile, db);
+ if (!saveFile.commit()) {
MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ saveFile.errorString());
return false;
}
+
+ dbStruct.modified = false;
+ dbStruct.saveToFilename = true;
+ dbStruct.readOnly = false;
+ dbStruct.filePath = fileInfo.absoluteFilePath();
+ dbStruct.canonicalFilePath = fileInfo.canonicalFilePath();
+ dbStruct.fileName = fileInfo.fileName();
+ dbStruct.dbWidget->updateFilename(dbStruct.filePath);
+ delete dbStruct.lockFile;
+ dbStruct.lockFile = lockFile.take();
+ updateTabName(db);
+ updateLastDatabases(dbStruct.filePath);
+ return true;
}
else {
return false;