mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-02-17 13:02:49 -05:00
Fix lock file handling in saveDatabaseAs().
This commit is contained in:
parent
1226d1dbd5
commit
df5da2fcef
@ -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 (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;
|
||||
}
|
||||
else {
|
||||
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());
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user