Do not expand autoptr macros when running introspection
[glib.git] / gio / gio-querymodules.c
blob7abfe07fb8bf7f014b98e71dd7d49d5e8f02a8cc
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>
21 #include "config.h"
22 #include "giomodule.h"
24 #include <gstdio.h>
25 #include <errno.h>
26 #include <locale.h>
28 static gboolean
29 is_valid_module_name (const gchar *basename)
31 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
32 return
33 g_str_has_prefix (basename, "lib") &&
34 g_str_has_suffix (basename, ".so");
35 #else
36 return g_str_has_suffix (basename, ".dll");
37 #endif
40 static void
41 query_dir (const char *dirname)
43 GString *data;
44 GDir *dir;
45 GList *list = NULL, *iterator = NULL;
46 const char *name;
47 char *cachename;
48 char **(* query) (void);
49 GError *error;
50 int i;
52 if (!g_module_supported ())
53 return;
55 error = NULL;
56 dir = g_dir_open (dirname, 0, &error);
57 if (!dir)
59 g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
60 g_error_free (error);
61 return;
64 data = g_string_new ("");
66 while ((name = g_dir_read_name (dir)))
67 list = g_list_prepend (list, g_strdup (name));
69 list = g_list_sort (list, (GCompareFunc) g_strcmp0);
70 for (iterator = list; iterator; iterator = iterator->next)
72 GModule *module;
73 gchar *path;
74 char **extension_points;
76 name = iterator->data;
77 if (!is_valid_module_name (name))
78 continue;
80 path = g_build_filename (dirname, name, NULL);
81 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
82 g_free (path);
84 if (module)
86 g_module_symbol (module, "g_io_module_query", (gpointer) &query);
88 if (query)
90 extension_points = query ();
92 if (extension_points)
94 g_string_append_printf (data, "%s: ", name);
96 for (i = 0; extension_points[i] != NULL; i++)
97 g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
99 g_string_append (data, "\n");
100 g_strfreev (extension_points);
104 g_module_close (module);
108 g_dir_close (dir);
109 g_list_free_full (list, g_free);
111 cachename = g_build_filename (dirname, "giomodule.cache", NULL);
113 if (data->len > 0)
115 error = NULL;
117 if (!g_file_set_contents (cachename, data->str, data->len, &error))
119 g_printerr ("Unable to create %s: %s\n", cachename, error->message);
120 g_error_free (error);
123 else
125 if (g_unlink (cachename) != 0 && errno != ENOENT)
127 int errsv = errno;
128 g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
132 g_free (cachename);
133 g_string_free (data, TRUE);
137 main (gint argc,
138 gchar *argv[])
140 int i;
142 if (argc == 1)
144 g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
145 g_print ("Will update giomodule.cache in the listed directories\n");
146 return 1;
149 setlocale (LC_ALL, "");
151 /* Be defensive and ensure we're linked to GObject */
152 g_type_ensure (G_TYPE_OBJECT);
154 for (i = 1; i < argc; i++)
155 query_dir (argv[i]);
157 return 0;