From be3e77d721a8bfb74cd2dcea82756744ac5d84fd Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 20 Feb 2021 08:41:47 -0500 Subject: [PATCH] Cleanup CLI includes across all components * Remove unused include files * Move includes out of widely shared headers (reduced rebuild time) * Consolidate code for Analyze command --- src/cli/Add.cpp | 10 ++-------- src/cli/Add.h | 1 - src/cli/AddGroup.cpp | 7 +------ src/cli/Analyze.cpp | 40 +++++++++++++++++-------------------- src/cli/Analyze.h | 3 --- src/cli/Clip.cpp | 12 +++-------- src/cli/Close.cpp | 7 ------- src/cli/Command.cpp | 11 +++------- src/cli/Command.h | 1 - src/cli/Create.cpp | 17 ++++------------ src/cli/DatabaseCommand.cpp | 1 - src/cli/DatabaseCommand.h | 4 ---- src/cli/Diceware.cpp | 4 ---- src/cli/Edit.cpp | 12 +++-------- src/cli/Estimate.cpp | 14 ++----------- src/cli/Exit.cpp | 4 ---- src/cli/Export.cpp | 8 ++------ src/cli/Generate.cpp | 7 ++----- src/cli/Help.cpp | 2 -- src/cli/Import.cpp | 18 ++++------------- src/cli/Info.cpp | 6 +----- src/cli/List.cpp | 8 +------- src/cli/Locate.cpp | 10 ++-------- src/cli/Merge.cpp | 7 ++----- src/cli/Move.cpp | 8 ++------ src/cli/Open.cpp | 6 ------ src/cli/Remove.cpp | 10 ++-------- src/cli/RemoveGroup.cpp | 10 ++-------- src/cli/Show.cpp | 9 --------- src/cli/Utils.cpp | 11 +++++++--- src/cli/Utils.h | 16 +++++++-------- src/cli/keepassxc-cli.cpp | 19 ++++++------------ tests/TestCli.cpp | 8 +------- 33 files changed, 79 insertions(+), 232 deletions(-) diff --git a/src/cli/Add.cpp b/src/cli/Add.cpp index bb3b87d7b..2f4f14d69 100644 --- a/src/cli/Add.cpp +++ b/src/cli/Add.cpp @@ -15,18 +15,12 @@ * along with this program. If not, see . */ -#include -#include - #include "Add.h" -#include "cli/Generate.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "Generate.h" +#include "Utils.h" #include "core/Entry.h" #include "core/Group.h" -#include "core/PasswordGenerator.h" const QCommandLineOption Add::UsernameOption = QCommandLineOption(QStringList() << "u" << "username", diff --git a/src/cli/Add.h b/src/cli/Add.h index 8c5e5ab5b..e36ec5eb3 100644 --- a/src/cli/Add.h +++ b/src/cli/Add.h @@ -32,7 +32,6 @@ public: static const QCommandLineOption NotesOption; static const QCommandLineOption PasswordPromptOption; static const QCommandLineOption GenerateOption; - static const QCommandLineOption PasswordLengthOption; }; #endif // KEEPASSXC_ADD_H diff --git a/src/cli/AddGroup.cpp b/src/cli/AddGroup.cpp index be6bfbf32..11c57f10c 100644 --- a/src/cli/AddGroup.cpp +++ b/src/cli/AddGroup.cpp @@ -15,14 +15,9 @@ * along with this program. If not, see . */ -#include -#include - #include "AddGroup.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "Utils.h" #include "core/Entry.h" #include "core/Group.h" diff --git a/src/cli/Analyze.cpp b/src/cli/Analyze.cpp index 51f8dea73..4498e1979 100644 --- a/src/cli/Analyze.cpp +++ b/src/cli/Analyze.cpp @@ -16,16 +16,14 @@ */ #include "Analyze.h" -#include "cli/Utils.h" + +#include "Utils.h" +#include "core/Group.h" #include "core/HibpOffline.h" +#include "core/Tools.h" #include #include -#include - -#include "cli/TextStream.h" -#include "core/Group.h" -#include "core/Tools.h" const QCommandLineOption Analyze::HIBPDatabaseOption = QCommandLineOption( {"H", "hibp"}, @@ -84,23 +82,21 @@ int Analyze::executeWithDatabase(QSharedPointer database, QSharedPoint } } - for (auto& finding : findings) { - printHibpFinding(finding.first, finding.second, out); + for (const auto& finding : findings) { + const auto entry = finding.first; + auto count = finding.second; + + QString path = entry->title(); + for (auto g = entry->group(); g && g != g->database()->rootGroup(); g = g->parentGroup()) { + path.prepend("/").prepend(g->name()); + } + + if (count > 0) { + out << QObject::tr("Password for '%1' has been leaked %2 time(s)!", "", count).arg(path).arg(count) << endl; + } else { + out << QObject::tr("Password for '%1' has been leaked!", "", count).arg(path) << endl; + } } return EXIT_SUCCESS; } - -void Analyze::printHibpFinding(const Entry* entry, int count, QTextStream& out) -{ - QString path = entry->title(); - for (auto g = entry->group(); g && g != g->database()->rootGroup(); g = g->parentGroup()) { - path.prepend("/").prepend(g->name()); - } - - if (count > 0) { - out << QObject::tr("Password for '%1' has been leaked %2 time(s)!", "", count).arg(path).arg(count) << endl; - } else { - out << QObject::tr("Password for '%1' has been leaked!", "", count).arg(path) << endl; - } -} diff --git a/src/cli/Analyze.h b/src/cli/Analyze.h index a616093a4..dec6b914f 100644 --- a/src/cli/Analyze.h +++ b/src/cli/Analyze.h @@ -28,9 +28,6 @@ public: static const QCommandLineOption HIBPDatabaseOption; static const QCommandLineOption OkonOption; - -private: - void printHibpFinding(const Entry* entry, int count, QTextStream& out); }; #endif // KEEPASSXC_HIBP_H diff --git a/src/cli/Clip.cpp b/src/cli/Clip.cpp index ac8e5b3fd..1096d4ff6 100644 --- a/src/cli/Clip.cpp +++ b/src/cli/Clip.cpp @@ -15,18 +15,12 @@ * along with this program. If not, see . */ -#include -#include -#include - #include "Clip.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "Utils.h" #include "core/Entry.h" -#include "core/Global.h" #include "core/Group.h" +#include "core/Tools.h" const QCommandLineOption Clip::AttributeOption = QCommandLineOption( QStringList() << "a" @@ -155,7 +149,7 @@ int Clip::executeWithDatabase(QSharedPointer database, QSharedPointer< out << '\r' << QString(lastLine.size(), ' ') << '\r'; lastLine = QObject::tr("Clearing the clipboard in %1 second(s)…", "", timeoutSeconds).arg(timeoutSeconds); out << lastLine << flush; - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + Tools::sleep(1000); --timeoutSeconds; } Utils::clipText(""); diff --git a/src/cli/Close.cpp b/src/cli/Close.cpp index 4ff3bcdad..58d123a86 100644 --- a/src/cli/Close.cpp +++ b/src/cli/Close.cpp @@ -17,13 +17,6 @@ #include "Close.h" -#include -#include - -#include "DatabaseCommand.h" -#include "TextStream.h" -#include "Utils.h" - Close::Close() { name = QString("close"); diff --git a/src/cli/Command.cpp b/src/cli/Command.cpp index 260d9b2b1..82b22cb0e 100644 --- a/src/cli/Command.cpp +++ b/src/cli/Command.cpp @@ -15,13 +15,6 @@ * along with this program. If not, see . */ -#include -#include -#include - -#include -#include - #include "Command.h" #include "Add.h" @@ -47,9 +40,11 @@ #include "Remove.h" #include "RemoveGroup.h" #include "Show.h" -#include "TextStream.h" #include "Utils.h" +#include +#include + const QCommandLineOption Command::HelpOption = QCommandLineOption(QStringList() #ifdef Q_OS_WIN << QStringLiteral("?") diff --git a/src/cli/Command.h b/src/cli/Command.h index 4381bf187..c296f6fd6 100644 --- a/src/cli/Command.h +++ b/src/cli/Command.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include diff --git a/src/cli/Create.cpp b/src/cli/Create.cpp index e2bbfac2e..c253312d1 100644 --- a/src/cli/Create.cpp +++ b/src/cli/Create.cpp @@ -15,21 +15,12 @@ * along with this program. If not, see . */ -#include -#include +#include "Create.h" + +#include "Utils.h" +#include "keys/FileKey.h" #include -#include -#include - -#include "Create.h" -#include "Utils.h" - -#include "core/Database.h" - -#include "keys/CompositeKey.h" -#include "keys/FileKey.h" -#include "keys/Key.h" const QCommandLineOption Create::DecryptionTimeOption = QCommandLineOption(QStringList() << "t" diff --git a/src/cli/DatabaseCommand.cpp b/src/cli/DatabaseCommand.cpp index 08104c120..7456f418a 100644 --- a/src/cli/DatabaseCommand.cpp +++ b/src/cli/DatabaseCommand.cpp @@ -16,7 +16,6 @@ */ #include "DatabaseCommand.h" - #include "Utils.h" DatabaseCommand::DatabaseCommand() diff --git a/src/cli/DatabaseCommand.h b/src/cli/DatabaseCommand.h index 61847ffa2..7e8818e55 100644 --- a/src/cli/DatabaseCommand.h +++ b/src/cli/DatabaseCommand.h @@ -18,11 +18,7 @@ #ifndef KEEPASSXC_DATABASECOMMAND_H #define KEEPASSXC_DATABASECOMMAND_H -#include - #include "Command.h" -#include "Utils.h" -#include "core/Database.h" class DatabaseCommand : public Command { diff --git a/src/cli/Diceware.cpp b/src/cli/Diceware.cpp index d0deb9072..cd73bed3d 100644 --- a/src/cli/Diceware.cpp +++ b/src/cli/Diceware.cpp @@ -15,13 +15,9 @@ * along with this program. If not, see . */ -#include -#include - #include "Diceware.h" #include "Utils.h" -#include "cli/TextStream.h" #include "core/PassphraseGenerator.h" const QCommandLineOption Diceware::WordCountOption = diff --git a/src/cli/Edit.cpp b/src/cli/Edit.cpp index 4dcda4fd5..d4564f8f0 100644 --- a/src/cli/Edit.cpp +++ b/src/cli/Edit.cpp @@ -15,19 +15,13 @@ * along with this program. If not, see . */ -#include -#include - #include "Edit.h" -#include "cli/Add.h" -#include "cli/Generate.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "Add.h" +#include "Generate.h" +#include "Utils.h" #include "core/Entry.h" #include "core/Group.h" -#include "core/PasswordGenerator.h" const QCommandLineOption Edit::TitleOption = QCommandLineOption(QStringList() << "t" << "title", diff --git a/src/cli/Estimate.cpp b/src/cli/Estimate.cpp index aca3118b4..f97c8d1ee 100644 --- a/src/cli/Estimate.cpp +++ b/src/cli/Estimate.cpp @@ -16,21 +16,11 @@ */ #include "Estimate.h" -#include "cli/Utils.h" -#include "cli/TextStream.h" +#include "Utils.h" #include "core/PasswordHealth.h" -#include -#include -#include -#include -/* For pre-compiled headers under windows */ -#ifdef _WIN32 -#ifndef __MINGW32__ -#include "stdafx.h" -#endif -#endif +#include const QCommandLineOption Estimate::AdvancedOption = QCommandLineOption(QStringList() << "a" diff --git a/src/cli/Exit.cpp b/src/cli/Exit.cpp index 768088e4d..832542036 100644 --- a/src/cli/Exit.cpp +++ b/src/cli/Exit.cpp @@ -17,10 +17,6 @@ #include "Exit.h" -#include -#include -#include - Exit::Exit(const QString& name) { this->name = name; diff --git a/src/cli/Export.cpp b/src/cli/Export.cpp index 930324daa..787781174 100644 --- a/src/cli/Export.cpp +++ b/src/cli/Export.cpp @@ -15,14 +15,10 @@ * along with this program. If not, see . */ -#include -#include - #include "Export.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "TextStream.h" +#include "Utils.h" #include "format/CsvExporter.h" const QCommandLineOption Export::FormatOption = QCommandLineOption( diff --git a/src/cli/Generate.cpp b/src/cli/Generate.cpp index c4761e6f2..fef828f65 100644 --- a/src/cli/Generate.cpp +++ b/src/cli/Generate.cpp @@ -15,13 +15,10 @@ * along with this program. If not, see . */ -#include -#include - #include "Generate.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" +#include "TextStream.h" +#include "Utils.h" const QCommandLineOption Generate::PasswordLengthOption = QCommandLineOption(QStringList() << "L" diff --git a/src/cli/Help.cpp b/src/cli/Help.cpp index 07a3f085f..6870b62a7 100644 --- a/src/cli/Help.cpp +++ b/src/cli/Help.cpp @@ -17,8 +17,6 @@ #include "Help.h" -#include "Command.h" -#include "TextStream.h" #include "Utils.h" Help::Help() diff --git a/src/cli/Import.cpp b/src/cli/Import.cpp index 12afb3d26..104a9b53d 100644 --- a/src/cli/Import.cpp +++ b/src/cli/Import.cpp @@ -15,22 +15,12 @@ * along with this program. If not, see . */ -#include -#include - -#include -#include -#include - -#include "Create.h" #include "Import.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" -#include "keys/CompositeKey.h" -#include "keys/FileKey.h" -#include "keys/Key.h" +#include "Create.h" +#include "Utils.h" + +#include /** * Create a database file from an XML export of another database. diff --git a/src/cli/Info.cpp b/src/cli/Info.cpp index 800996d64..5412dd0e5 100644 --- a/src/cli/Info.cpp +++ b/src/cli/Info.cpp @@ -14,16 +14,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include -#include #include "Info.h" -#include "Utils.h" -#include "core/Database.h" +#include "Utils.h" #include "core/Global.h" #include "core/Metadata.h" -#include "format/KeePass2.h" Info::Info() { diff --git a/src/cli/List.cpp b/src/cli/List.cpp index ba00a7162..8959e5f1c 100644 --- a/src/cli/List.cpp +++ b/src/cli/List.cpp @@ -15,15 +15,9 @@ * along with this program. If not, see . */ -#include -#include - #include "List.h" -#include "cli/Utils.h" -#include "cli/TextStream.h" -#include "core/Database.h" -#include "core/Entry.h" +#include "Utils.h" #include "core/Group.h" const QCommandLineOption List::RecursiveOption = diff --git a/src/cli/Locate.cpp b/src/cli/Locate.cpp index a7a1e06f2..ed1a39b70 100644 --- a/src/cli/Locate.cpp +++ b/src/cli/Locate.cpp @@ -15,18 +15,12 @@ * along with this program. If not, see . */ -#include -#include - #include "Locate.h" #include -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" -#include "core/Entry.h" -#include "core/Global.h" +#include "TextStream.h" +#include "Utils.h" #include "core/Group.h" Locate::Locate() diff --git a/src/cli/Merge.cpp b/src/cli/Merge.cpp index ce7ecc558..50f58f4cd 100644 --- a/src/cli/Merge.cpp +++ b/src/cli/Merge.cpp @@ -15,13 +15,10 @@ * along with this program. If not, see . */ -#include - #include "Merge.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "TextStream.h" +#include "Utils.h" #include "core/Merger.h" const QCommandLineOption Merge::SameCredentialsOption = diff --git a/src/cli/Move.cpp b/src/cli/Move.cpp index 74070372d..960f9aae8 100644 --- a/src/cli/Move.cpp +++ b/src/cli/Move.cpp @@ -15,14 +15,10 @@ * along with this program. If not, see . */ -#include -#include - #include "Move.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" +#include "TextStream.h" +#include "Utils.h" #include "core/Entry.h" #include "core/Group.h" diff --git a/src/cli/Open.cpp b/src/cli/Open.cpp index c4e9a79ad..2f7492fc7 100644 --- a/src/cli/Open.cpp +++ b/src/cli/Open.cpp @@ -17,12 +17,6 @@ #include "Open.h" -#include - -#include "DatabaseCommand.h" -#include "TextStream.h" -#include "Utils.h" - Open::Open() { name = QString("open"); diff --git a/src/cli/Remove.cpp b/src/cli/Remove.cpp index 6a3a87b00..2f5ed2e95 100644 --- a/src/cli/Remove.cpp +++ b/src/cli/Remove.cpp @@ -15,18 +15,12 @@ * along with this program. If not, see . */ -#include -#include - #include "Remove.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" -#include "core/Entry.h" +#include "TextStream.h" +#include "Utils.h" #include "core/Group.h" #include "core/Metadata.h" -#include "core/Tools.h" Remove::Remove() { diff --git a/src/cli/RemoveGroup.cpp b/src/cli/RemoveGroup.cpp index 5ebac16f9..b11b759a4 100644 --- a/src/cli/RemoveGroup.cpp +++ b/src/cli/RemoveGroup.cpp @@ -15,18 +15,12 @@ * along with this program. If not, see . */ -#include -#include - #include "RemoveGroup.h" -#include "cli/TextStream.h" -#include "cli/Utils.h" -#include "core/Database.h" -#include "core/Entry.h" +#include "TextStream.h" +#include "Utils.h" #include "core/Group.h" #include "core/Metadata.h" -#include "core/Tools.h" RemoveGroup::RemoveGroup() { diff --git a/src/cli/Show.cpp b/src/cli/Show.cpp index 02ec4677e..979877161 100644 --- a/src/cli/Show.cpp +++ b/src/cli/Show.cpp @@ -17,18 +17,9 @@ #include "Show.h" -#include -#include - #include "Utils.h" -#include "cli/TextStream.h" -#include "core/Database.h" -#include "core/Entry.h" -#include "core/Global.h" #include "core/Group.h" -#include - const QCommandLineOption Show::TotpOption = QCommandLineOption(QStringList() << "t" << "totp", QObject::tr("Show the entry's current TOTP.")); diff --git a/src/cli/Utils.cpp b/src/cli/Utils.cpp index a41649873..b1c6ca645 100644 --- a/src/cli/Utils.cpp +++ b/src/cli/Utils.cpp @@ -17,6 +17,11 @@ #include "Utils.h" +#include "core/Database.h" +#include "core/EntryAttributes.h" +#include "keys/CompositeKey.h" +#include "keys/FileKey.h" +#include "keys/PasswordKey.h" #ifdef WITH_XC_YUBIKEY #include "keys/YkChallengeResponseKey.h" #endif @@ -30,7 +35,7 @@ #include #include -#include +#include namespace Utils { @@ -91,7 +96,7 @@ namespace Utils } QSharedPointer unlockDatabase(const QString& databaseFilename, - const bool isPasswordProtected, + bool isPasswordProtected, const QString& keyFilename, const QString& yubiKeySlot, bool quiet) @@ -289,7 +294,7 @@ namespace Utils QStringList failedProgramNames; - for (auto prog : clipPrograms) { + for (const auto& prog : clipPrograms) { QScopedPointer clipProcess(new QProcess(nullptr)); // Skip empty parts, otherwise the program may clip the empty string diff --git a/src/cli/Utils.h b/src/cli/Utils.h index 5b3d5ad2c..dedc818f6 100644 --- a/src/cli/Utils.h +++ b/src/cli/Utils.h @@ -18,13 +18,13 @@ #ifndef KEEPASSXC_UTILS_H #define KEEPASSXC_UTILS_H -#include "cli/TextStream.h" -#include "core/Database.h" -#include "core/EntryAttributes.h" -#include "keys/CompositeKey.h" -#include "keys/FileKey.h" -#include "keys/PasswordKey.h" -#include +#include + +class CompositeKey; +class Database; +class EntryAttributes; +class FileKey; +class PasswordKey; namespace Utils { @@ -41,7 +41,7 @@ namespace Utils QSharedPointer getConfirmedPassword(); int clipText(const QString& text); QSharedPointer unlockDatabase(const QString& databaseFilename, - const bool isPasswordProtected = true, + bool isPasswordProtected = true, const QString& keyFilename = {}, const QString& yubiKeySlot = {}, bool quiet = false); diff --git a/src/cli/keepassxc-cli.cpp b/src/cli/keepassxc-cli.cpp index e55766eaa..fb1341611 100644 --- a/src/cli/keepassxc-cli.cpp +++ b/src/cli/keepassxc-cli.cpp @@ -15,20 +15,14 @@ * along with this program. If not, see . */ -#include -#include - #include -#include -#include -#include +#include #include -#include "cli/TextStream.h" -#include - +#include "Command.h" #include "DatabaseCommand.h" #include "Open.h" +#include "TextStream.h" #include "Utils.h" #include "config-keepassx.h" #include "core/Bootstrap.h" @@ -165,10 +159,9 @@ void enterInteractiveMode(const QStringList& arguments) break; } - cmd->currentDatabase = currentDatabase; + cmd->currentDatabase.swap(currentDatabase); cmd->execute(args); - currentDatabase = cmd->currentDatabase; - cmd->currentDatabase.reset(); + currentDatabase.swap(cmd->currentDatabase); } if (currentDatabase) { @@ -179,7 +172,7 @@ void enterInteractiveMode(const QStringList& arguments) int main(int argc, char** argv) { if (!Crypto::init()) { - qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); + qWarning("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); return EXIT_FAILURE; } diff --git a/tests/TestCli.cpp b/tests/TestCli.cpp index fad5b98aa..b01e9576e 100644 --- a/tests/TestCli.cpp +++ b/tests/TestCli.cpp @@ -21,18 +21,17 @@ #include "core/Bootstrap.h" #include "core/Config.h" #include "core/Entry.h" -#include "core/Global.h" #include "core/Group.h" #include "core/Metadata.h" #include "core/Tools.h" #include "crypto/Crypto.h" +#include "keys/FileKey.h" #include "keys/drivers/YubiKey.h" #include "cli/Add.h" #include "cli/AddGroup.h" #include "cli/Analyze.h" #include "cli/Clip.h" -#include "cli/Command.h" #include "cli/Create.h" #include "cli/Diceware.h" #include "cli/Edit.h" @@ -53,16 +52,11 @@ #include "cli/Utils.h" #include -#include -#include #include -#include #include QTEST_MAIN(TestCli) -QSharedPointer globalCurrentDatabase; - void TestCli::initTestCase() { QVERIFY(Crypto::init());