read ini-file for binary (e.g. ATBUpdateTool, ATBUpdateDC etc). 1: using optional path 2: under /etc... 3: under /opt...
165 lines
5.2 KiB
C++
165 lines
5.2 KiB
C++
#include "utils_internal.h"
|
|
|
|
#include <QFile>
|
|
#include <QDir>
|
|
#include <QTextStream>
|
|
#include <QSettings>
|
|
#include <QDebug>
|
|
#include <QCryptographicHash>
|
|
#include <QFileInfoList>
|
|
|
|
namespace internal {
|
|
|
|
int read1stLineOfFile(QString fileName) {
|
|
QFile f(fileName);
|
|
if (f.exists()) {
|
|
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
QTextStream in(&f);
|
|
in.setCodec("UTF-8");
|
|
while(!in.atEnd()) {
|
|
return in.readLine().toInt();
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
QString customerRepoRoot() {
|
|
return "/opt/app/tools/atbupdate/";
|
|
}
|
|
|
|
QString customerRepoDirName() {
|
|
int const customerNr = read1stLineOfFile("/mnt/system_data/cust_nr");
|
|
return (customerNr != -1) ? QString("customer_%1").arg(customerNr) : "";
|
|
}
|
|
|
|
QString customerRepoDir() {
|
|
QString const &n = customerRepoDirName();
|
|
QString const &r = customerRepoRoot();
|
|
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;
|
|
}
|
|
|
|
QString repositoryUrl() {
|
|
return "gitea@ptu-config.atb-comm.de:ATB/";
|
|
}
|
|
|
|
QString branchName() {
|
|
int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
|
|
if (zoneNr != -1) {
|
|
return QString("zg1/zone%1").arg(zoneNr);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::unique_ptr<QSettings> readSettings(QString const &optionalDirName) {
|
|
std::unique_ptr<QSettings> settings{std::make_unique<QSettings>()};
|
|
|
|
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<QString> dcCandidateToInstall(QString const &dcDirectory) {
|
|
std::unique_ptr<QString> 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
|