2010-01-12 13:11:22 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2010-01-12 13:11:22 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-01-12 13:11:22 +01:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2010-01-12 13:11:22 +01:00
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2022-10-05 23:45:07 +02:00
|
|
|
|
|
|
|
#include <gio/gio.h>
|
2017-11-30 21:36:21 +01:00
|
|
|
#include "giomodule-priv.h"
|
2010-01-12 13:11:22 +01:00
|
|
|
|
2012-03-08 17:55:09 +01:00
|
|
|
#include <gstdio.h>
|
|
|
|
#include <errno.h>
|
2016-01-11 06:51:32 +01:00
|
|
|
#include <locale.h>
|
2012-03-08 17:55:09 +01:00
|
|
|
|
2019-06-05 10:42:30 +02:00
|
|
|
#include "glib/glib-private.h"
|
|
|
|
|
2010-01-12 13:11:22 +01:00
|
|
|
static gboolean
|
|
|
|
is_valid_module_name (const gchar *basename)
|
|
|
|
{
|
|
|
|
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
|
2022-11-02 11:01:09 +01:00
|
|
|
#if defined(__APPLE__)
|
Improve g_module_open(), deprecate G_MODULE_SUFFIX
G_MODULE_SUFFIX is deprecated now because you will get the wrong
results using it most of the time:
1. The suffix on macOS is usually 'dylib', but it's 'so' when using
Autotools, so there's no way to get the suffix correct using
a pre-processor macro.
2. Prefixes also vary in a platform-specific way. You may or may not have
a 'lib' prefix for the name on Windows and on Cygwin the prefix is
'cyg'.
3. The library name itself can vary per platform. For instance, you may
want to load foo-1.dll on Windows and libfoo.1.dylib on macOS. This
is for libraries, not modules, but that is still a use-case that
people use the GModule API for.
g_module_build_path() does take care of (2) on Cygwin, but it
fundamentally cannot handle the possibility of multiple options for
the module name, since it does not do any I/O. Hence, it is also
deprecated.
Instead, g_module_open() has been improved so that it takes care of
all this by searching the filesystem for combinations of possible
suffixes and prefixes on each platform. Along the way, the
documentation for it was also improved to make it clearer what it
does.
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/520
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/1413
2022-10-14 01:04:13 +02:00
|
|
|
return g_str_has_prefix (basename, "lib") &&
|
|
|
|
(g_str_has_suffix (basename, ".so") ||
|
|
|
|
g_str_has_suffix (basename, ".dylib"));
|
|
|
|
#else
|
2010-01-12 13:11:22 +01:00
|
|
|
return
|
|
|
|
g_str_has_prefix (basename, "lib") &&
|
|
|
|
g_str_has_suffix (basename, ".so");
|
Improve g_module_open(), deprecate G_MODULE_SUFFIX
G_MODULE_SUFFIX is deprecated now because you will get the wrong
results using it most of the time:
1. The suffix on macOS is usually 'dylib', but it's 'so' when using
Autotools, so there's no way to get the suffix correct using
a pre-processor macro.
2. Prefixes also vary in a platform-specific way. You may or may not have
a 'lib' prefix for the name on Windows and on Cygwin the prefix is
'cyg'.
3. The library name itself can vary per platform. For instance, you may
want to load foo-1.dll on Windows and libfoo.1.dylib on macOS. This
is for libraries, not modules, but that is still a use-case that
people use the GModule API for.
g_module_build_path() does take care of (2) on Cygwin, but it
fundamentally cannot handle the possibility of multiple options for
the module name, since it does not do any I/O. Hence, it is also
deprecated.
Instead, g_module_open() has been improved so that it takes care of
all this by searching the filesystem for combinations of possible
suffixes and prefixes on each platform. Along the way, the
documentation for it was also improved to make it clearer what it
does.
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/520
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/1413
2022-10-14 01:04:13 +02:00
|
|
|
#endif
|
2010-01-12 13:11:22 +01:00
|
|
|
#else
|
|
|
|
return g_str_has_suffix (basename, ".dll");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
query_dir (const char *dirname)
|
|
|
|
{
|
|
|
|
GString *data;
|
|
|
|
GDir *dir;
|
2017-08-29 17:59:56 +02:00
|
|
|
GList *list = NULL, *iterator = NULL;
|
2010-01-12 13:11:22 +01:00
|
|
|
const char *name;
|
|
|
|
char *cachename;
|
|
|
|
char **(* query) (void);
|
|
|
|
GError *error;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!g_module_supported ())
|
|
|
|
return;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
dir = g_dir_open (dirname, 0, &error);
|
|
|
|
if (!dir)
|
|
|
|
{
|
|
|
|
g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = g_string_new ("");
|
|
|
|
|
|
|
|
while ((name = g_dir_read_name (dir)))
|
2017-08-29 17:59:56 +02:00
|
|
|
list = g_list_prepend (list, g_strdup (name));
|
|
|
|
|
|
|
|
list = g_list_sort (list, (GCompareFunc) g_strcmp0);
|
|
|
|
for (iterator = list; iterator; iterator = iterator->next)
|
2010-01-12 13:11:22 +01:00
|
|
|
{
|
|
|
|
GModule *module;
|
|
|
|
gchar *path;
|
|
|
|
char **extension_points;
|
|
|
|
|
2017-08-29 17:59:56 +02:00
|
|
|
name = iterator->data;
|
2010-01-12 13:11:22 +01:00
|
|
|
if (!is_valid_module_name (name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path = g_build_filename (dirname, name, NULL);
|
2021-07-21 22:54:35 +02:00
|
|
|
module = g_module_open_full (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
|
2010-01-12 13:11:22 +01:00
|
|
|
g_free (path);
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
{
|
2017-11-30 21:36:21 +01:00
|
|
|
gchar *modulename;
|
|
|
|
gchar *symname;
|
|
|
|
|
|
|
|
modulename = _g_io_module_extract_name (name);
|
|
|
|
symname = g_strconcat ("g_io_", modulename, "_query", NULL);
|
|
|
|
g_module_symbol (module, symname, (gpointer) &query);
|
|
|
|
g_free (symname);
|
|
|
|
g_free (modulename);
|
|
|
|
|
|
|
|
if (!query)
|
|
|
|
{
|
|
|
|
/* Fallback to old name */
|
|
|
|
g_module_symbol (module, "g_io_module_query", (gpointer) &query);
|
|
|
|
}
|
2010-01-12 13:11:22 +01:00
|
|
|
|
|
|
|
if (query)
|
|
|
|
{
|
|
|
|
extension_points = query ();
|
|
|
|
|
|
|
|
if (extension_points)
|
|
|
|
{
|
|
|
|
g_string_append_printf (data, "%s: ", name);
|
|
|
|
|
|
|
|
for (i = 0; extension_points[i] != NULL; i++)
|
|
|
|
g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
|
|
|
|
|
|
|
|
g_string_append (data, "\n");
|
|
|
|
g_strfreev (extension_points);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_module_close (module);
|
|
|
|
}
|
2021-07-21 22:54:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_debug ("Failed to open module %s: %s", name, error->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_clear_error (&error);
|
2010-01-12 13:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_dir_close (dir);
|
2017-08-29 17:59:56 +02:00
|
|
|
g_list_free_full (list, g_free);
|
2010-01-12 13:11:22 +01:00
|
|
|
|
|
|
|
cachename = g_build_filename (dirname, "giomodule.cache", NULL);
|
|
|
|
|
2012-03-08 17:55:09 +01:00
|
|
|
if (data->len > 0)
|
2010-01-12 13:11:22 +01:00
|
|
|
{
|
2012-03-08 17:55:09 +01:00
|
|
|
error = NULL;
|
|
|
|
|
|
|
|
if (!g_file_set_contents (cachename, data->str, data->len, &error))
|
|
|
|
{
|
|
|
|
g_printerr ("Unable to create %s: %s\n", cachename, error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (g_unlink (cachename) != 0 && errno != ENOENT)
|
2017-07-31 12:30:55 +02:00
|
|
|
{
|
|
|
|
int errsv = errno;
|
|
|
|
g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
|
|
|
|
}
|
2010-01-12 13:11:22 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:28:21 +01:00
|
|
|
g_free (cachename);
|
2010-01-12 13:11:22 +01:00
|
|
|
g_string_free (data, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (gint argc,
|
|
|
|
gchar *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-01-31 15:47:57 +01:00
|
|
|
if (argc <= 1)
|
2010-01-12 13:11:22 +01:00
|
|
|
{
|
|
|
|
g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
|
|
|
|
g_print ("Will update giomodule.cache in the listed directories\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-05 10:42:30 +02:00
|
|
|
setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
|
2016-01-11 06:51:32 +01:00
|
|
|
|
2013-01-10 22:25:15 +01:00
|
|
|
/* Be defensive and ensure we're linked to GObject */
|
|
|
|
g_type_ensure (G_TYPE_OBJECT);
|
|
|
|
|
2010-01-12 13:11:22 +01:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
query_dir (argv[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|