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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "giomodule.h"
26 #include "giomodule-priv.h"
27 #include "glocalfilemonitor.h"
28 #include "glocaldirectorymonitor.h"
29 #include "gnativevolumemonitor.h"
36 * @short_description: Loadable GIO Modules
39 * Provides an interface and default functions for loading and unloading
40 * modules. This is used internally to make gio extensible, but can also
41 * be used by other to implement module loading.
46 GTypeModule parent_instance;
51 void (* load) (GIOModule *module);
52 void (* unload) (GIOModule *module);
55 struct _GIOModuleClass
57 GTypeModuleClass parent_class;
61 static void g_io_module_finalize (GObject *object);
62 static gboolean g_io_module_load_module (GTypeModule *gmodule);
63 static void g_io_module_unload_module (GTypeModule *gmodule);
65 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
68 g_io_module_class_init (GIOModuleClass *class)
70 GObjectClass *object_class = G_OBJECT_CLASS (class);
71 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
73 object_class->finalize = g_io_module_finalize;
75 type_module_class->load = g_io_module_load_module;
76 type_module_class->unload = g_io_module_unload_module;
80 g_io_module_init (GIOModule *module)
85 g_io_module_finalize (GObject *object)
87 GIOModule *module = G_IO_MODULE (object);
89 g_free (module->filename);
91 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
95 g_io_module_load_module (GTypeModule *gmodule)
97 GIOModule *module = G_IO_MODULE (gmodule);
99 if (!module->filename)
101 g_warning ("GIOModule path not set");
105 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
107 if (!module->library)
109 g_printerr ("%s\n", g_module_error ());
113 /* Make sure that the loaded library contains the required methods */
114 if (! g_module_symbol (module->library,
116 (gpointer) &module->load) ||
117 ! g_module_symbol (module->library,
118 "g_io_module_unload",
119 (gpointer) &module->unload))
121 g_printerr ("%s\n", g_module_error ());
122 g_module_close (module->library);
127 /* Initialize the loaded module */
128 module->load (module);
134 g_io_module_unload_module (GTypeModule *gmodule)
136 GIOModule *module = G_IO_MODULE (gmodule);
138 module->unload (module);
140 g_module_close (module->library);
141 module->library = NULL;
144 module->unload = NULL;
149 * @filename: filename of the shared library module.
151 * Creates a new GIOModule that will load the specific
152 * shared library when in use.
154 * Returns: a #GIOModule from given @filename,
158 g_io_module_new (const gchar *filename)
162 g_return_val_if_fail (filename != NULL, NULL);
164 module = g_object_new (G_IO_TYPE_MODULE, NULL);
165 module->filename = g_strdup (filename);
171 is_valid_module_name (const gchar *basename)
173 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
175 g_str_has_prefix (basename, "lib") &&
176 g_str_has_suffix (basename, ".so");
178 return g_str_has_suffix (basename, ".dll");
183 * g_io_modules_load_all_in_directory:
184 * @dirname: pathname for a directory containing modules to load.
186 * Loads all the modules in the the specified directory.
188 * Returns: a list of #GIOModules loaded from the directory,
189 * All the modules are loaded into memory, if you want to
190 * unload them (enabling on-demand loading) you must call
191 * g_type_module_unuse() on all the modules. Free the list
192 * with g_list_free().
195 g_io_modules_load_all_in_directory (const char *dirname)
201 if (!g_module_supported ())
204 dir = g_dir_open (dirname, 0, NULL);
209 while ((name = g_dir_read_name (dir)))
211 if (is_valid_module_name (name))
216 path = g_build_filename (dirname, name, NULL);
217 module = g_io_module_new (path);
219 if (!g_type_module_use (G_TYPE_MODULE (module)))
221 g_printerr ("Failed to load module: %s\n", path);
222 g_object_unref (module);
229 modules = g_list_prepend (modules, module);
238 G_LOCK_DEFINE_STATIC (loaded_dirs);
240 extern GType _g_inotify_directory_monitor_get_type (void);
241 extern GType _g_inotify_file_monitor_get_type (void);
242 extern GType _g_unix_volume_monitor_get_type (void);
243 extern GType _g_local_vfs_get_type (void);
246 _g_io_modules_ensure_loaded (void)
249 static gboolean loaded_dirs = FALSE;
253 GQuark private_q, name_q;
255 G_LOCK (loaded_dirs);
260 modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
262 private_q = g_quark_from_static_string ("gio-prio");
263 name_q = g_quark_from_static_string ("gio-name");
265 /* Initialize types from built-in "modules" */
266 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
267 _g_inotify_directory_monitor_get_type ();
268 _g_inotify_file_monitor_get_type ();
271 _g_unix_volume_monitor_get_type ();
273 _g_local_vfs_get_type ();
275 /* Copy over all prios to static gtype data so
276 * we can avoid loading the module again
279 types = g_type_children (G_TYPE_LOCAL_FILE_MONITOR, &n_types);
280 for (i = 0; i < n_types; i++)
282 GLocalFileMonitorClass *klass = g_type_class_ref (types[i]);
283 g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->prio));
284 g_type_class_unref (klass);
288 types = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR, &n_types);
289 for (i = 0; i < n_types; i++)
291 GLocalDirectoryMonitorClass *klass = g_type_class_ref (types[i]);
292 g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->prio));
293 g_type_class_unref (klass);
297 types = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_types);
298 for (i = 0; i < n_types; i++)
300 GNativeVolumeMonitorClass *klass = g_type_class_ref (types[i]);
301 g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->priority));
302 g_type_set_qdata (types[i], name_q, g_strdup (klass->name));
303 g_type_class_unref (klass);
307 types = g_type_children (G_TYPE_VFS, &n_types);
308 for (i = 0; i < n_types; i++)
310 GVfsClass *klass = g_type_class_ref (types[i]);
311 g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->priority));
312 g_type_set_qdata (types[i], name_q, g_strdup (klass->name));
313 g_type_class_unref (klass);
317 for (l = modules; l != NULL; l = l->next)
318 g_type_module_unuse (G_TYPE_MODULE (l->data));
320 g_list_free (modules);
323 G_UNLOCK (loaded_dirs);
326 #define __G_IO_MODULE_C__
327 #include "gioaliasdef.c"