File indexing completed on 2024-11-17 04:55:39
0001 /* 0002 * SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com> 0003 * SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "FlatpakTransactionThread.h" 0009 #include "FlatpakResource.h" 0010 0011 #include <KLocalizedString> 0012 #include <QDebug> 0013 #include <QDesktopServices> 0014 0015 static int FLATPAK_CLI_UPDATE_FREQUENCY = 150; 0016 0017 gboolean FlatpakTransactionThread::add_new_remote_cb(FlatpakTransaction *object, 0018 gint /*reason*/, 0019 gchar *from_id, 0020 gchar *suggested_remote_name, 0021 gchar *url, 0022 gpointer user_data) 0023 { 0024 auto obj = static_cast<FlatpakTransactionThread *>(user_data); 0025 0026 // TODO ask instead 0027 auto name = QString::fromUtf8(suggested_remote_name); 0028 obj->m_addedRepositories[FlatpakResource::installationPath(flatpak_transaction_get_installation(object))].append(name); 0029 Q_EMIT obj->passiveMessage(i18n("Adding remote '%1' in %2 from %3", name, QString::fromUtf8(url), QString::fromUtf8(from_id))); 0030 return true; 0031 } 0032 0033 void FlatpakTransactionThread::progress_changed_cb(FlatpakTransactionProgress *progress, gpointer user_data) 0034 { 0035 auto obj = static_cast<FlatpakTransactionThread *>(user_data); 0036 0037 g_autolist(GObject) ops = flatpak_transaction_get_operations(obj->m_transaction); 0038 g_autoptr(FlatpakTransactionOperation) op = flatpak_transaction_get_current_operation(obj->m_transaction); 0039 const int idx = g_list_index(ops, op); 0040 obj->setProgress(qMin<int>(99, (100 * idx + flatpak_transaction_progress_get_progress(progress)) / g_list_length(ops))); 0041 0042 #ifdef FLATPAK_VERBOSE_PROGRESS 0043 guint64 start_time = flatpak_transaction_progress_get_start_time(progress); 0044 guint64 elapsed_time = (g_get_monotonic_time() - start_time) / G_USEC_PER_SEC; 0045 if (elapsed_time > 0) { 0046 guint64 transferred = flatpak_transaction_progress_get_bytes_transferred(progress); 0047 obj->setSpeed(transferred / elapsed_time); 0048 } 0049 #endif 0050 } 0051 0052 void FlatpakTransactionThread::new_operation_cb(FlatpakTransaction * /*object*/, 0053 FlatpakTransactionOperation * /*operation*/, 0054 FlatpakTransactionProgress *progress, 0055 gpointer user_data) 0056 { 0057 auto obj = static_cast<FlatpakTransactionThread *>(user_data); 0058 0059 g_signal_connect(progress, "changed", G_CALLBACK(&FlatpakTransactionThread::progress_changed_cb), obj); 0060 flatpak_transaction_progress_set_update_frequency(progress, FLATPAK_CLI_UPDATE_FREQUENCY); 0061 } 0062 0063 void operation_error_cb(FlatpakTransaction * /*object*/, FlatpakTransactionOperation * /*operation*/, GError *error, gint /*details*/, gpointer user_data) 0064 { 0065 auto obj = static_cast<FlatpakTransactionThread *>(user_data); 0066 if (error) { 0067 obj->addErrorMessage(QString::fromUtf8(error->message)); 0068 } 0069 } 0070 0071 gboolean 0072 FlatpakTransactionThread::webflowStart(FlatpakTransaction *transaction, const char *remote, const char *url, GVariant *options, guint id, gpointer user_data) 0073 { 0074 Q_UNUSED(transaction); 0075 Q_UNUSED(options); 0076 0077 QUrl webflowUrl(QString::fromUtf8(url)); 0078 qDebug() << "starting web flow" << webflowUrl << remote << id; 0079 auto obj = static_cast<FlatpakTransactionThread *>(user_data); 0080 obj->m_webflows << id; 0081 Q_EMIT obj->webflowStarted(webflowUrl, id); 0082 return true; 0083 } 0084 0085 void FlatpakTransactionThread::webflowDoneCallback(FlatpakTransaction *transaction, GVariant *options, guint id, gpointer user_data) 0086 { 0087 Q_UNUSED(transaction); 0088 Q_UNUSED(options); 0089 auto obj = static_cast<FlatpakTransactionThread *>(user_data); 0090 obj->m_webflows << id; 0091 Q_EMIT obj->webflowDone(id); 0092 qDebug() << "webflow done" << id; 0093 } 0094 0095 FlatpakTransactionThread::FlatpakTransactionThread(FlatpakResource *app, Transaction::Role role) 0096 : m_result(false) 0097 , m_app(app) 0098 , m_role(role) 0099 { 0100 m_cancellable = g_cancellable_new(); 0101 0102 g_autoptr(GError) localError = nullptr; 0103 m_transaction = flatpak_transaction_new_for_installation(app->installation(), m_cancellable, &localError); 0104 if (localError) { 0105 addErrorMessage(QString::fromUtf8(localError->message)); 0106 qWarning() << "Failed to create transaction" << m_errorMessage; 0107 } else { 0108 g_signal_connect(m_transaction, "add-new-remote", G_CALLBACK(add_new_remote_cb), this); 0109 g_signal_connect(m_transaction, "new-operation", G_CALLBACK(new_operation_cb), this); 0110 g_signal_connect(m_transaction, "operation-error", G_CALLBACK(operation_error_cb), this); 0111 0112 if (qEnvironmentVariableIntValue("DISCOVER_FLATPAK_WEBFLOW")) { 0113 g_signal_connect(m_transaction, "webflow-start", G_CALLBACK(webflowStart), this); 0114 g_signal_connect(m_transaction, "webflow-done", G_CALLBACK(webflowDoneCallback), this); 0115 } 0116 } 0117 } 0118 0119 FlatpakTransactionThread::~FlatpakTransactionThread() 0120 { 0121 g_object_unref(m_transaction); 0122 g_object_unref(m_cancellable); 0123 } 0124 0125 void FlatpakTransactionThread::cancel() 0126 { 0127 for (int id : std::as_const(m_webflows)) { 0128 flatpak_transaction_abort_webflow(m_transaction, id); 0129 } 0130 g_cancellable_cancel(m_cancellable); 0131 } 0132 0133 void FlatpakTransactionThread::run() 0134 { 0135 auto finish = qScopeGuard([this] { 0136 Q_EMIT finished(); 0137 }); 0138 0139 if (!m_transaction) { 0140 return; 0141 } 0142 g_autoptr(GError) localError = nullptr; 0143 0144 const QString refName = m_app->ref(); 0145 0146 if (m_role == Transaction::Role::InstallRole) { 0147 bool correct = false; 0148 if (m_app->state() == AbstractResource::Upgradeable && m_app->isInstalled()) { 0149 correct = flatpak_transaction_add_update(m_transaction, refName.toUtf8().constData(), nullptr, nullptr, &localError); 0150 } else if (m_app->flatpakFileType() == FlatpakResource::FileFlatpak) { 0151 g_autoptr(GFile) file = g_file_new_for_path(m_app->resourceFile().toLocalFile().toUtf8().constData()); 0152 if (!file) { 0153 qWarning() << "Failed to install bundled application" << refName; 0154 m_result = false; 0155 return; 0156 } 0157 correct = flatpak_transaction_add_install_bundle(m_transaction, file, nullptr, &localError); 0158 } else if (m_app->flatpakFileType() == FlatpakResource::FileFlatpakRef && m_app->resourceFile().isLocalFile()) { 0159 g_autoptr(GFile) file = g_file_new_for_path(m_app->resourceFile().toLocalFile().toUtf8().constData()); 0160 if (!file) { 0161 qWarning() << "Failed to install flatpakref application" << refName; 0162 m_result = false; 0163 return; 0164 } 0165 g_autoptr(GBytes) bytes = g_file_load_bytes(file, m_cancellable, nullptr, &localError); 0166 correct = flatpak_transaction_add_install_flatpakref(m_transaction, bytes, &localError); 0167 } else { 0168 correct = flatpak_transaction_add_install(m_transaction, // 0169 m_app->origin().toUtf8().constData(), 0170 refName.toUtf8().constData(), 0171 nullptr, 0172 &localError); 0173 } 0174 0175 if (!correct) { 0176 m_result = false; 0177 m_errorMessage = QString::fromUtf8(localError->message); 0178 // We are done so we can set the progress to 100 0179 setProgress(100); 0180 qWarning() << "Failed to install" << m_app->flatpakFileType() << refName << ':' << m_errorMessage; 0181 return; 0182 } 0183 } else if (m_role == Transaction::Role::RemoveRole) { 0184 if (!flatpak_transaction_add_uninstall(m_transaction, refName.toUtf8().constData(), &localError)) { 0185 m_result = false; 0186 m_errorMessage = QString::fromUtf8(localError->message); 0187 // We are done so we can set the progress to 100 0188 setProgress(100); 0189 qWarning() << "Failed to uninstall" << refName << ':' << m_errorMessage; 0190 return; 0191 } 0192 } 0193 0194 m_result = flatpak_transaction_run(m_transaction, m_cancellable, &localError); 0195 if (!m_result) { 0196 if (localError->code == FLATPAK_ERROR_REF_NOT_FOUND) { 0197 m_errorMessage = i18n("Could not find '%1' in '%2'; please make sure it's available.", refName, m_app->origin()); 0198 } else { 0199 m_errorMessage = QString::fromUtf8(localError->message); 0200 } 0201 #if defined(FLATPAK_LIST_UNUSED_REFS) 0202 } else { 0203 const auto installation = flatpak_transaction_get_installation(m_transaction); 0204 g_autoptr(GError) refsError = nullptr; 0205 g_autoptr(GPtrArray) refs = flatpak_installation_list_unused_refs(installation, nullptr, m_cancellable, &refsError); 0206 if (!refs) { 0207 qWarning() << "could not fetch unused refs" << refsError->message; 0208 } else if (refs->len > 0) { 0209 g_autoptr(GError) localError = nullptr; 0210 qDebug() << "found unused refs:" << refs->len; 0211 auto transaction = flatpak_transaction_new_for_installation(installation, m_cancellable, &localError); 0212 for (uint i = 0; i < refs->len; i++) { 0213 FlatpakRef *ref = FLATPAK_REF(g_ptr_array_index(refs, i)); 0214 g_autofree gchar *strRef = flatpak_ref_format_ref(ref); 0215 qDebug() << "unused ref:" << strRef; 0216 if (!flatpak_transaction_add_uninstall(transaction, strRef, &localError)) { 0217 qDebug() << "failed to uninstall unused ref" << refName << localError->message; 0218 break; 0219 } 0220 } 0221 if (!flatpak_transaction_run(transaction, m_cancellable, &localError)) { 0222 qWarning() << "could not properly clean the elements" << refs->len << localError->message; 0223 } 0224 } 0225 #endif 0226 } 0227 // We are done so we can set the progress to 100 0228 setProgress(100); 0229 } 0230 0231 void FlatpakTransactionThread::setProgress(int progress) 0232 { 0233 Q_ASSERT(qBound(0, progress, 100) == progress); 0234 if (m_progress != progress) { 0235 m_progress = progress; 0236 Q_EMIT progressChanged(m_progress); 0237 } 0238 } 0239 0240 void FlatpakTransactionThread::setSpeed(quint64 speed) 0241 { 0242 if (m_speed != speed) { 0243 m_speed = speed; 0244 Q_EMIT speedChanged(m_speed); 0245 } 0246 } 0247 0248 QString FlatpakTransactionThread::errorMessage() const 0249 { 0250 return m_errorMessage; 0251 } 0252 0253 bool FlatpakTransactionThread::result() const 0254 { 0255 return m_result; 0256 } 0257 0258 void FlatpakTransactionThread::addErrorMessage(const QString &error) 0259 { 0260 if (!m_errorMessage.isEmpty()) { 0261 m_errorMessage.append(QLatin1Char('\n')); 0262 } 0263 m_errorMessage.append(error); 0264 } 0265 0266 bool FlatpakTransactionThread::cancelled() const 0267 { 0268 return g_cancellable_is_cancelled(m_cancellable); 0269 }