- config_updater.{h,cpp}: single implementation — config_update_in_lines
for batch updates + config_update_option for file-level. REPLACE/APPEND
modes, section auto-creation
- node_config.cpp: generateIdentity() now derives node_id via
SHA256(pubkey) (like sc_derive_node_id_from_pubkey) instead of random.
save() uses config_update_in_lines for line-level update when file
exists, saveFull() only for new file and addServer/addClient.
Removed duplicate findSectionBounds/updateOptionInSection/configUpdateOption
- test_config_updater.cpp: Q_ASSERT replaced with VERIFY macro — Q_ASSERT
in Release mode does not evaluate the expression, causing false positives.
Test now works in both Debug and Release
88 lines
2.8 KiB
C++
88 lines
2.8 KiB
C++
// config_updater.cpp — Universal INI config file updater (like insert_or_replace_option in utun)
|
|
#include "config_updater.h"
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
|
|
static std::pair<int,int> find_section_bounds(const QStringList& lines, const QString& section) {
|
|
QString tag = "[" + section + "]";
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
if (lines[i].trimmed() == tag) {
|
|
int se = lines.size();
|
|
for (int j = i + 1; j < lines.size(); j++) {
|
|
QString t = lines[j].trimmed();
|
|
if (t.startsWith('[') && t.endsWith(']')) { se = j; break; }
|
|
}
|
|
return {i, se};
|
|
}
|
|
}
|
|
return {-1, -1};
|
|
}
|
|
|
|
bool config_update_in_lines(QStringList& lines, const QString& section,
|
|
const QString& key, const QString& value, bool replace) {
|
|
auto bounds = find_section_bounds(lines, section);
|
|
if (bounds.first < 0) {
|
|
if (!lines.isEmpty() && !lines.last().trimmed().isEmpty())
|
|
lines.append("");
|
|
lines.append("[" + section + "]");
|
|
lines.append(key + "=" + value);
|
|
return true;
|
|
}
|
|
|
|
int si = bounds.first, se = bounds.second;
|
|
int ki = -1;
|
|
for (int i = si + 1; i < se; i++) {
|
|
int eq = lines[i].indexOf('=');
|
|
if (eq > 0 && lines[i].left(eq).trimmed() == key) { ki = i; break; }
|
|
}
|
|
|
|
QString newLine = key + "=" + value;
|
|
|
|
if (replace) {
|
|
if (ki >= 0)
|
|
lines[ki] = newLine;
|
|
else
|
|
lines.insert(si + 1, newLine);
|
|
} else {
|
|
if (ki >= 0) {
|
|
int lastKi = ki;
|
|
for (int i = ki + 1; i < se; i++) {
|
|
int eq = lines[i].indexOf('=');
|
|
if (eq > 0 && lines[i].left(eq).trimmed() == key) lastKi = i;
|
|
}
|
|
lines.insert(lastKi + 1, newLine);
|
|
} else {
|
|
lines.insert(si + 1, newLine);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool config_update_option(const QString& filePath, const QString& section,
|
|
const QString& param, const QString& value, int flags) {
|
|
QFile f(filePath);
|
|
QStringList lines;
|
|
|
|
if (f.exists()) {
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return false;
|
|
QTextStream in(&f);
|
|
while (!in.atEnd()) {
|
|
QString line = in.readLine(0);
|
|
if (line.endsWith('\r')) line.chop(1);
|
|
lines.append(line);
|
|
}
|
|
f.close();
|
|
}
|
|
|
|
bool replace = (flags != CONFIG_UPDATE_APPEND);
|
|
if (!config_update_in_lines(lines, section, param, value, replace))
|
|
return false;
|
|
|
|
if (!f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) return false;
|
|
QTextStream out(&f);
|
|
for (int i = 0; i < lines.size(); i++)
|
|
out << lines[i] << '\n';
|
|
f.close();
|
|
return true;
|
|
}
|