Skip to content

Commit

Permalink
core: Automatically scan default module path
Browse files Browse the repository at this point in the history
Allow passing NULL to cog_modules_add_directory() and add strategically
placed calls to the function to automatically scan the default module
path configured at build time. This makes call sites to the function
simpler as a side effect.
  • Loading branch information
aperezdc committed Nov 14, 2023
1 parent 88fcc0a commit fc83b6d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
26 changes: 24 additions & 2 deletions core/cog-modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

#include "cog-modules.h"

#include "cog-config.h"
#include "cog-fallback-platform.h"

struct ExtensionPoints {
Expand Down Expand Up @@ -104,6 +106,7 @@ cog_modules_get_preferred(GIOExtensionPoint *extension_point, const char *prefer
g_return_val_if_fail(extension_point != NULL, G_TYPE_INVALID);

ensure_builtin_types();
cog_modules_add_directory(NULL);

if (extension_point == COG_MODULES_PLATFORM && g_strcmp0(preferred_module, "fdo") == 0) {
g_warning("Platform module name 'fdo' is deprecated, please use 'wl' instead.");
Expand Down Expand Up @@ -148,6 +151,7 @@ cog_modules_foreach(GIOExtensionPoint *extension_point, void (*callback)(GIOExte
g_return_if_fail(callback != NULL);

ensure_builtin_types();
cog_modules_add_directory(NULL);

GList *item = g_list_first(g_io_extension_point_get_extensions(extension_point));
while (item) {
Expand All @@ -158,11 +162,18 @@ cog_modules_foreach(GIOExtensionPoint *extension_point, void (*callback)(GIOExte

/**
* cog_modules_add_directory:
* @directory_path: Directory to scan for loadable modules.
* @directory_path: (nullable): Directory to scan for loadable modules.
*
* Scans a directory for loadable modules and registers them with the
* extension points they implement.
*
* Since version 0.20 the @directory_path parameter may be %NULL, which
* results in the default module directory chosen at build time to be
* scanned. Normally this function does not need to be done manually,
* because the default module directory will be scanned automatically
* the first time [id@cog_modules_get_preferred] or [id@cog_modules_foreach]
* is used.
*
* Note that in versions 0.20 and newer this function will skip modules
* with the same file base name as the ones previously scanned. If two
* directories contain a module with the same file base name, the one
Expand All @@ -172,7 +183,8 @@ cog_modules_foreach(GIOExtensionPoint *extension_point, void (*callback)(GIOExte
void
cog_modules_add_directory(const char *directory_path)
{
g_return_if_fail(directory_path != NULL);
if (!directory_path)
directory_path = COG_MODULEDIR;

ensure_extension_points();

Expand All @@ -183,8 +195,18 @@ cog_modules_add_directory(const char *directory_path)
if (!scope)
scope = g_io_module_scope_new(G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);

static gboolean default_path_added = FALSE;
if (!strcmp(COG_MODULEDIR, directory_path)) {
if (default_path_added) {
g_debug("%s: Default path already added, skipping.", G_STRFUNC);
goto out; // Ensure that the the lock is released.
}
default_path_added = TRUE;
}

g_debug("%s: Scanning '%s'", G_STRFUNC, directory_path);
g_io_modules_scan_all_in_directory_with_scope(directory_path, scope);

out:
G_UNLOCK(module_scan);
}
2 changes: 1 addition & 1 deletion examples/viewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ main(int argc, char *argv[])
}

g_set_prgname("view-stack");
cog_modules_add_directory(g_getenv("COG_MODULEDIR") ?: COG_MODULEDIR);
cog_modules_add_directory(g_getenv("COG_MODULEDIR"));

g_autoptr(GError) error = NULL;
g_autoptr(CogShell) shell = cog_shell_new(g_get_prgname(), FALSE);
Expand Down
2 changes: 1 addition & 1 deletion launcher/cog.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int
main(int argc, char *argv[])
{
g_set_application_name("Cog");
cog_modules_add_directory(g_getenv("COG_MODULEDIR") ?: COG_MODULEDIR);
cog_modules_add_directory(g_getenv("COG_MODULEDIR"));

g_info("%s:", COG_MODULES_PLATFORM_EXTENSION_POINT);
cog_modules_foreach(COG_MODULES_PLATFORM, print_module_info, NULL);
Expand Down

0 comments on commit fc83b6d

Please sign in to comment.