gio-querymodules: unlink instead of writing empty cache
[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 #include <gstdio.h>
27 #include <errno.h>
28
29 static gboolean
30 is_valid_module_name (const gchar *basename)
31 {
32 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
33   return
34     g_str_has_prefix (basename, "lib") &&
35     g_str_has_suffix (basename, ".so");
36 #else
37   return g_str_has_suffix (basename, ".dll");
38 #endif
39 }
40
41 static void
42 query_dir (const char *dirname)
43 {
44   GString *data;
45   GDir *dir;
46   const char *name;
47   char *cachename;
48   char **(* query)  (void);
49   GError *error;
50   int i;
51
52   if (!g_module_supported ())
53     return;
54
55   error = NULL;
56   dir = g_dir_open (dirname, 0, &error);
57   if (!dir)
58     {
59       g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
60       g_error_free (error);
61       return;
62     }
63
64   data = g_string_new ("");
65
66   while ((name = g_dir_read_name (dir)))
67     {
68       GModule *module;
69       gchar     *path;
70       char **extension_points;
71
72       if (!is_valid_module_name (name))
73         continue;
74
75       path = g_build_filename (dirname, name, NULL);
76       module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
77       g_free (path);
78
79       if (module)
80         {
81           g_module_symbol (module, "g_io_module_query", (gpointer) &query);
82
83           if (query)
84             {
85               extension_points = query ();
86
87               if (extension_points)
88                 {
89                   g_string_append_printf (data, "%s: ", name);
90
91                   for (i = 0; extension_points[i] != NULL; i++)
92                     g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
93
94                   g_string_append (data, "\n");
95                   g_strfreev (extension_points);
96                 }
97             }
98
99           g_module_close (module);
100         }
101     }
102
103   g_dir_close (dir);
104
105   cachename = g_build_filename (dirname, "giomodule.cache", NULL);
106
107   if (data->len > 0)
108     {
109       error = NULL;
110
111       if (!g_file_set_contents (cachename, data->str, data->len, &error))
112         {
113           g_printerr ("Unable to create %s: %s\n", cachename, error->message);
114           g_error_free (error);
115         }
116     }
117   else
118     {
119       if (g_unlink (cachename) != 0 && errno != ENOENT)
120         g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errno));
121     }
122
123   g_string_free (data, TRUE);
124 }
125
126 int
127 main (gint   argc,
128       gchar *argv[])
129 {
130   int i;
131
132   g_type_init ();
133
134   if (argc == 1)
135     {
136       g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
137       g_print ("Will update giomodule.cache in the listed directories\n");
138       return 1;
139     }
140
141   for (i = 1; i < argc; i++)
142     query_dir (argv[i]);
143
144   return 0;
145 }