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"
29 * @short_description: Loadable GIO Modules
31 * Provides an interface and default functions for loading and unloading
37 GTypeModule parent_instance;
42 void (* load) (GIOModule *module);
43 void (* unload) (GIOModule *module);
46 struct _GIOModuleClass
48 GTypeModuleClass parent_class;
52 static void g_io_module_finalize (GObject *object);
53 static gboolean g_io_module_load_module (GTypeModule *gmodule);
54 static void g_io_module_unload_module (GTypeModule *gmodule);
56 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
59 g_io_module_class_init (GIOModuleClass *class)
61 GObjectClass *object_class = G_OBJECT_CLASS (class);
62 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
64 object_class->finalize = g_io_module_finalize;
66 type_module_class->load = g_io_module_load_module;
67 type_module_class->unload = g_io_module_unload_module;
71 g_io_module_init (GIOModule *module)
76 g_io_module_finalize (GObject *object)
78 GIOModule *module = G_IO_MODULE (object);
80 g_free (module->filename);
82 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
86 g_io_module_load_module (GTypeModule *gmodule)
88 GIOModule *module = G_IO_MODULE (gmodule);
90 if (!module->filename)
92 g_warning ("GIOModule path not set");
96 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
100 g_printerr ("%s\n", g_module_error ());
104 /* Make sure that the loaded library contains the required methods */
105 if (! g_module_symbol (module->library,
107 (gpointer *) &module->load) ||
108 ! g_module_symbol (module->library,
109 "g_io_module_unload",
110 (gpointer *) &module->unload))
112 g_printerr ("%s\n", g_module_error ());
113 g_module_close (module->library);
118 /* Initialize the loaded module */
119 module->load (module);
125 g_io_module_unload_module (GTypeModule *gmodule)
127 GIOModule *module = G_IO_MODULE (gmodule);
129 module->unload (module);
131 g_module_close (module->library);
132 module->library = NULL;
135 module->unload = NULL;
140 * @filename: filename of the module to load.
142 * Loads a new module into GIO.
144 * Returns: a #GIOModule from given @filename,
148 g_io_module_new (const gchar *filename)
152 g_return_val_if_fail (filename != NULL, NULL);
154 module = g_object_new (G_IO_TYPE_MODULE, NULL);
155 module->filename = g_strdup (filename);
161 is_valid_module_name (const gchar *basename)
163 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
165 g_str_has_prefix (basename, "lib") &&
166 g_str_has_suffix (basename, ".so");
168 return g_str_has_suffix (basename, ".dll");
173 load_modules (const char *dirname)
179 if (!g_module_supported ())
182 dir = g_dir_open (dirname, 0, NULL);
187 while ((name = g_dir_read_name (dir)))
189 if (is_valid_module_name (name))
194 path = g_build_filename (dirname, name, NULL);
195 module = g_io_module_new (path);
197 if (!g_type_module_use (G_TYPE_MODULE (module)))
199 g_printerr ("Failed to load module: %s\n", path);
200 g_object_unref (module);
207 g_type_module_unuse (G_TYPE_MODULE (module));
209 modules = g_list_prepend (modules, module);
218 G_LOCK_DEFINE_STATIC (loaded_dirs);
219 static GHashTable *loaded_dirs = NULL;
222 * g_io_modules_ensure_loaded:
223 * @directory: string containing a directory path.
225 * Loads all of the modules within the @directory.
228 g_io_modules_ensure_loaded (const char *directory)
232 g_return_if_fail (directory != NULL);
234 G_LOCK (loaded_dirs);
236 if (loaded_dirs == NULL)
237 loaded_dirs = g_hash_table_new (g_str_hash, g_str_equal);
239 if (!g_hash_table_lookup_extended (loaded_dirs, directory,
242 modules = load_modules (directory);
243 g_hash_table_insert (loaded_dirs,
244 g_strdup (directory),
248 G_UNLOCK (loaded_dirs);