/
ssurepomanager.cpp
253 lines (203 loc) · 8.43 KB
/
ssurepomanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @file ssurepomanager.cpp
* @copyright 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QStringList>
#include <QRegExp>
#include <QDirIterator>
#include "ssudeviceinfo.h"
#include "ssurepomanager.h"
#include "ssucoreconfig.h"
#include "ssulog.h"
#include "ssuvariables.h"
#include "ssu.h"
#include "../constants.h"
SsuRepoManager::SsuRepoManager(): QObject() {
}
void SsuRepoManager::add(QString repo, QString repoUrl){
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
if (repoUrl == ""){
// just enable a repository which has URL in repos.ini
QStringList enabledRepos;
if (ssuSettings->contains("enabled-repos"))
enabledRepos = ssuSettings->value("enabled-repos").toStringList();
enabledRepos.append(repo);
enabledRepos.removeDuplicates();
ssuSettings->setValue("enabled-repos", enabledRepos);
} else
ssuSettings->setValue("repository-urls/" + repo, repoUrl);
ssuSettings->sync();
}
void SsuRepoManager::disable(QString repo){
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
QStringList disabledRepos;
if (ssuSettings->contains("disabled-repos"))
disabledRepos = ssuSettings->value("disabled-repos").toStringList();
disabledRepos.append(repo);
disabledRepos.removeDuplicates();
ssuSettings->setValue("disabled-repos", disabledRepos);
ssuSettings->sync();
}
void SsuRepoManager::enable(QString repo){
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
QStringList disabledRepos;
if (ssuSettings->contains("disabled-repos"))
disabledRepos = ssuSettings->value("disabled-repos").toStringList();
disabledRepos.removeAll(repo);
disabledRepos.removeDuplicates();
ssuSettings->setValue("disabled-repos", disabledRepos);
ssuSettings->sync();
}
void SsuRepoManager::remove(QString repo){
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
if (ssuSettings->contains("repository-urls/" + repo))
ssuSettings->remove("repository-urls/" + repo);
if (ssuSettings->contains("enabled-repos")){
QStringList enabledRepos = ssuSettings->value("enabled-repos").toStringList();
if (enabledRepos.contains(repo)){
enabledRepos.removeAll(repo);
enabledRepos.removeDuplicates();
ssuSettings->setValue("enabled-repos", enabledRepos);
}
}
ssuSettings->sync();
}
void SsuRepoManager::update(){
// - delete all non-ssu managed repositories (missing ssu_ prefix)
// - create list of ssu-repositories for current adaptation
// - go through ssu_* repositories, delete all which are not in the list; write others
SsuDeviceInfo deviceInfo;
QStringList ssuFilters;
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
int deviceMode = ssuSettings->value("deviceMode").toInt();
SsuLog *ssuLog = SsuLog::instance();
// if device is misconfigured, always assume release mode
bool rndMode = false;
if ((deviceMode & Ssu::DisableRepoManager) == Ssu::DisableRepoManager){
ssuLog->print(LOG_INFO, "Repo management requested, but not enabled (option 'deviceMode')");
return;
}
if ((deviceMode & Ssu::RndMode) == Ssu::RndMode)
rndMode = true;
// get list of device-specific repositories...
QStringList repos = deviceInfo.repos(rndMode);
// strict mode enabled -> delete all repositories not prefixed by ssu
// assume configuration error if there are no device repos, and don't delete
// anything, even in strict mode
if ((deviceMode & Ssu::LenientMode) != Ssu::LenientMode && !repos.isEmpty()){
QDirIterator it(ZYPP_REPO_PATH, QDir::AllEntries|QDir::NoDot|QDir::NoDotDot);
while (it.hasNext()){
it.next();
if (it.fileName().left(4) != "ssu_"){
ssuLog->print(LOG_INFO, "Strict mode enabled, removing unmanaged repository " + it.fileName());
QFile(it.filePath()).remove();
}
}
}
// ... delete all ssu-managed repositories not valid for this device ...
ssuFilters.append("ssu_*");
QDirIterator it(ZYPP_REPO_PATH, ssuFilters);
while (it.hasNext()){
QString f = it.next();
QStringList parts = it.fileName().split("_");
// repo file structure is ssu_<reponame>_<rnd|release>.repo -> splits to 3 parts
if (parts.count() == 3){
if (!repos.contains(parts.at(1)) ||
parts.at(2) != (rndMode ? "rnd.repo" : "release.repo" ))
QFile(it.filePath()).remove();
} else
QFile(it.filePath()).remove();
}
// ... and create all repositories required for this device
foreach (const QString &repo, repos){
QFile repoFile(QString("%1/ssu_%2_%3.repo")
.arg(ZYPP_REPO_PATH)
.arg(repo)
.arg(rndMode ? "rnd" : "release"));
if (repoFile.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream out(&repoFile);
// TODO, add -rnd or -release if we want to support having rnd and
// release enabled at the same time
out << "[" << repo << "]" << endl
<< "name=" << repo << endl
<< "failovermethod=priority" << endl
<< "type=rpm-md" << endl
<< "gpgcheck=0" << endl
<< "enabled=1" << endl;
if (rndMode)
out << "baseurl=plugin:ssu?rnd&repo=" << repo << endl;
else
out << "baseurl=plugin:ssu?repo=" << repo << endl;
out.flush();
}
}
}
// RND repos have flavour (devel, testing, release), and release (latest, next)
// Release repos only have release (latest, next, version number)
QString SsuRepoManager::url(QString repoName, bool rndRepo,
QHash<QString, QString> repoParameters,
QHash<QString, QString> parametersOverride){
QString r;
QStringList configSections;
SsuVariables var;
SsuLog *ssuLog = SsuLog::instance();
SsuCoreConfig *settings = SsuCoreConfig::instance();
QSettings *repoSettings = new QSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat);
SsuDeviceInfo deviceInfo;
// fill in all arbitrary variables from ssu.ini
var.resolveSection(settings, "repository-url-variables", &repoParameters);
// add/overwrite some of the variables with sane ones
if (rndRepo){
repoParameters.insert("flavour",
repoSettings->value(
settings->flavour()+"-flavour/flavour-pattern").toString());
repoParameters.insert("flavourPattern",
repoSettings->value(
settings->flavour()+"-flavour/flavour-pattern").toString());
repoParameters.insert("flavourName", settings->flavour());
configSections << settings->flavour()+"-flavour" << "rnd" << "all";
// Make it possible to give any values with the flavour as well.
// These values can be overridden later with domain if needed.
var.resolveSection(repoSettings, settings->flavour()+"-flavour", &repoParameters);
} else {
configSections << "release" << "all";
}
repoParameters.insert("release", settings->release(rndRepo));
if (!repoParameters.contains("debugSplit"))
repoParameters.insert("debugSplit", "packages");
if (!repoParameters.contains("arch"))
repoParameters.insert("arch", settings->value("arch").toString());
// Override device model (and therefore all the family, ... stuff)
if (parametersOverride.contains("model"))
deviceInfo.setDeviceModel(parametersOverride.value("model"));
repoParameters.insert("deviceFamily", deviceInfo.deviceFamily());
repoParameters.insert("deviceModel", deviceInfo.deviceModel());
repoName = deviceInfo.adaptationVariables(repoName, &repoParameters);
// Domain variables
// first read all variables from default-domain
var.resolveSection(repoSettings, "default-domain", &repoParameters);
// then overwrite with domain specific things if that block is available
var.resolveSection(repoSettings, settings->domain()+"-domain", &repoParameters);
// override arbitrary variables, mostly useful for generating mic URLs
QHash<QString, QString>::const_iterator i = parametersOverride.constBegin();
while (i != parametersOverride.constEnd()){
repoParameters.insert(i.key(), i.value());
i++;
}
if (settings->contains("repository-urls/" + repoName))
r = settings->value("repository-urls/" + repoName).toString();
else {
foreach (const QString §ion, configSections){
repoSettings->beginGroup(section);
if (repoSettings->contains(repoName)){
r = repoSettings->value(repoName).toString();
repoSettings->endGroup();
break;
}
repoSettings->endGroup();
}
}
return var.resolveString(r, &repoParameters);
}