From 7d3fb58cf586e56d881b2592f0b0f07df215575e Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sun, 19 Jul 2015 00:41:35 +0200 Subject: [PATCH] Fix lock file location in saveDatabaseAs(). Previously the directory of the lock file path was empty when saving to a new file. saveFile.open() doesn't create the file and canonicalPath() only works when the file exists. --- src/gui/DatabaseTabWidget.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 3f4cbcf42..8964d98cf 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -329,15 +329,16 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db) QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"), oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)")); if (!fileName.isEmpty()) { - QSaveFile saveFile(fileName); - if (!saveFile.open(QIODevice::WriteOnly)) { - MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" - + saveFile.errorString()); - return false; - } - QFileInfo fileInfo(fileName); - QString lockFileName = QString("%1/.%2.lock").arg(fileInfo.canonicalPath(), fileInfo.fileName()); + QString lockFilePath; + if (fileInfo.exists()) { + // returns empty string when file doesn't exist + lockFilePath = fileInfo.canonicalPath(); + } + else { + lockFilePath = fileInfo.absolutePath(); + } + QString lockFileName = QString("%1/.%2.lock").arg(lockFilePath, fileInfo.fileName()); QScopedPointer lockFile(new QLockFile(lockFileName)); lockFile->setStaleLockTime(0); if (!lockFile->tryLock()) { @@ -361,6 +362,13 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db) } } + QSaveFile saveFile(fileName); + if (!saveFile.open(QIODevice::WriteOnly)) { + MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" + + saveFile.errorString()); + return false; + } + m_writer.writeDatabase(&saveFile, db); if (!saveFile.commit()) { MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"