Set correct case for database file path on Windows

* Fix #7139 - when opening database files from the command line, ensure the correct case is fed to the program to prevent case changes during saves.
* Cleanup old code (checking for .json extension) from when KeePassXC app could act as a proxy.
This commit is contained in:
mckeema 2022-09-10 12:23:30 -04:00 committed by Jonathan White
parent ef6d8f1138
commit dd15db721a
2 changed files with 33 additions and 11 deletions

View File

@ -155,11 +155,12 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath,
const QString& password, const QString& password,
const QString& keyfile) const QString& keyfile)
{ {
QFileInfo fileInfo(filePath); QString cleanFilePath = QDir::toNativeSeparators(filePath);
QFileInfo fileInfo(cleanFilePath);
QString canonicalFilePath = fileInfo.canonicalFilePath(); QString canonicalFilePath = fileInfo.canonicalFilePath();
if (canonicalFilePath.isEmpty()) { if (canonicalFilePath.isEmpty()) {
emit messageGlobal(tr("Failed to open %1. It either does not exist or is not accessible.").arg(filePath), emit messageGlobal(tr("Failed to open %1. It either does not exist or is not accessible.").arg(cleanFilePath),
MessageWidget::Error); MessageWidget::Error);
return; return;
} }
@ -178,10 +179,10 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath,
} }
} }
auto* dbWidget = new DatabaseWidget(QSharedPointer<Database>::create(filePath), this); auto* dbWidget = new DatabaseWidget(QSharedPointer<Database>::create(cleanFilePath), this);
addDatabaseTab(dbWidget, inBackground); addDatabaseTab(dbWidget, inBackground);
dbWidget->performUnlockDatabase(password, keyfile); dbWidget->performUnlockDatabase(password, keyfile);
updateLastDatabases(filePath); updateLastDatabases(cleanFilePath);
} }
/** /**
@ -782,7 +783,7 @@ void DatabaseTabWidget::updateLastDatabases(const QString& filename)
config()->remove(Config::LastDatabases); config()->remove(Config::LastDatabases);
} else { } else {
QStringList lastDatabases = config()->get(Config::LastDatabases).toStringList(); QStringList lastDatabases = config()->get(Config::LastDatabases).toStringList();
lastDatabases.prepend(filename); lastDatabases.prepend(QDir::toNativeSeparators(filename));
lastDatabases.removeDuplicates(); lastDatabases.removeDuplicates();
while (lastDatabases.count() > config()->get(Config::NumberOfRememberedLastDatabases).toInt()) { while (lastDatabases.count() > config()->get(Config::NumberOfRememberedLastDatabases).toInt()) {

View File

@ -44,6 +44,10 @@ Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
#endif #endif
#endif #endif
#ifdef Q_OS_WIN
#include <windows.h>
#endif
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR) QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR)
@ -110,9 +114,29 @@ int main(int argc, char** argv)
Config::createConfigFromFile(parser.value(configOption), parser.value(localConfigOption)); Config::createConfigFromFile(parser.value(configOption), parser.value(localConfigOption));
} }
// Extract file names provided on the command line for opening
QStringList fileNames;
#ifdef Q_OS_WIN
// Get correct case for Windows filenames (fixes #7139)
for (const auto& file : parser.positionalArguments()) {
const auto fileInfo = QFileInfo(file);
WIN32_FIND_DATA findFileData;
HANDLE hFind;
hFind = FindFirstFile(fileInfo.absoluteFilePath().toUtf8(), &findFileData);
if (hFind != INVALID_HANDLE_VALUE) {
fileNames << QString("%1/%2").arg(fileInfo.absolutePath(), QString::fromUtf8(findFileData.cFileName));
FindClose(hFind);
}
}
#else
for (const auto& file : parser.positionalArguments()) {
if (QFile::exists(file)) {
fileNames << QDir::toNativeSeparators(file);
}
}
#endif
// Process single instance and early exit if already running // Process single instance and early exit if already running
// FIXME: this is a *mess* and it is entirely my fault. --wundrweapon
const QStringList fileNames = parser.positionalArguments();
if (app.isAlreadyRunning()) { if (app.isAlreadyRunning()) {
if (parser.isSet(lockOption)) { if (parser.isSet(lockOption)) {
if (app.sendLockToInstance()) { if (app.sendLockToInstance()) {
@ -179,10 +203,7 @@ int main(int argc, char** argv)
out << QObject::tr("Database password: ") << flush; out << QObject::tr("Database password: ") << flush;
password = Utils::getPassword(); password = Utils::getPassword();
} }
mainWindow.openDatabase(filename, password, parser.value(keyfileOption));
if (!filename.isEmpty() && QFile::exists(filename) && !filename.endsWith(".json", Qt::CaseInsensitive)) {
mainWindow.openDatabase(QDir::toNativeSeparators(filename), password, parser.value(keyfileOption));
}
} }
int exitCode = Application::exec(); int exitCode = Application::exec();