1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include <gio/gio.h>
#include "giomodule-priv.h"
#include <gstdio.h>
#include <errno.h>
#include <locale.h>
#include "glib/glib-private.h"
static gboolean
is_valid_module_name (const gchar *basename)
{
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
#if defined(__APPLE__)
return g_str_has_prefix (basename, "lib") &&
(g_str_has_suffix (basename, ".so") ||
g_str_has_suffix (basename, ".dylib"));
#else
return
g_str_has_prefix (basename, "lib") &&
g_str_has_suffix (basename, ".so");
#endif
#else
return g_str_has_suffix (basename, ".dll");
#endif
}
static void
query_dir (const char *dirname)
{
GString *data;
GDir *dir;
GList *list = NULL, *iterator = NULL;
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)))
list = g_list_prepend (list, g_strdup (name));
list = g_list_sort (list, (GCompareFunc) g_strcmp0);
for (iterator = list; iterator; iterator = iterator->next)
{
GModule *module;
gchar *path;
char **extension_points;
name = iterator->data;
if (!is_valid_module_name (name))
continue;
path = g_build_filename (dirname, name, NULL);
module = g_module_open_full (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
g_free (path);
if (module)
{
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);
}
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);
}
else
{
g_debug ("Failed to open module %s: %s", name, error->message);
}
g_clear_error (&error);
}
g_dir_close (dir);
g_list_free_full (list, g_free);
cachename = g_build_filename (dirname, "giomodule.cache", NULL);
if (data->len > 0)
{
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)
{
int errsv = errno;
g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
}
}
g_free (cachename);
g_string_free (data, TRUE);
}
int
main (gint argc,
gchar *argv[])
{
int i;
if (argc <= 1)
{
g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
g_print ("Will update giomodule.cache in the listed directories\n");
return 1;
}
setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
/* Be defensive and ensure we're linked to GObject */
g_type_ensure (G_TYPE_OBJECT);
for (i = 1; i < argc; i++)
query_dir (argv[i]);
return 0;
}
|