ab7c29d1448216fb92c167268508e6be41af0b0f
[platform/upstream/glib.git] / gio / giomodule.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
25 #include "giomodule.h"
26
27 struct _GIOModule {
28   GTypeModule parent_instance;
29   
30   gchar       *filename;
31   GModule     *library;
32   
33   void (* load)   (GIOModule *module);
34   void (* unload) (GIOModule *module);
35 };
36
37 struct _GIOModuleClass
38 {
39   GTypeModuleClass parent_class;
40
41 };
42
43 static void      g_io_module_finalize      (GObject      *object);
44 static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
45 static void      g_io_module_unload_module (GTypeModule  *gmodule);
46
47 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
48
49 static void
50 g_io_module_class_init (GIOModuleClass *class)
51 {
52   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
53   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
54
55   object_class->finalize     = g_io_module_finalize;
56
57   type_module_class->load    = g_io_module_load_module;
58   type_module_class->unload  = g_io_module_unload_module;
59 }
60
61 static void
62 g_io_module_init (GIOModule *module)
63 {
64 }
65
66 static void
67 g_io_module_finalize (GObject *object)
68 {
69   GIOModule *module = G_IO_MODULE (object);
70
71   g_free (module->filename);
72
73   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
74 }
75
76 static gboolean
77 g_io_module_load_module (GTypeModule *gmodule)
78 {
79   GIOModule *module = G_IO_MODULE (gmodule);
80
81   if (!module->filename)
82     {
83       g_warning ("GIOModule path not set");
84       return FALSE;
85     }
86
87   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
88
89   if (!module->library)
90     {
91       g_printerr ("%s\n", g_module_error ());
92       return FALSE;
93     }
94
95   /* Make sure that the loaded library contains the required methods */
96   if (! g_module_symbol (module->library,
97                          "g_io_module_load",
98                          (gpointer *) &module->load) ||
99       ! g_module_symbol (module->library,
100                          "g_io_module_unload",
101                          (gpointer *) &module->unload))
102     {
103       g_printerr ("%s\n", g_module_error ());
104       g_module_close (module->library);
105
106       return FALSE;
107     }
108
109   /* Initialize the loaded module */
110   module->load (module);
111
112   return TRUE;
113 }
114
115 static void
116 g_io_module_unload_module (GTypeModule *gmodule)
117 {
118   GIOModule *module = G_IO_MODULE (gmodule);
119
120   module->unload (module);
121
122   g_module_close (module->library);
123   module->library = NULL;
124
125   module->load   = NULL;
126   module->unload = NULL;
127 }
128
129 /**
130  * g_io_module_new:
131  * @filename: filename of the module to load.
132  * 
133  * Returns: a new #GIOModule from given @filename, 
134  * or %NULL on error.
135  **/
136 GIOModule *
137 g_io_module_new (const gchar *filename)
138 {
139   GIOModule *module;
140
141   g_return_val_if_fail (filename != NULL, NULL);
142
143   module = g_object_new (G_IO_TYPE_MODULE, NULL);
144   module->filename = g_strdup (filename);
145
146   return module;
147 }
148
149 static gboolean
150 is_valid_module_name (const gchar *basename)
151 {
152 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
153   return
154     g_str_has_prefix (basename, "lib") &&
155     g_str_has_suffix (basename, ".so");
156 #else
157   return g_str_has_suffix (basename, ".dll");
158 #endif
159 }
160
161 static GList *
162 load_modules (const char *dirname)
163 {
164   const gchar *name;
165   GDir        *dir;
166   GList *modules;
167
168   if (!g_module_supported ())
169     return NULL;
170
171   dir = g_dir_open (dirname, 0, NULL);
172   if (!dir)
173     return NULL;
174
175   modules = NULL;
176   while ((name = g_dir_read_name (dir)))
177     {
178       if (is_valid_module_name (name))
179         {
180           GIOModule *module;
181           gchar     *path;
182
183           path = g_build_filename (dirname, name, NULL);
184           module = g_io_module_new (path);
185
186           if (!g_type_module_use (G_TYPE_MODULE (module)))
187             {
188               g_printerr ("Failed to load module: %s\n", path);
189               g_object_unref (module);
190               g_free (path);
191               continue;
192             }
193           
194           g_free (path);
195
196           g_type_module_unuse (G_TYPE_MODULE (module));
197           
198           modules = g_list_prepend (modules, module);
199         }
200     }
201   
202   g_dir_close (dir);
203
204   return modules;
205 }
206
207 G_LOCK_DEFINE_STATIC (loaded_dirs);
208 static GHashTable *loaded_dirs = NULL;
209
210 /**
211  * g_io_module_ensure_loaded:
212  * @directory: directory to ensure is loaded.
213  * 
214  **/
215 void
216 g_io_modules_ensure_loaded (const char *directory)
217 {
218   GList *modules;
219
220   g_return_if_fail (directory != NULL);
221   
222   G_LOCK (loaded_dirs);
223
224   if (loaded_dirs == NULL)
225     loaded_dirs = g_hash_table_new (g_str_hash, g_str_equal);
226
227   if (!g_hash_table_lookup_extended (loaded_dirs, directory,
228                                      NULL, NULL))
229     {
230       modules = load_modules (directory);
231       g_hash_table_insert (loaded_dirs,
232                            g_strdup (directory),
233                            modules);
234     }
235   
236   G_UNLOCK (loaded_dirs);
237 }