Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gio-querymodules.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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 of the License, or (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "giomodule.h"
25
26 static gboolean
27 is_valid_module_name (const gchar *basename)
28 {
29 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
30   return
31     g_str_has_prefix (basename, "lib") &&
32     g_str_has_suffix (basename, ".so");
33 #else
34   return g_str_has_suffix (basename, ".dll");
35 #endif
36 }
37
38 static void
39 query_dir (const char *dirname)
40 {
41   GString *data;
42   GDir *dir;
43   const char *name;
44   char *cachename;
45   char **(* query)  (void);
46   GError *error;
47   int i;
48
49   if (!g_module_supported ())
50     return;
51
52   error = NULL;
53   dir = g_dir_open (dirname, 0, &error);
54   if (!dir)
55     {
56       g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
57       g_error_free (error);
58       return;
59     }
60
61   data = g_string_new ("");
62
63   while ((name = g_dir_read_name (dir)))
64     {
65       GModule *module;
66       gchar     *path;
67       char **extension_points;
68
69       if (!is_valid_module_name (name))
70         continue;
71
72       path = g_build_filename (dirname, name, NULL);
73       module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
74       g_free (path);
75
76       if (module)
77         {
78           g_module_symbol (module, "g_io_module_query", (gpointer) &query);
79
80           if (query)
81             {
82               extension_points = query ();
83
84               if (extension_points)
85                 {
86                   g_string_append_printf (data, "%s: ", name);
87
88                   for (i = 0; extension_points[i] != NULL; i++)
89                     g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
90
91                   g_string_append (data, "\n");
92                   g_strfreev (extension_points);
93                 }
94             }
95
96           g_module_close (module);
97         }
98     }
99
100   g_dir_close (dir);
101
102   cachename = g_build_filename (dirname, "giomodule.cache", NULL);
103
104   error = NULL;
105   if (!g_file_set_contents (cachename, data->str, data->len, &error))
106     {
107       g_printerr ("Unable to create %s: %s\n", cachename, error->message);
108       g_error_free (error);
109     }
110
111   g_string_free (data, TRUE);
112 }
113
114 int
115 main (gint   argc,
116       gchar *argv[])
117 {
118   int i;
119
120   g_type_init ();
121
122   if (argc == 1)
123     {
124       g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
125       g_print ("Will update giomodule.cache in the listed directories\n");
126       return 1;
127     }
128
129   for (i = 1; i < argc; i++)
130     query_dir (argv[i]);
131
132   return 0;
133 }