| Directory: | src/ |
|---|---|
| File: | src/backend/px-module.c |
| Date: | 2023-01-04 17:35:37 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 35 | 83 | 42.2% |
| Branches: | 20 | 47 | 42.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* px-module.c | ||
| 2 | * | ||
| 3 | * Copyright 2022-2023 Jan-Michael Brummer | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | * Lesser General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU Lesser General Public | ||
| 16 | * License along with this library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | * | ||
| 19 | * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "px-module.h" | ||
| 23 | #include "px-config-module.h" | ||
| 24 | #include "px-pacrunner-module.h" | ||
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | G_DEFINE_QUARK (px - module - error - quark, px_module_error) |
| 27 | |||
| 28 | struct _PxModule { | ||
| 29 | GObject parent_instance; | ||
| 30 | }; | ||
| 31 | |||
| 32 |
6/7✓ Branch 0 taken 3 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 13 times.
|
44 | G_DEFINE_TYPE (PxModule, px_module, G_TYPE_OBJECT) |
| 33 | |||
| 34 | static void | ||
| 35 | 3 | px_module_class_init (PxModuleClass *klass) | |
| 36 | { | ||
| 37 | 3 | } | |
| 38 | |||
| 39 | static void | ||
| 40 | 30 | px_module_init (PxModule *self) | |
| 41 | { | ||
| 42 | 30 | } | |
| 43 | |||
| 44 | PxModule * | ||
| 45 | ✗ | px_module_load_finished (GObject *source_object, | |
| 46 | GAsyncResult *result, | ||
| 47 | GError **error) | ||
| 48 | { | ||
| 49 | ✗ | return g_task_propagate_pointer (G_TASK (result), error); | |
| 50 | } | ||
| 51 | |||
| 52 | static void | ||
| 53 | ✗ | load_module_ready_cb (GFile *target, | |
| 54 | GAsyncResult *result, | ||
| 55 | gpointer user_data) | ||
| 56 | { | ||
| 57 | ✗ | GTask *load_task = user_data; | |
| 58 | ✗ | g_autoptr (PxModule) module = NULL; | |
| 59 | ✗ | g_autoptr (GError) error = NULL; | |
| 60 | |||
| 61 | ✗ | module = g_task_propagate_pointer (G_TASK (result), &error); | |
| 62 | ✗ | if (error) { | |
| 63 | ✗ | g_task_return_error (load_task, g_steal_pointer (&error)); | |
| 64 | ✗ | return; | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | g_task_return_pointer (load_task, g_steal_pointer (&module), NULL); | |
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | static void | ||
| 72 | ✗ | load_module_thread (GTask *task, | |
| 73 | gpointer source_object, | ||
| 74 | gpointer task_data, | ||
| 75 | GCancellable *cancellable) | ||
| 76 | { | ||
| 77 | ✗ | GFile *target = source_object; | |
| 78 | GModule *module; | ||
| 79 | gpointer func; | ||
| 80 | PxModuleCreate create; | ||
| 81 | PxModule *px_module; | ||
| 82 | |||
| 83 | ✗ | g_debug ("Loading %s...\n", g_file_peek_path (target)); | |
| 84 | ✗ | module = g_module_open (g_file_peek_path (target), G_MODULE_BIND_LAZY); | |
| 85 | ✗ | if (!module) { | |
| 86 | ✗ | g_warning ("%s: failed for %s", __FUNCTION__, g_file_peek_path (target)); | |
| 87 | ✗ | g_task_return_new_error (task, PX_MODULE_ERROR, PX_MODULE_ERROR_INVALID_MODULE, "Could not load module"); | |
| 88 | ✗ | return; | |
| 89 | } | ||
| 90 | |||
| 91 | ✗ | if (!g_module_symbol (module, "px_module_create", &func)) { | |
| 92 | ✗ | g_warning ("Could not load module!"); | |
| 93 | ✗ | g_module_close (module); | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | g_module_make_resident (module); | |
| 97 | |||
| 98 | ✗ | create = func; | |
| 99 | ✗ | px_module = create (); | |
| 100 | |||
| 101 | ✗ | if (!px_module) { | |
| 102 | ✗ | g_warning ("%s: failed for %s", __FUNCTION__, g_file_peek_path (target)); | |
| 103 | ✗ | g_task_return_new_error (task, PX_MODULE_ERROR, PX_MODULE_ERROR_INVALID_MODULE, "Could not instantiate module"); | |
| 104 | ✗ | return; | |
| 105 | } | ||
| 106 | |||
| 107 | ✗ | g_print ("%s: Loaded %s\n", __FUNCTION__, g_file_peek_path (target)); | |
| 108 | |||
| 109 | ✗ | g_task_return_pointer (task, g_steal_pointer (&px_module), NULL); | |
| 110 | } | ||
| 111 | |||
| 112 | PxModule * | ||
| 113 | 30 | px_module_load (GFile *target, | |
| 114 | GFileInfo *info, | ||
| 115 | gpointer user_data) | ||
| 116 | { | ||
| 117 | GModule *module; | ||
| 118 | gpointer func; | ||
| 119 | PxModuleCreate create; | ||
| 120 | PxModule *px_module; | ||
| 121 | |||
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | g_assert (target); |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | g_assert (info); |
| 124 | |||
| 125 | 30 | g_debug ("%s: Loading %s\n", __FUNCTION__, g_file_peek_path (target)); | |
| 126 | 30 | module = g_module_open (g_file_peek_path (target), G_MODULE_BIND_LAZY); | |
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!module) { |
| 128 | ✗ | g_warning ("%s: failed for %s", __FUNCTION__, g_file_peek_path (target)); | |
| 129 | ✗ | return NULL; | |
| 130 | } | ||
| 131 | |||
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!g_module_symbol (module, "px_module_create", &func)) { |
| 133 | ✗ | g_warning ("Could not load module!"); | |
| 134 | ✗ | g_module_close (module); | |
| 135 | } | ||
| 136 | |||
| 137 | 30 | g_module_make_resident (module); | |
| 138 | |||
| 139 | 30 | create = func; | |
| 140 | 30 | px_module = create (); | |
| 141 | |||
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!px_module) { |
| 143 | ✗ | g_warning ("%s: failed for %s", __FUNCTION__, g_file_peek_path (target)); | |
| 144 | ✗ | return NULL; | |
| 145 | } | ||
| 146 | |||
| 147 | 30 | g_print ("%s: Loaded %s\n", __FUNCTION__, g_file_peek_path (target)); | |
| 148 | 30 | return px_module; | |
| 149 | } | ||
| 150 | |||
| 151 | void | ||
| 152 | ✗ | px_module_load_async (GFile *target, | |
| 153 | GFileInfo *info, | ||
| 154 | GCancellable *cancellable, | ||
| 155 | GAsyncReadyCallback callback, | ||
| 156 | gpointer user_data) | ||
| 157 | { | ||
| 158 | GTask *task; | ||
| 159 | GTask *module_task; | ||
| 160 | |||
| 161 | ✗ | g_assert (target); | |
| 162 | ✗ | g_assert (info); | |
| 163 | |||
| 164 | ✗ | g_debug ("%s: Loading %s\n", __FUNCTION__, g_file_peek_path (target)); | |
| 165 | ✗ | task = g_task_new (target, cancellable, callback, user_data); | |
| 166 | ✗ | g_task_set_return_on_cancel (task, TRUE); | |
| 167 | |||
| 168 | ✗ | module_task = g_task_new (target, g_task_get_cancellable (task), (GAsyncReadyCallback)load_module_ready_cb, task); | |
| 169 | ✗ | g_task_set_task_data (module_task, GUINT_TO_POINTER (TRUE), NULL); | |
| 170 | ✗ | g_task_set_return_on_cancel (module_task, TRUE); | |
| 171 | ✗ | g_task_run_in_thread (module_task, load_module_thread); | |
| 172 | } | ||
| 173 | |||
| 174 | const char * | ||
| 175 | 9 | px_module_get_name (PxModule *self) | |
| 176 | { | ||
| 177 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
|
9 | if (PX_IS_CONFIG_MODULE (self)) { |
| 178 | 9 | PxConfigModuleInterface *ifc = PX_CONFIG_MODULE_GET_INTERFACE (self); | |
| 179 | |||
| 180 | 9 | return ifc->name; | |
| 181 | } | ||
| 182 | |||
| 183 | ✗ | return NULL; | |
| 184 | } | ||
| 185 | |||
| 186 | static void | ||
| 187 | 2 | px_config_module_interface_init (PxConfigModuleInterface *iface) | |
| 188 | { | ||
| 189 | 2 | } | |
| 190 | |||
| 191 | GType | ||
| 192 | 49 | px_config_module_get_type (void) | |
| 193 | { | ||
| 194 | static GType type = 0; | ||
| 195 | static const GTypeInfo info = { | ||
| 196 | sizeof (PxConfigModuleInterface), | ||
| 197 | NULL, | ||
| 198 | NULL, | ||
| 199 | (GClassInitFunc)px_config_module_interface_init | ||
| 200 | }; | ||
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2 times.
|
49 | if (type) return type; |
| 203 | |||
| 204 | 2 | type = g_type_register_static ( | |
| 205 | G_TYPE_INTERFACE, | ||
| 206 | "PxConfigModule", | ||
| 207 | &info, | ||
| 208 | 0); | ||
| 209 | |||
| 210 | 2 | return type; | |
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | static void | ||
| 215 | 2 | px_pacrunner_module_interface_init (PxPacrunnerModuleInterface *iface) | |
| 216 | { | ||
| 217 | 2 | } | |
| 218 | |||
| 219 | GType | ||
| 220 | 2 | px_pacrunner_module_get_type (void) | |
| 221 | { | ||
| 222 | static GType type = 0; | ||
| 223 | static const GTypeInfo info = { | ||
| 224 | sizeof (PxPacrunnerModuleInterface), | ||
| 225 | NULL, | ||
| 226 | NULL, | ||
| 227 | (GClassInitFunc)px_pacrunner_module_interface_init | ||
| 228 | }; | ||
| 229 | |||
| 230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (type) return type; |
| 231 | |||
| 232 | 2 | type = g_type_register_static ( | |
| 233 | G_TYPE_INTERFACE, | ||
| 234 | "PxPacrunnerModule", | ||
| 235 | &info, | ||
| 236 | 0); | ||
| 237 | |||
| 238 | 2 | return type; | |
| 239 | } | ||
| 240 |