From 268affd14ee2275c88ed1467cf2ee262928c46c9 Mon Sep 17 00:00:00 2001 From: Bernd Wachter Date: Sun, 21 Oct 2012 23:25:47 +0300 Subject: [PATCH] Make ssu.ini versioned, allow adding new keys/update unchanged default values --- constants.h | 2 ++ libssu/ssu.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++----- ssu-defaults.ini | 16 +++++++++++ ssu.pro | 2 +- 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 ssu-defaults.ini diff --git a/constants.h b/constants.h index 373916b..082bd20 100644 --- a/constants.h +++ b/constants.h @@ -14,6 +14,8 @@ #define SSU_CONFIGURATION "/etc/ssu/ssu.ini" /// Path to the main SSU configuration file #define SSU_REPO_CONFIGURATION "/usr/share/ssu/repos.ini" +/// Path to the main SSU configuration file +#define SSU_DEFAULT_CONFIGURATION "/usr/share/ssu/ssu-defaults.ini" /// The SSU protocol version used by the ssu client libraries #define SSU_PROTOCOL_VERSION "1" #endif diff --git a/libssu/ssu.cpp b/libssu/ssu.cpp index b9d2f50..ca72a14 100644 --- a/libssu/ssu.cpp +++ b/libssu/ssu.cpp @@ -31,12 +31,69 @@ Ssu::Ssu(): QObject(){ settings = new QSettings(SSU_CONFIGURATION, QSettings::IniFormat); repoSettings = new QSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat); - - bool initialized=settings->value("initialized").toBool(); - if (!initialized){ - // FIXME, there's currently no fallback for missing configuration - settings->setValue("initialized", true); - settings->setValue("flavour", "testing"); + QSettings defaultSettings(SSU_DEFAULT_CONFIGURATION, QSettings::IniFormat); + + int configVersion=0; + int defaultConfigVersion=0; + if (settings->contains("configVersion")) + configVersion = settings->value("configVersion").toInt(); + if (defaultSettings.contains("configVersion")) + defaultConfigVersion = defaultSettings.value("configVersion").toInt(); + + if (configVersion < defaultConfigVersion){ + qDebug() << "Configuration is outdated, updating from " << configVersion + << " to " << defaultConfigVersion; + + for (int i=configVersion+1;i<=defaultConfigVersion;i++){ + QStringList defaultKeys; + QString currentSection = QString("%1/").arg(i); + + qDebug() << "Processing configuration version " << i; + defaultSettings.beginGroup(currentSection); + defaultKeys = defaultSettings.allKeys(); + defaultSettings.endGroup(); + foreach (const QString &key, defaultKeys){ + if (!settings->contains(key)){ + // Add new keys.. + settings->setValue(key, defaultSettings.value(currentSection + key)); + qDebug() << "Adding new key: " << key; + } else { + // ... or update the ones where default values has changed. + QVariant oldValue; + + // check if an old value exists in an older configuration version + for (int j=i-1;j>0;j--){ + if (defaultSettings.contains(QString("%1/").arg(j)+key)){ + oldValue = defaultSettings.value(QString("%1/").arg(j)+key); + break; + } + } + + // skip updating if there is no old value, since we can't check if the + // default value has changed + if (oldValue.isNull()) + continue; + + QVariant newValue = defaultSettings.value(currentSection + key); + if (oldValue == newValue){ + // old and new value match, no need to do anything, apart from beating the + // person who added a useless key + continue; + } else { + // default value has changed, so check if the configuration is still + // using the old default value... + QVariant currentValue = settings->value(key); + // testcase: handles properly default update of thing with changed value in ssu.ini? + if (currentValue == oldValue){ + // ...and update the key if it does + settings->setValue(key, newValue); + qDebug() << "Updating " << key << " from " << currentValue << " to " << newValue; + } + } + } + } + settings->setValue("configVersion", i); + } } #ifdef TARGET_ARCH diff --git a/ssu-defaults.ini b/ssu-defaults.ini new file mode 100644 index 0000000..27019b7 --- /dev/null +++ b/ssu-defaults.ini @@ -0,0 +1,16 @@ +[General] +configVersion=2 + +[1] +flavour=testing +registered=false +rndRelease=latest +release= +adaptation= +ca-certificate= +credentials-url=https://example.com/ssu/device/%1/credentials.xml +register-url=https://example.com/ssu/device/%1/register.xml +credentials-scope=example + +[2] +release=latest diff --git a/ssu.pro b/ssu.pro index 9d99148..d6f4232 100644 --- a/ssu.pro +++ b/ssu.pro @@ -20,7 +20,7 @@ tests.depends = libssu config.files = ssu.ini config.path = /etc/ssu -static_config.files = repos.ini +static_config.files = repos.ini ssu-defaults.ini static_config.path = /usr/share/ssu INSTALLS += config static_config