1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
22 #include "giomodule.h"
23 #include "giomodule-priv.h"
29 #include "glib/glib-private.h"
32 is_valid_module_name (const gchar *basename)
34 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
36 g_str_has_prefix (basename, "lib") &&
37 g_str_has_suffix (basename, ".so");
39 return g_str_has_suffix (basename, ".dll");
44 query_dir (const char *dirname)
48 GList *list = NULL, *iterator = NULL;
51 char **(* query) (void);
55 if (!g_module_supported ())
59 dir = g_dir_open (dirname, 0, &error);
62 g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
67 data = g_string_new ("");
69 while ((name = g_dir_read_name (dir)))
70 list = g_list_prepend (list, g_strdup (name));
72 list = g_list_sort (list, (GCompareFunc) g_strcmp0);
73 for (iterator = list; iterator; iterator = iterator->next)
77 char **extension_points;
79 name = iterator->data;
80 if (!is_valid_module_name (name))
83 path = g_build_filename (dirname, name, NULL);
84 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
92 modulename = _g_io_module_extract_name (name);
93 symname = g_strconcat ("g_io_", modulename, "_query", NULL);
94 g_module_symbol (module, symname, (gpointer) &query);
100 /* Fallback to old name */
101 g_module_symbol (module, "g_io_module_query", (gpointer) &query);
106 extension_points = query ();
108 if (extension_points)
110 g_string_append_printf (data, "%s: ", name);
112 for (i = 0; extension_points[i] != NULL; i++)
113 g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
115 g_string_append (data, "\n");
116 g_strfreev (extension_points);
120 g_module_close (module);
125 g_list_free_full (list, g_free);
127 cachename = g_build_filename (dirname, "giomodule.cache", NULL);
133 if (!g_file_set_contents (cachename, data->str, data->len, &error))
135 g_printerr ("Unable to create %s: %s\n", cachename, error->message);
136 g_error_free (error);
141 if (g_unlink (cachename) != 0 && errno != ENOENT)
144 g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
149 g_string_free (data, TRUE);
160 g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
161 g_print ("Will update giomodule.cache in the listed directories\n");
165 setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
167 /* Be defensive and ensure we're linked to GObject */
168 g_type_ensure (G_TYPE_OBJECT);
170 for (i = 1; i < argc; i++)