Additional database file checks in cli/Utils.unlockDatabase

Avoids prompting the user for a password if unlocking is likely to fail
due to some problem with the database file (i.e. not found, not a file,
not readable).

Add unit tests.
This commit is contained in:
James Ring 2019-09-03 05:50:44 -07:00 committed by Jonathan White
parent 344198bc2a
commit 547c246e88
3 changed files with 53 additions and 0 deletions

View file

@ -24,6 +24,7 @@
#include <unistd.h>
#endif
#include <QFileInfo>
#include <QProcess>
#include <QScopedPointer>
@ -108,6 +109,22 @@ namespace Utils
TextStream out(outputDescriptor);
TextStream err(errorDescriptor);
QFileInfo dbFileInfo(databaseFilename);
if (dbFileInfo.canonicalFilePath().isEmpty()) {
err << QObject::tr("Failed to open database file %1: not found").arg(databaseFilename) << endl;
return {};
}
if (!dbFileInfo.isFile()) {
err << QObject::tr("Failed to open database file %1: not a plain file").arg(databaseFilename) << endl;
return {};
}
if (!dbFileInfo.isReadable()) {
err << QObject::tr("Failed to open database file %1: not readable").arg(databaseFilename) << endl;
return {};
}
if (isPasswordProtected) {
out << QObject::tr("Insert password to unlock %1: ").arg(databaseFilename) << flush;
QString line = Utils::getPassword(outputDescriptor);