Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make ssu.ini versioned, allow adding new keys/update unchanged defaul…
…t values
  • Loading branch information
Bernd Wachter committed Oct 21, 2012
1 parent b1410bd commit 268affd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
2 changes: 2 additions & 0 deletions constants.h
Expand Up @@ -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
69 changes: 63 additions & 6 deletions libssu/ssu.cpp
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion ssu.pro
Expand Up @@ -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

0 comments on commit 268affd

Please sign in to comment.