-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gs-plugin-vso.c
424 lines (344 loc) · 15.1 KB
/
gs-plugin-vso.c
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright (C) 2024 Mateus Melchiades
*/
#include <glib.h>
#include <gnome-software.h>
#include <json-glib/json-glib.h>
#include <stdlib.h>
#include "gs-plugin-vso.h"
static gint get_priority_for_interactivity(gboolean interactive);
static void
setup_thread_cb(GTask *task, gpointer source_object, gpointer task_data, GCancellable *cancellable);
static void list_apps_thread_cb(GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable);
void add_package(JsonArray *array, guint index_, JsonNode *element_node, gpointer user_data);
struct _GsPluginVso {
GsPlugin parent;
GsWorkerThread *worker; /* (owned) */
};
G_DEFINE_TYPE(GsPluginVso, gs_plugin_vso, GS_TYPE_PLUGIN)
#define assert_in_worker(self) g_assert(gs_worker_thread_is_in_worker_context(self->worker))
const gchar *lock_path = "/tmp/ABSystem.Upgrade.lock";
static void
gs_plugin_vso_dispose(GObject *object)
{
GsPluginVso *self = GS_PLUGIN_VSO(object);
g_clear_object(&self->worker);
G_OBJECT_CLASS(gs_plugin_vso_parent_class)->dispose(object);
}
static void
gs_plugin_vso_finalize(GObject *object)
{
G_OBJECT_CLASS(gs_plugin_vso_parent_class)->finalize(object);
}
static gint
get_priority_for_interactivity(gboolean interactive)
{
return interactive ? G_PRIORITY_DEFAULT : G_PRIORITY_LOW;
}
static void
gs_plugin_vso_setup_async(GsPlugin *plugin,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GsPluginVso *self = GS_PLUGIN_VSO(plugin);
g_autoptr(GTask) task = NULL;
task = g_task_new(plugin, cancellable, callback, user_data);
g_task_set_source_tag(task, gs_plugin_vso_setup_async);
// Start up a worker thread to process all the plugin’s function calls.
self->worker = gs_worker_thread_new("gs-plugin-vso");
gs_worker_thread_queue(self->worker, G_PRIORITY_DEFAULT, setup_thread_cb,
g_steal_pointer(&task));
}
static void
setup_thread_cb(GTask *task, gpointer source_object, gpointer task_data, GCancellable *cancellable)
{
g_task_return_boolean(task, TRUE);
}
static gboolean
gs_plugin_vso_setup_finish(GsPlugin *plugin, GAsyncResult *result, GError **error)
{
return g_task_propagate_boolean(G_TASK(result), error);
}
static void
gs_plugin_vso_init(GsPluginVso *self)
{
GsPlugin *plugin = GS_PLUGIN(self);
gs_plugin_add_rule(plugin, GS_PLUGIN_RULE_RUN_BEFORE, "os-release");
gs_plugin_add_rule(plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
}
void
gs_plugin_adopt_app(GsPlugin *plugin, GsApp *app)
{
if (gs_app_get_kind(app) == AS_COMPONENT_KIND_DESKTOP_APP &&
gs_app_has_management_plugin(app, NULL)) {
gs_app_set_management_plugin(app, plugin);
gs_app_add_quirk(app, GS_APP_QUIRK_PROVENANCE);
// FIXME: Appinfo for pre-installed apps have no indidation of what is the package
// name, so we have no way of knowing how to uninstall them.
gs_app_add_quirk(app, GS_APP_QUIRK_COMPULSORY);
gs_app_set_scope(app, AS_COMPONENT_SCOPE_SYSTEM);
gs_app_set_metadata(app, "GnomeSoftware::SortKey", "200");
gs_app_set_metadata(app, "GnomeSoftware::PackagingBaseCssColor", "warning_color");
gs_app_set_metadata(app, "GnomeSoftware::PackagingIcon",
"org.vanillaos.FirstSetup-symbolic");
gs_app_set_metadata(app, "GnomeSoftware::PackagingFormat", "System");
gs_app_set_origin(app, "vso");
gs_app_set_origin_ui(app, "Vanilla OS Base");
gs_app_set_origin_hostname(app, "https://vanillaos.org");
if (gs_plugin_cache_lookup(plugin, gs_app_get_id(app)) == NULL) {
gs_plugin_cache_add(plugin, gs_app_get_id(app), app);
}
}
}
static void
gs_plugin_vso_list_apps_async(GsPlugin *plugin,
GsAppQuery *query,
GsPluginListAppsFlags flags,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GsPluginVso *self = GS_PLUGIN_VSO(plugin);
g_autoptr(GTask) task = NULL;
gboolean interactive = (flags & GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);
task =
gs_plugin_list_apps_data_new_task(plugin, query, flags, cancellable, callback, user_data);
g_task_set_source_tag(task, gs_plugin_vso_list_apps_async);
/* Queue a job to get the apps. */
gs_worker_thread_queue(self->worker, get_priority_for_interactivity(interactive),
list_apps_thread_cb, g_steal_pointer(&task));
}
static void
list_apps_thread_cb(GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable)
{
GsPluginVso *self = GS_PLUGIN_VSO(source_object);
g_autoptr(GsAppList) list = gs_app_list_new();
GsPluginListAppsData *data = task_data;
GsApp *alternate_of = NULL;
assert_in_worker(self);
if (data->query != NULL) {
alternate_of = gs_app_query_get_alternate_of(data->query);
}
if (alternate_of != NULL) {
GsApp *app = gs_plugin_cache_lookup(GS_PLUGIN(self), gs_app_get_id(alternate_of));
gs_app_set_origin(app, "vso");
gs_app_set_origin_ui(app, "Vanilla OS Base");
gs_app_set_origin_hostname(app, "https://vanillaos.org");
if (app != NULL)
gs_app_list_add(list, app);
}
g_task_return_pointer(task, g_steal_pointer(&list), g_object_unref);
}
static GsAppList *
gs_plugin_vso_list_apps_finish(GsPlugin *plugin, GAsyncResult *result, GError **error)
{
g_return_val_if_fail(g_task_get_source_tag(G_TASK(result)) == gs_plugin_vso_list_apps_async,
FALSE);
return g_task_propagate_pointer(G_TASK(result), error);
}
static gboolean
plugin_vso_pick_desktop_file_cb(GsPlugin *plugin,
GsApp *app,
const gchar *filename,
GKeyFile *key_file)
{
return strstr(filename, "/snapd/") == NULL && strstr(filename, "/snap/") == NULL &&
strstr(filename, "/flatpak/") == NULL &&
g_key_file_has_group(key_file, "Desktop Entry") &&
!g_key_file_has_key(key_file, "Desktop Entry", "X-Flatpak", NULL) &&
!g_key_file_has_key(key_file, "Desktop Entry", "X-SnapInstanceName", NULL);
}
gboolean
gs_plugin_launch(GsPlugin *plugin, GsApp *app, GCancellable *cancellable, GError **error)
{
/* only process this app if was created by this plugin */
if (!gs_app_has_management_plugin(app, plugin))
return TRUE;
return gs_plugin_app_launch_filtered(plugin, app, plugin_vso_pick_desktop_file_cb, NULL, error);
}
gboolean
gs_plugin_add_updates_historical(GsPlugin *plugin,
GsAppList *list,
GCancellable *cancellable,
GError **error)
{
return TRUE;
}
gboolean
gs_plugin_update(GsPlugin *plugin, GsAppList *list, GCancellable *cancellable, GError **error)
{
gboolean ours = false;
for (guint i = 0; i < gs_app_list_length(list); i++) {
GsApp *app = gs_app_list_index(list, i);
if (!g_strcmp0(gs_app_get_id(app), "org.gnome.Software.OsUpdate")) {
ours = true;
break;
}
}
// Nothing to do with us...
if (!ours)
return FALSE;
// Cannot update if transactions are locked
g_autoptr(GFile) lock_file = g_file_new_for_path(lock_path);
g_autoptr(GError) local_error = NULL;
if (g_file_query_exists(lock_file, cancellable)) {
g_set_error_literal(&local_error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
"Another transaction has already been executed, you must reboot your "
"system before starting a new transaction.");
*error = g_steal_pointer(&local_error);
return FALSE;
}
// Call trigger-update
const gchar *cmd = "pkexec vso sys-upgrade upgrade";
g_autoptr(GSubprocess) subprocess = NULL;
guint exit_status = -1;
subprocess = g_subprocess_new(G_SUBPROCESS_FLAGS_NONE, error, "sh", "-c", cmd, NULL);
if (!g_subprocess_wait(subprocess, cancellable, error))
return FALSE;
exit_status = g_subprocess_get_exit_status(subprocess);
if (exit_status != EXIT_SUCCESS) {
g_set_error_literal(&local_error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
"VSO failed to update the system, please try again later.");
*error = g_steal_pointer(&local_error);
return FALSE;
}
return TRUE;
}
void
add_package(JsonArray *array, guint index_, JsonNode *element_node, gpointer user_data)
{
GsPluginPkgAddFuncData *data = user_data;
GsPlugin *plugin = data->plugin;
GsAppList *list = data->list;
JsonObject *pkg_info = json_node_get_object(element_node);
const gchar *name = NULL;
const gchar *old_version = NULL;
const gchar *new_version = NULL;
if (json_object_has_member(pkg_info, "name"))
name = json_object_get_string_member(pkg_info, "name");
else
name = "";
if (json_object_has_member(pkg_info, "previous_version"))
old_version = json_object_get_string_member(pkg_info, "previous_version");
if (json_object_has_member(pkg_info, "new_version"))
new_version = json_object_get_string_member(pkg_info, "new_version");
g_debug("Adding package %s: %s -> %s", name, old_version, new_version);
g_autoptr(GsApp) app = gs_app_new(NULL);
gs_app_set_management_plugin(app, plugin);
gs_app_add_quirk(app, GS_APP_QUIRK_NEEDS_REBOOT);
gs_app_set_bundle_kind(app, AS_BUNDLE_KIND_PACKAGE);
gs_app_set_scope(app, AS_COMPONENT_SCOPE_SYSTEM);
gs_app_set_kind(app, AS_COMPONENT_KIND_GENERIC);
gs_app_set_size_download(app, GS_SIZE_TYPE_UNKNOWN, 0);
gs_app_add_source(app, g_strdup(name));
gs_app_set_name(app, GS_APP_QUALITY_LOWEST, g_strdup(name));
if (old_version != NULL && new_version == NULL) { // Removed
gs_app_set_version(app, g_strdup(old_version));
gs_app_set_state(app, GS_APP_STATE_UNAVAILABLE);
} else if (old_version == NULL && new_version != NULL) { // Added
gs_app_set_version(app, g_strdup(new_version));
gs_app_set_state(app, GS_APP_STATE_AVAILABLE);
} else { // Modified
gs_app_set_version(app, g_strdup(old_version));
gs_app_set_update_version(app, g_strdup(new_version));
gs_app_set_state(app, GS_APP_STATE_UPDATABLE);
}
gs_plugin_cache_add(plugin, name, app);
gs_app_list_add(list, app);
}
gboolean
gs_plugin_add_updates(GsPlugin *plugin, GsAppList *list, GCancellable *cancellable, GError **error)
{
const gchar *cmd = "pkexec vso sys-upgrade check --json";
g_autoptr(GSubprocess) subprocess = NULL;
GInputStream *input_stream;
subprocess = g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE, error, "sh", "-c", cmd, NULL);
if (!g_subprocess_wait(subprocess, cancellable, error))
return FALSE;
input_stream = g_subprocess_get_stdout_pipe(subprocess);
if (input_stream != NULL) {
g_autoptr(GByteArray) cmd_out = g_byte_array_new();
gchar buffer[4096];
gsize nread = 0;
gboolean success;
g_auto(GStrv) splits = NULL;
g_auto(GStrv) pkg_splits = NULL;
g_auto(GStrv) pkg_versions = NULL;
while (success = g_input_stream_read_all(input_stream, buffer, sizeof(buffer), &nread,
cancellable, error),
success && nread > 0) {
g_byte_array_append(cmd_out, (const guint8 *)buffer, nread);
}
// If we have a valid output
if (success && cmd_out->len > 0) {
// NUL-terminate the array, to use it as a string
g_byte_array_append(cmd_out, (const guint8 *)"", 1);
g_debug("Got JSON: %s", cmd_out->data);
splits = g_strsplit((gchar *)cmd_out->data, "\n", -1);
JsonNode *output_json = json_from_string(splits[g_strv_length(splits) - 2], error);
if (output_json == NULL) {
(*error)->message = g_strconcat(
"Error parsing VSO upgrade-check output: ", (*error)->message, NULL);
g_debug("%s", (*error)->message);
return FALSE;
}
JsonObject *update_info = json_node_get_object(output_json);
if (!json_object_get_boolean_member(update_info, "hasUpdate")) {
g_debug("No updates");
json_node_free(output_json);
return TRUE;
}
g_debug("New image digest: %s",
json_object_get_string_member(update_info, "newDigest"));
GsPluginPkgAddFuncData data = {.plugin = plugin, .list = list};
// Parse image packages
JsonObject *system_pkgs =
json_object_get_object_member(update_info, "systemPackageDiff");
json_array_foreach_element(json_object_get_array_member(system_pkgs, "added"),
add_package, &data);
json_array_foreach_element(json_object_get_array_member(system_pkgs, "upgraded"),
add_package, &data);
json_array_foreach_element(json_object_get_array_member(system_pkgs, "downgraded"),
add_package, &data);
json_array_foreach_element(json_object_get_array_member(system_pkgs, "removed"),
add_package, &data);
// Parse overlay packages
JsonObject *user_pkgs =
json_object_get_object_member(update_info, "overlayPackageDiff");
json_array_foreach_element(json_object_get_array_member(user_pkgs, "added"),
add_package, &data);
json_array_foreach_element(json_object_get_array_member(user_pkgs, "upgraded"),
add_package, &data);
json_array_foreach_element(json_object_get_array_member(user_pkgs, "downgraded"),
add_package, &data);
json_array_foreach_element(json_object_get_array_member(user_pkgs, "removed"),
add_package, &data);
json_node_free(output_json);
}
}
return TRUE;
}
static void
gs_plugin_vso_class_init(GsPluginVsoClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
GsPluginClass *plugin_class = GS_PLUGIN_CLASS(klass);
object_class->dispose = gs_plugin_vso_dispose;
object_class->finalize = gs_plugin_vso_finalize;
plugin_class->setup_async = gs_plugin_vso_setup_async;
plugin_class->setup_finish = gs_plugin_vso_setup_finish;
plugin_class->list_apps_async = gs_plugin_vso_list_apps_async;
plugin_class->list_apps_finish = gs_plugin_vso_list_apps_finish;
}
GType
gs_plugin_query_type(void)
{
return GS_TYPE_PLUGIN_VSO;
}