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.
This commit is contained in:
Felix Geyer 2015-07-19 00:41:35 +02:00
parent df5da2fcef
commit 7d3fb58cf5

View File

@ -329,15 +329,16 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db)
QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"), QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"),
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)")); oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"));
if (!fileName.isEmpty()) { 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); 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<QLockFile> lockFile(new QLockFile(lockFileName)); QScopedPointer<QLockFile> lockFile(new QLockFile(lockFileName));
lockFile->setStaleLockTime(0); lockFile->setStaleLockTime(0);
if (!lockFile->tryLock()) { 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); m_writer.writeDatabase(&saveFile, db);
if (!saveFile.commit()) { if (!saveFile.commit()) {
MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"