1 | /* |
2 | SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-or-later |
5 | */ |
6 | |
7 | #include "transaction.h" |
8 | #include "enginebase.h" |
9 | #include "enginebase_p.h" |
10 | #include "entry_p.h" |
11 | #include "providerbase_p.h" |
12 | #include "providercore.h" |
13 | #include "providercore_p.h" |
14 | #include "question.h" |
15 | |
16 | #include <KLocalizedString> |
17 | #include <KShell> |
18 | #include <QDir> |
19 | #include <QProcess> |
20 | #include <QTimer> |
21 | #include <QVersionNumber> |
22 | |
23 | #include <knewstuffcore_debug.h> |
24 | |
25 | using namespace KNSCore; |
26 | |
27 | namespace |
28 | { |
29 | std::optional<int> linkIdFromVersions(const QList<DownloadLinkInformationV2Private> &downloadLinksInformationList) |
30 | { |
31 | switch (downloadLinksInformationList.size()) { |
32 | case 0: |
33 | return {}; |
34 | case 1: |
35 | return downloadLinksInformationList.at(i: 0).id; |
36 | } |
37 | |
38 | QMap<QVersionNumber, int> infoByVersion; |
39 | for (const auto &info : downloadLinksInformationList) { |
40 | const auto number = QVersionNumber::fromString(string: info.version); |
41 | if (number.isNull()) { |
42 | qCDebug(KNEWSTUFFCORE) << "Found no valid version number on linkid" << info.id << info.version; |
43 | continue; |
44 | } |
45 | if (infoByVersion.contains(key: number)) { |
46 | qCWarning(KNEWSTUFFCORE) << "Encountered version number" << info.version << "more than once. Ignoring duplicates." << info.distributionType; |
47 | continue; |
48 | } |
49 | infoByVersion[number] = info.id; |
50 | } |
51 | |
52 | if (infoByVersion.isEmpty()) { // found no valid version |
53 | return {}; |
54 | } |
55 | |
56 | return infoByVersion.last(); // map is sorted by keys, highest version is last entry. |
57 | } |
58 | } // namespace |
59 | |
60 | class KNSCore::TransactionPrivate |
61 | { |
62 | public: |
63 | [[nodiscard]] static Transaction *createInstallTransaction(const KNSCore::Entry &_entry, EngineBase *engine, int linkId) |
64 | { |
65 | auto ret = new Transaction(_entry, engine); |
66 | QObject::connect(sender: engine->d->installation, signal: &Installation::signalInstallationError, context: ret, slot: [ret, _entry](const QString &msg, const KNSCore::Entry &entry) { |
67 | if (_entry.uniqueId() == entry.uniqueId()) { |
68 | Q_EMIT ret->signalErrorCode(errorCode: KNSCore::ErrorCode::InstallationError, message: msg, metadata: {}); |
69 | } |
70 | }); |
71 | QTimer::singleShot(interval: 0, receiver: ret, slot: [ret, linkId] { |
72 | ret->d->installLinkId(linkId); |
73 | }); |
74 | return ret; |
75 | } |
76 | |
77 | TransactionPrivate(const KNSCore::Entry &entry, EngineBase *engine, Transaction *q) |
78 | : m_engine(engine) |
79 | , q(q) |
80 | , subject(entry) |
81 | { |
82 | } |
83 | |
84 | void finish() |
85 | { |
86 | m_finished = true; |
87 | Q_EMIT q->finished(); |
88 | q->deleteLater(); |
89 | } |
90 | |
91 | int findLinkIdToInstall(KNSCore::Entry &entry) |
92 | { |
93 | const auto downloadLinksInformationList = entry.d.constData()->mDownloadLinkInformationList; |
94 | const auto optionalLinkId = linkIdFromVersions(downloadLinksInformationList); |
95 | if (optionalLinkId.has_value()) { |
96 | qCDebug(KNEWSTUFFCORE) << "Found linkid by version" << optionalLinkId.value(); |
97 | payloadToIdentify[entry] = QString{}; |
98 | return optionalLinkId.value(); |
99 | } |
100 | |
101 | if (downloadLinksInformationList.size() == 1 || !entry.payload().isEmpty()) { |
102 | // If there is only one downloadable item (which also includes a predefined payload name), then we can fairly safely assume that's |
103 | // what we're wanting to update, meaning we can bypass some of the more expensive operations in downloadLinkLoaded |
104 | qCDebug(KNEWSTUFFCORE) << "Just the one download link, so let's use that" ; |
105 | payloadToIdentify[entry] = QString{}; |
106 | return 1; |
107 | } |
108 | |
109 | qCDebug(KNEWSTUFFCORE) << "Try and identify a download link to use from a total of" << entry.downloadLinkCount(); |
110 | // While this seems silly, the payload gets reset when fetching the new download link information |
111 | payloadToIdentify[entry] = entry.payload(); |
112 | // Drop a fresh list in place so we've got something to work with when we get the links |
113 | payloads[entry] = QStringList{}; |
114 | return 1; |
115 | } |
116 | |
117 | // linkid may be -1 to denote the latest link id |
118 | void installLinkId(int linkId) |
119 | { |
120 | if (subject.downloadLinkCount() == 0 && subject.payload().isEmpty()) { |
121 | // Turns out this happens sometimes, so we should deal with that and spit out an error |
122 | qCDebug(KNEWSTUFFCORE) << "There were no downloadlinks defined in the entry we were just asked to update: " << subject.uniqueId() << "on provider" |
123 | << subject.providerId(); |
124 | Q_EMIT q->signalErrorCode( |
125 | errorCode: KNSCore::ErrorCode::InstallationError, |
126 | i18n("Could not perform an installation of the entry %1 as it does not have any downloadable items defined. Please contact the " |
127 | "author so they can fix this." , |
128 | subject.name()), |
129 | metadata: subject.uniqueId()); |
130 | finish(); |
131 | return; |
132 | } |
133 | |
134 | KNSCore::Entry entry = subject; |
135 | if (entry.status() == KNSCore::Entry::Updateable) { |
136 | entry.setStatus(KNSCore::Entry::Updating); |
137 | } else { |
138 | entry.setStatus(KNSCore::Entry::Installing); |
139 | } |
140 | Q_EMIT q->signalEntryEvent(entry, event: Entry::StatusChangedEvent); |
141 | |
142 | qCDebug(KNEWSTUFFCORE) << "Install " << entry.name() << " from: " << entry.providerId(); |
143 | auto provider = m_engine->d->providerCores.value(key: entry.providerId()); |
144 | if (!provider) { |
145 | return; |
146 | } |
147 | |
148 | QObject::connect(sender: provider->d->base, signal: &ProviderBase::payloadLinkLoaded, context: q, slot: &Transaction::downloadLinkLoaded); |
149 | // If linkId is -1, assume we don't know what to update |
150 | if (linkId == -1) { |
151 | linkId = findLinkIdToInstall(entry); |
152 | } else { |
153 | qCDebug(KNEWSTUFFCORE) << "Link ID already known" << linkId; |
154 | // If there is no payload to identify, we will assume the payload is already known and just use that |
155 | payloadToIdentify[entry] = QString{}; |
156 | } |
157 | |
158 | provider->d->base->loadPayloadLink(entry, linkId); |
159 | |
160 | m_finished = false; |
161 | m_engine->updateStatus(); |
162 | } |
163 | |
164 | EngineBase *const m_engine; |
165 | Transaction *const q; |
166 | bool m_finished = false; |
167 | // Used for updating purposes - we ought to be saving this information, but we also have to deal with old stuff, and so... this will have to do for now |
168 | // TODO KF6: Installed state needs to move onto a per-downloadlink basis rather than per-entry |
169 | QMap<Entry, QStringList> payloads; |
170 | QMap<Entry, QString> payloadToIdentify; |
171 | const Entry subject; |
172 | }; |
173 | |
174 | /** |
175 | * we look for the directory where all the resources got installed. |
176 | * assuming it was extracted into a directory |
177 | */ |
178 | static QDir sharedDir(QStringList dirs, QString rootPath) |
179 | { |
180 | // Ensure that rootPath definitely is a clean path with a slash at the end |
181 | rootPath = QDir::cleanPath(path: rootPath) + QStringLiteral("/" ); |
182 | qCInfo(KNEWSTUFFCORE) << Q_FUNC_INFO << dirs << rootPath; |
183 | while (!dirs.isEmpty()) { |
184 | QString thisDir(dirs.takeLast()); |
185 | if (thisDir.endsWith(QStringLiteral("*" ))) { |
186 | qCInfo(KNEWSTUFFCORE) << "Directory entry" << thisDir |
187 | << "ends in a *, indicating this was installed from an archive - see Installation::archiveEntries" ; |
188 | thisDir.chop(n: 1); |
189 | } |
190 | |
191 | const QString currentPath = QDir::cleanPath(path: thisDir); |
192 | qCInfo(KNEWSTUFFCORE) << "Current path is" << currentPath; |
193 | if (!currentPath.startsWith(s: rootPath)) { |
194 | qCInfo(KNEWSTUFFCORE) << "Current path" << currentPath << "does not start with" << rootPath << "and should be ignored" ; |
195 | continue; |
196 | } |
197 | |
198 | const QFileInfo current(currentPath); |
199 | qCInfo(KNEWSTUFFCORE) << "Current file info is" << current; |
200 | if (!current.isDir()) { |
201 | qCInfo(KNEWSTUFFCORE) << "Current path" << currentPath << "is not a directory, and should be ignored" ; |
202 | continue; |
203 | } |
204 | |
205 | const QDir dir(currentPath); |
206 | if (dir.path() == (rootPath + dir.dirName())) { |
207 | qCDebug(KNEWSTUFFCORE) << "Found directory" << dir; |
208 | return dir; |
209 | } |
210 | } |
211 | qCWarning(KNEWSTUFFCORE) << "Failed to locate any shared installed directory in" << dirs << "and this is almost certainly very bad." ; |
212 | return {}; |
213 | } |
214 | |
215 | static QString getAdoptionCommand(const QString &command, const KNSCore::Entry &entry, Installation *inst) |
216 | { |
217 | auto adoption = command; |
218 | if (adoption.isEmpty()) { |
219 | return {}; |
220 | } |
221 | |
222 | const QLatin1String dirReplace("%d" ); |
223 | if (adoption.contains(s: dirReplace)) { |
224 | QString installPath = sharedDir(dirs: entry.installedFiles(), rootPath: inst->targetInstallationPath()).path(); |
225 | adoption.replace(before: dirReplace, after: KShell::quoteArg(arg: installPath)); |
226 | } |
227 | |
228 | const QLatin1String fileReplace("%f" ); |
229 | if (adoption.contains(s: fileReplace)) { |
230 | if (entry.installedFiles().isEmpty()) { |
231 | qCWarning(KNEWSTUFFCORE) << "no installed files to adopt" ; |
232 | return {}; |
233 | } else if (entry.installedFiles().count() != 1) { |
234 | qCWarning(KNEWSTUFFCORE) << "can only adopt one file, will be using the first" << entry.installedFiles().at(i: 0); |
235 | } |
236 | |
237 | adoption.replace(before: fileReplace, after: KShell::quoteArg(arg: entry.installedFiles().at(i: 0))); |
238 | } |
239 | return adoption; |
240 | } |
241 | |
242 | Transaction::Transaction(const KNSCore::Entry &entry, EngineBase *engine) |
243 | : QObject(engine) |
244 | , d(new TransactionPrivate(entry, engine, this)) |
245 | { |
246 | connect(sender: d->m_engine->d->installation, signal: &Installation::signalEntryChanged, context: this, slot: [this](const KNSCore::Entry &changedEntry) { |
247 | Q_EMIT signalEntryEvent(entry: changedEntry, event: Entry::StatusChangedEvent); |
248 | d->m_engine->d->cache->registerChangedEntry(entry: changedEntry); |
249 | }); |
250 | connect(sender: d->m_engine->d->installation, signal: &Installation::signalInstallationFailed, context: this, slot: [this](const QString &message, const KNSCore::Entry &entry) { |
251 | if (entry == d->subject) { |
252 | Q_EMIT signalErrorCode(errorCode: KNSCore::ErrorCode::InstallationError, message, metadata: {}); |
253 | d->finish(); |
254 | } |
255 | }); |
256 | } |
257 | |
258 | Transaction::~Transaction() = default; |
259 | |
260 | #if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9) |
261 | Transaction *Transaction::install(EngineBase *engine, const KNSCore::Entry &_entry, int _linkId) |
262 | { |
263 | return TransactionPrivate::createInstallTransaction(_entry, engine, linkId: _linkId); |
264 | } |
265 | #endif |
266 | |
267 | Transaction *Transaction::installLatest(EngineBase *engine, const KNSCore::Entry &_entry) |
268 | { |
269 | return TransactionPrivate::createInstallTransaction(_entry, engine, linkId: -1); |
270 | } |
271 | |
272 | Transaction *Transaction::installLinkId(EngineBase *engine, const KNSCore::Entry &_entry, quint8 _linkId) |
273 | { |
274 | return TransactionPrivate::createInstallTransaction(_entry, engine, linkId: _linkId); |
275 | } |
276 | |
277 | void Transaction::downloadLinkLoaded(const KNSCore::Entry &entry) |
278 | { |
279 | if (entry.status() == KNSCore::Entry::Updating) { |
280 | if (d->payloadToIdentify[entry].isEmpty()) { |
281 | // If there's nothing to identify, and we've arrived here, then we know what the payload is |
282 | qCDebug(KNEWSTUFFCORE) << "If there's nothing to identify, and we've arrived here, then we know what the payload is" ; |
283 | d->m_engine->d->installation->install(entry); |
284 | connect(sender: d->m_engine->d->installation, signal: &Installation::signalInstallationFinished, context: this, slot: [this, entry](const KNSCore::Entry &finishedEntry) { |
285 | if (entry.uniqueId() == finishedEntry.uniqueId()) { |
286 | d->finish(); |
287 | } |
288 | }); |
289 | d->payloadToIdentify.remove(key: entry); |
290 | } else if (d->payloads[entry].count() < entry.downloadLinkCount()) { |
291 | // We've got more to get before we can attempt to identify anything, so fetch the next one... |
292 | qCDebug(KNEWSTUFFCORE) << "We've got more to get before we can attempt to identify anything, so fetch the next one..." ; |
293 | QStringList payloads = d->payloads[entry]; |
294 | payloads << entry.payload(); |
295 | d->payloads[entry] = payloads; |
296 | const auto &p = d->m_engine->d->providerCores.value(key: entry.providerId()); |
297 | if (p) { |
298 | // ok, so this should definitely always work, but... safety first, kids! |
299 | p->d->base->loadPayloadLink(entry, linkId: payloads.count()); |
300 | } |
301 | } else { |
302 | // We now have all the links, so let's try and identify the correct one... |
303 | qCDebug(KNEWSTUFFCORE) << "We now have all the links, so let's try and identify the correct one..." ; |
304 | QString identifiedLink; |
305 | const QString payloadToIdentify = d->payloadToIdentify[entry]; |
306 | const QList<Entry::DownloadLinkInformation> downloadLinks = entry.downloadLinkInformationList(); |
307 | const QStringList &payloads = d->payloads[entry]; |
308 | |
309 | if (payloads.contains(str: payloadToIdentify)) { |
310 | // Simplest option, the link hasn't changed at all |
311 | qCDebug(KNEWSTUFFCORE) << "Simplest option, the link hasn't changed at all" ; |
312 | identifiedLink = payloadToIdentify; |
313 | } else { |
314 | // Next simplest option, filename is the same but in a different folder |
315 | qCDebug(KNEWSTUFFCORE) << "Next simplest option, filename is the same but in a different folder" ; |
316 | const QString fileName = payloadToIdentify.split(sep: QChar::fromLatin1(c: '/')).last(); |
317 | for (const QString &payload : payloads) { |
318 | if (payload.endsWith(s: fileName)) { |
319 | identifiedLink = payload; |
320 | break; |
321 | } |
322 | } |
323 | |
324 | // Possibly the payload itself is named differently (by a CDN, for example), but the link identifier is the same... |
325 | qCDebug(KNEWSTUFFCORE) << "Possibly the payload itself is named differently (by a CDN, for example), but the link identifier is the same..." ; |
326 | QStringList payloadNames; |
327 | for (const Entry::DownloadLinkInformation &downloadLink : downloadLinks) { |
328 | qCDebug(KNEWSTUFFCORE) << "Download link" << downloadLink.name << downloadLink.id << downloadLink.size << downloadLink.descriptionLink; |
329 | payloadNames << downloadLink.name; |
330 | if (downloadLink.name == fileName) { |
331 | identifiedLink = payloads[payloadNames.count() - 1]; |
332 | qCDebug(KNEWSTUFFCORE) << "Found a suitable download link for" << fileName << "which should match" << identifiedLink; |
333 | } |
334 | } |
335 | |
336 | if (identifiedLink.isEmpty()) { |
337 | // Least simple option, no match - ask the user to pick (and if we still haven't got one... that's us done, no installation) |
338 | qCDebug(KNEWSTUFFCORE) |
339 | << "Least simple option, no match - ask the user to pick (and if we still haven't got one... that's us done, no installation)" ; |
340 | auto question = std::make_unique<Question>(args: Question::SelectFromListQuestion); |
341 | question->setTitle(i18n("Pick Update Item" )); |
342 | question->setQuestion( |
343 | i18n("Please pick the item from the list below which should be used to apply this update. We were unable to identify which item to " |
344 | "select, based on the original item, which was named %1" , |
345 | fileName)); |
346 | question->setList(payloadNames); |
347 | if (question->ask() == Question::OKResponse) { |
348 | identifiedLink = payloads.value(i: payloadNames.indexOf(str: question->response())); |
349 | } |
350 | } |
351 | } |
352 | if (!identifiedLink.isEmpty()) { |
353 | KNSCore::Entry theEntry(entry); |
354 | theEntry.setPayload(identifiedLink); |
355 | d->m_engine->d->installation->install(entry: theEntry); |
356 | connect(sender: d->m_engine->d->installation, signal: &Installation::signalInstallationFinished, context: this, slot: [this, entry](const KNSCore::Entry &finishedEntry) { |
357 | if (entry.uniqueId() == finishedEntry.uniqueId()) { |
358 | d->finish(); |
359 | } |
360 | }); |
361 | } else { |
362 | qCWarning(KNEWSTUFFCORE) << "We failed to identify a good link for updating" << entry.name() << "and are unable to perform the update" ; |
363 | KNSCore::Entry theEntry(entry); |
364 | theEntry.setStatus(KNSCore::Entry::Updateable); |
365 | Q_EMIT signalEntryEvent(entry: theEntry, event: Entry::StatusChangedEvent); |
366 | Q_EMIT signalErrorCode(errorCode: ErrorCode::InstallationError, |
367 | i18n("We failed to identify a good link for updating %1, and are unable to perform the update" , entry.name()), |
368 | metadata: {entry.uniqueId()}); |
369 | } |
370 | // As the serverside data may change before next time this is called, even in the same session, |
371 | // let's not make assumptions, and just get rid of this |
372 | d->payloads.remove(key: entry); |
373 | d->payloadToIdentify.remove(key: entry); |
374 | d->finish(); |
375 | } |
376 | } else { |
377 | d->m_engine->d->installation->install(entry); |
378 | connect(sender: d->m_engine->d->installation, signal: &Installation::signalInstallationFinished, context: this, slot: [this, entry](const KNSCore::Entry &finishedEntry) { |
379 | if (entry.uniqueId() == finishedEntry.uniqueId()) { |
380 | d->finish(); |
381 | } |
382 | }); |
383 | } |
384 | } |
385 | |
386 | Transaction *Transaction::uninstall(EngineBase *engine, const KNSCore::Entry &_entry) |
387 | { |
388 | auto ret = new Transaction(_entry, engine); |
389 | const KNSCore::Entry::List list = ret->d->m_engine->d->cache->registryForProvider(providerId: _entry.providerId()); |
390 | // we have to use the cached entry here, not the entry from the provider |
391 | // since that does not contain the list of installed files |
392 | KNSCore::Entry actualEntryForUninstall; |
393 | for (const KNSCore::Entry &eInt : list) { |
394 | if (eInt.uniqueId() == _entry.uniqueId()) { |
395 | actualEntryForUninstall = eInt; |
396 | break; |
397 | } |
398 | } |
399 | if (!actualEntryForUninstall.isValid()) { |
400 | qCDebug(KNEWSTUFFCORE) << "could not find a cached entry with following id:" << _entry.uniqueId() << " -> using the non-cached version" ; |
401 | actualEntryForUninstall = _entry; |
402 | } |
403 | |
404 | QTimer::singleShot(interval: 0, receiver: ret, slot: [actualEntryForUninstall, _entry, ret] { |
405 | KNSCore::Entry entry = _entry; |
406 | entry.setStatus(KNSCore::Entry::Installing); |
407 | |
408 | Entry actualEntryForUninstall2 = actualEntryForUninstall; |
409 | actualEntryForUninstall2.setStatus(KNSCore::Entry::Installing); |
410 | Q_EMIT ret->signalEntryEvent(entry, event: Entry::StatusChangedEvent); |
411 | |
412 | // We connect to/forward the relevant signals |
413 | qCDebug(KNEWSTUFFCORE) << "about to uninstall entry " << entry.uniqueId(); |
414 | ret->d->m_engine->d->installation->uninstall(entry: actualEntryForUninstall2); |
415 | |
416 | // FIXME: signalEntryEvent to uninstalled already happened in installation.cpp:584 |
417 | // Update the correct entry |
418 | entry.setStatus(actualEntryForUninstall2.status()); |
419 | Q_EMIT ret->signalEntryEvent(entry, event: Entry::StatusChangedEvent); |
420 | |
421 | ret->d->finish(); |
422 | }); |
423 | |
424 | return ret; |
425 | } |
426 | |
427 | Transaction *Transaction::adopt(EngineBase *engine, const Entry &entry) |
428 | { |
429 | if (!engine->hasAdoptionCommand()) { |
430 | qCWarning(KNEWSTUFFCORE) << "no adoption command specified" ; |
431 | return nullptr; |
432 | } |
433 | |
434 | auto ret = new Transaction(entry, engine); |
435 | const QString command = getAdoptionCommand(command: engine->d->adoptionCommand, entry, inst: engine->d->installation); |
436 | |
437 | QTimer::singleShot(interval: 0, receiver: ret, slot: [command, entry, ret] { |
438 | QStringList split = KShell::splitArgs(cmd: command); |
439 | QProcess *process = new QProcess(ret); |
440 | process->setProgram(split.takeFirst()); |
441 | process->setArguments(split); |
442 | |
443 | QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
444 | // The debug output is too talkative to be useful |
445 | env.insert(QStringLiteral("QT_LOGGING_RULES" ), QStringLiteral("*.debug=false" )); |
446 | process->setProcessEnvironment(env); |
447 | |
448 | process->start(); |
449 | |
450 | connect(sender: process, signal: &QProcess::finished, context: ret, slot: [ret, process, entry, command](int exitCode) { |
451 | if (exitCode == 0) { |
452 | Q_EMIT ret->signalEntryEvent(entry, event: Entry::EntryEvent::AdoptedEvent); |
453 | |
454 | // Handle error output as warnings if the process hasn't crashed |
455 | const QString stdErr = QString::fromLocal8Bit(ba: process->readAllStandardError()); |
456 | if (!stdErr.isEmpty()) { |
457 | Q_EMIT ret->signalMessage(message: stdErr); |
458 | } |
459 | } else { |
460 | const QString errorMsg = i18n("Failed to adopt '%1'\n%2" , entry.name(), QString::fromLocal8Bit(process->readAllStandardError())); |
461 | Q_EMIT ret->signalErrorCode(errorCode: KNSCore::ErrorCode::AdoptionError, message: errorMsg, metadata: QVariantList{command}); |
462 | } |
463 | ret->d->finish(); |
464 | }); |
465 | }); |
466 | return ret; |
467 | } |
468 | |
469 | bool Transaction::isFinished() const |
470 | { |
471 | return d->m_finished; |
472 | } |
473 | |
474 | #include "moc_transaction.cpp" |
475 | |