1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "giomodule-priv.h"
32 #include "glib/glib-private.h"
35 is_valid_module_name (const gchar *basename)
37 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
38 #if defined(__APPLE__)
39 return g_str_has_prefix (basename, "lib") &&
40 (g_str_has_suffix (basename, ".so") ||
41 g_str_has_suffix (basename, ".dylib"));
44 g_str_has_prefix (basename, "lib") &&
45 g_str_has_suffix (basename, ".so");
48 return g_str_has_suffix (basename, ".dll");
53 query_dir (const char *dirname)
57 GList *list = NULL, *iterator = NULL;
60 char **(* query) (void);
64 if (!g_module_supported ())
68 dir = g_dir_open (dirname, 0, &error);
71 g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
76 data = g_string_new ("");
78 while ((name = g_dir_read_name (dir)))
79 list = g_list_prepend (list, g_strdup (name));
81 list = g_list_sort (list, (GCompareFunc) g_strcmp0);
82 for (iterator = list; iterator; iterator = iterator->next)
86 char **extension_points;
88 name = iterator->data;
89 if (!is_valid_module_name (name))
92 path = g_build_filename (dirname, name, NULL);
93 module = g_module_open_full (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
101 modulename = _g_io_module_extract_name (name);
102 symname = g_strconcat ("g_io_", modulename, "_query", NULL);
103 g_module_symbol (module, symname, (gpointer) &query);
109 /* Fallback to old name */
110 g_module_symbol (module, "g_io_module_query", (gpointer) &query);
115 extension_points = query ();
117 if (extension_points)
119 g_string_append_printf (data, "%s: ", name);
121 for (i = 0; extension_points[i] != NULL; i++)
122 g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
124 g_string_append (data, "\n");
125 g_strfreev (extension_points);
129 g_module_close (module);
133 g_debug ("Failed to open module %s: %s", name, error->message);
136 g_clear_error (&error);
140 g_list_free_full (list, g_free);
142 cachename = g_build_filename (dirname, "giomodule.cache", NULL);
148 if (!g_file_set_contents (cachename, data->str, data->len, &error))
150 g_printerr ("Unable to create %s: %s\n", cachename, error->message);
151 g_error_free (error);
156 if (g_unlink (cachename) != 0 && errno != ENOENT)
159 g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
164 g_string_free (data, TRUE);
175 g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
176 g_print ("Will update giomodule.cache in the listed directories\n");
180 setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
182 /* Be defensive and ensure we're linked to GObject */
183 g_type_ensure (G_TYPE_OBJECT);
185 for (i = 1; i < argc; i++)