From c304d1a78b09de50e78b7dd1218f55a8d7d208f2 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 7 Mar 2025 11:50:58 +0100 Subject: [PATCH] readSettings() read ini-file for binary (e.g. ATBUpdateTool, ATBUpdateDC etc). 1: using optional path 2: under /etc... 3: under /opt... --- common/include/utils_internal.h | 8 +++ common/src/utils_internal.cpp | 111 +++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/common/include/utils_internal.h b/common/include/utils_internal.h index 1f7b3c3..b299743 100644 --- a/common/include/utils_internal.h +++ b/common/include/utils_internal.h @@ -2,6 +2,8 @@ #define UTILS_INTERNAL_H_INCLUDED #include +#include +#include namespace internal { @@ -62,13 +64,19 @@ namespace internal { static constexpr const int PERCENT_UPDATE_DC{80}; static constexpr const int PERCENT_SHOW_FINAL_STATUS{90}; + static constexpr const char *DEFAULT_INI_DIR{"/etc/tools/atbupdate/"}; + static constexpr const char *DEFAULT_INSTALL_DIR{"/opt/app/tools/atbupdate/"}; + int read1stLineOfFile(QString fileName); QString customerRepoRoot(); QString customerRepoDir(); + QString customerRepoDcDir(); QString customerRepoDirName(); QString repositoryUrl(); QString branchName(); bool customerRepoExists(); + std::unique_ptr readSettings(QString const &optionalDirName = ""); + std::unique_ptr dcCandidateToInstall(QString const &dcDirectory = ""); } #endif // UTILS_INTERNAL_H_INCLUDED diff --git a/common/src/utils_internal.cpp b/common/src/utils_internal.cpp index 145bd0a..e21d0b1 100644 --- a/common/src/utils_internal.cpp +++ b/common/src/utils_internal.cpp @@ -1,8 +1,12 @@ -#include "utils.h" +#include "utils_internal.h" #include #include #include +#include +#include +#include +#include namespace internal { @@ -35,6 +39,11 @@ QString customerRepoDir() { return !n.isEmpty() ? QDir::cleanPath(r + QDir::separator() + n) : ""; } +QString customerRepoDcDir() { + QString const &r = customerRepoDir(); + return QDir::cleanPath(r + QDir::separator() + "etc/dc/"); +} + bool customerRepoExists() { QString const repoDir{customerRepoDir()}; return !repoDir.isEmpty() ? QDir(repoDir).exists() : false; @@ -52,4 +61,104 @@ QString branchName() { return ""; } +std::unique_ptr readSettings(QString const &optionalDirName) { + std::unique_ptr settings{std::make_unique()}; + + QString const fileName{settings->applicationName() + ".ini"}; + QDir d; + + if (!optionalDirName.isEmpty()) { + d = QDir{optionalDirName}; + if (d.exists()) { // try to find ini-file under optionalDirname + QFileInfo fi{d, optionalDirName}; + if (fi.exists()) { + settings.reset(new QSettings(fi.absoluteFilePath(), QSettings::IniFormat)); + return settings; + } else { + qCritical() << fi.absoluteFilePath() << "not found." + << "Try" << internal::DEFAULT_INI_DIR; + } + } else { + qCritical() << optionalDirName << "not found." + << "Try" << internal::DEFAULT_INSTALL_DIR; + } + } + d = internal::DEFAULT_INI_DIR; + if (d.exists()) { // try to find ini-file under /etc/tools/atbupdate + QFileInfo fi{d, fileName}; + if (fi.exists()) { + settings.reset(new QSettings(fi.absoluteFilePath(), QSettings::IniFormat)); + return settings; + } else { + qCritical() << fi.absoluteFilePath() << "not found." + << "Try" << internal::DEFAULT_INSTALL_DIR; + } + } else { + qCritical() << internal::DEFAULT_INI_DIR << "not found." + << "Try" << internal::DEFAULT_INSTALL_DIR; + } + d = QDir{internal::DEFAULT_INSTALL_DIR}; + if (d.exists()) { // try to find ini-file under /opt/app/tools/atbupdate + QFileInfo fi{d, fileName}; + if (fi.exists()) { + settings.reset(new QSettings(fi.absoluteFilePath(), QSettings::IniFormat)); + return settings; + } else { + qCritical() << fi.absoluteFilePath() << "not found."; + } + } else { + qCritical() << internal::DEFAULT_INSTALL_DIR << "not found."; + } + + return settings; +} + +std::unique_ptr dcCandidateToInstall(QString const &dcDirectory) { + std::unique_ptr dcCandidate{nullptr}; + + QDir dcDir{dcDirectory.isEmpty() ? customerRepoDcDir() : dcDirectory}; + if (dcDir.exists()) { + + QFileInfoList fileInfoList = + dcDir.entryInfoList(QStringList("*.bin"), + QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks); + + QFileInfo dc2cbin{dcDir.absoluteFilePath("dc2c.bin")}; + + + if (dc2cbin.exists()) { + + QCryptographicHash md5gen(QCryptographicHash::Md5); + QByteArray ba_dc2cbin{}; + { + QFile f{dc2cbin.absoluteFilePath()}; + if (f.open(QIODevice::ReadOnly)) { + md5gen.addData(f.readAll()); + ba_dc2cbin = md5gen.result(); + md5gen.reset(); + } + } + + if (ba_dc2cbin.size() > 0) { + QFileInfoList::const_iterator it; + for (it = fileInfoList.cbegin(); it != fileInfoList.cend(); ++it) { + if (it->absoluteFilePath() != dc2cbin.absoluteFilePath()) { + QFile f{it->absoluteFilePath()}; + if (f.open(QIODevice::ReadOnly)) { + md5gen.addData(f.readAll()); + if (ba_dc2cbin == md5gen.result()) { + dcCandidate.reset(new QString(f.fileName())); + break; + } + md5gen.reset(); + } + } + } + } + } + } + + return dcCandidate; +} + } // namespace internal