From 22e853f04c4acd2765e2b3484f91d692df06302e Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Sat, 13 Jun 2020 22:33:57 +0800 Subject: [PATCH] refactor: main.cpp refactor, fixed a crash --- makespec/BUILDVERSION | 2 +- src/Qv2rayApplication.cpp | 147 ++++++++++++++++---------------------- src/main.cpp | 11 ++- 3 files changed, 69 insertions(+), 91 deletions(-) diff --git a/makespec/BUILDVERSION b/makespec/BUILDVERSION index a9842242..e479892d 100644 --- a/makespec/BUILDVERSION +++ b/makespec/BUILDVERSION @@ -1 +1 @@ -5579 +5580 diff --git a/src/Qv2rayApplication.cpp b/src/Qv2rayApplication.cpp index ed201803..d1934960 100644 --- a/src/Qv2rayApplication.cpp +++ b/src/Qv2rayApplication.cpp @@ -168,32 +168,30 @@ namespace Qv2ray bool Qv2rayApplication::CheckSettingsPathAvailability(const QString &_path, bool checkExistingConfig) { auto path = _path; + if (!path.endsWith("/")) - { path.append("/"); - } + // Does not exist. if (!QDir(path).exists()) return false; - // A temp file used to test file permissions in that folder. - QFile testFile(path + ".qv2ray_file_write_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay())); - bool opened = testFile.open(QFile::OpenModeFlag::ReadWrite); + { + // A temp file used to test file permissions in that folder. + QFile testFile(path + ".qv2ray_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay())); + + if (!testFile.open(QFile::OpenModeFlag::ReadWrite)) + { + LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.") + LOG(MODULE_SETTINGS, "---> Cannot create a new file or open a file for writing.") + return false; + } - if (!opened) - { - LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.") - LOG(MODULE_SETTINGS, "---> Cannot create a new file or open a file for writing.") - return false; - } - else - { testFile.write("Qv2ray test file, feel free to remove."); testFile.flush(); testFile.close(); - bool removed = testFile.remove(); - if (!removed) + if (!testFile.remove()) { // This is rare, as we can create a file but failed to remove it. LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.") @@ -202,89 +200,64 @@ namespace Qv2ray } } - if (checkExistingConfig) - { - // Check if an existing config is found. - QFile configFile(path + "Qv2ray.conf"); - - // No such config file. - if (!configFile.exists()) - return false; - - bool opened2 = configFile.open(QIODevice::ReadWrite); - - try - { - if (opened2) - { - // Verify if the config can be loaded. - // Failed to parse if we changed the file structure... - // Original: - // -- auto conf = - // StructFromJsonString(configFile.readAll()); - // - // Verify JSON file format. (only) because this file version may - // not be upgraded and may contain unsupported structure. - auto err = VerifyJsonString(StringFromFile(configFile)); - - if (!err.isEmpty()) - { - LOG(MODULE_INIT, "Json parse returns: " + err) - return false; - } - else - { - // If the file format is valid. - auto conf = JsonFromString(StringFromFile(configFile)); - LOG(MODULE_SETTINGS, - "Path: " + path + " contains a config file, in version " + conf["config_version"].toVariant().toString()) - configFile.close(); - return true; - } - } - else - { - LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!") - return false; - } - } - catch (...) - { - LOG(MODULE_SETTINGS, "Exception raised when checking config: " + configFile.fileName()) - // LOG(INIT, e->what()) - QvMessageBoxWarn(nullptr, tr("Warning"), tr("Qv2ray cannot load the config file from here:") + NEWLINE + configFile.fileName()); - return false; - } - } - else + if (!checkExistingConfig) { + // Just pass the test return true; } + + // Check if an existing config is found. + QFile configFile(path + "Qv2ray.conf"); + + // No such config file. + if (!configFile.exists()) + return false; + + if (!configFile.open(QIODevice::ReadWrite)) + { + LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!") + return false; + } + + const auto err = VerifyJsonString(StringFromFile(configFile)); + if (!err.isEmpty()) + { + LOG(MODULE_INIT, "Json parse returns: " + err) + return false; + } + + // If the file format is valid. + const auto conf = JsonFromString(StringFromFile(configFile)); + LOG(MODULE_SETTINGS, "Found a config file, v=" + conf["config_version"].toString() + " path=" + path) + configFile.close(); + return true; } bool Qv2rayApplication::PreInitilize(int argc, char *argv[]) { - QCoreApplication consoleApp(argc, argv); QString errorMessage; - const auto &args = consoleApp.arguments(); - Qv2rayProcessArgument.path = args.first(); - Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING; - Qv2rayProcessArgument.data = args.join(" "); - switch (ParseCommandLine(&errorMessage)) { - case QUIT: + QCoreApplication coreApp(argc, argv); + const auto &args = coreApp.arguments(); + Qv2rayProcessArgument.path = args.first(); + Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING; + Qv2rayProcessArgument.data = args.join(" "); + switch (ParseCommandLine(&errorMessage)) { - return false; - } - case ERROR: - { - LOG(MODULE_INIT, errorMessage) - return false; - } - case CONTINUE: - { - break; + case QUIT: + { + return false; + } + case ERROR: + { + LOG(MODULE_INIT, errorMessage) + return false; + } + case CONTINUE: + { + break; + } } } // noScaleFactors = disable HiDPI diff --git a/src/main.cpp b/src/main.cpp index 165e673a..d5b9468a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,7 +47,10 @@ int main(int argc, char *argv[]) #endif // // parse the command line before starting as a Qt application - Qv2rayApplication::PreInitilize(argc, argv); + if (!Qv2rayApplication::PreInitilize(argc, argv)) + { + return -1; + } Qv2rayApplication app(argc, argv); if (app.SetupQv2ray()) { @@ -210,8 +213,8 @@ int main(int argc, char *argv[]) { // Initialise Connection Handler PluginHost = new QvPluginHost(); - ConnectionManager = new QvConfigHandler(qvApp); - RouteManager = new RouteHandler(qvApp); + ConnectionManager = new QvConfigHandler(); + RouteManager = new RouteHandler(); #ifdef Q_OS_LINUX qvApp->setFallbackSessionManagementEnabled(false); @@ -234,6 +237,8 @@ int main(int argc, char *argv[]) signal(SIGUSR2, [](int) { ConnectionManager->StopConnection(); }); #endif auto rcode = app.exec(); + delete ConnectionManager; + delete RouteManager; delete PluginHost; LOG(MODULE_INIT, "Quitting normally") return rcode;