Blame gio/gio-querymodules.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "giomodule.h"
Packit ae235b
#include "giomodule-priv.h"
Packit ae235b
Packit ae235b
#include <gstdio.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <locale.h>
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_valid_module_name (const gchar *basename)
Packit ae235b
{
Packit ae235b
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
Packit ae235b
  return
Packit ae235b
    g_str_has_prefix (basename, "lib") &&
Packit ae235b
    g_str_has_suffix (basename, ".so");
Packit ae235b
#else
Packit ae235b
  return g_str_has_suffix (basename, ".dll");
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
query_dir (const char *dirname)
Packit ae235b
{
Packit ae235b
  GString *data;
Packit ae235b
  GDir *dir;
Packit ae235b
  GList *list = NULL, *iterator = NULL;
Packit ae235b
  const char *name;
Packit ae235b
  char *cachename;
Packit ae235b
  char **(* query)  (void);
Packit ae235b
  GError *error;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  if (!g_module_supported ())
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  dir = g_dir_open (dirname, 0, &error);
Packit ae235b
  if (!dir)
Packit ae235b
    {
Packit ae235b
      g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  data = g_string_new ("");
Packit ae235b
Packit ae235b
  while ((name = g_dir_read_name (dir)))
Packit ae235b
    list = g_list_prepend (list, g_strdup (name));
Packit ae235b
Packit ae235b
  list = g_list_sort (list, (GCompareFunc) g_strcmp0);
Packit ae235b
  for (iterator = list; iterator; iterator = iterator->next)
Packit ae235b
    {
Packit ae235b
      GModule *module;
Packit ae235b
      gchar     *path;
Packit ae235b
      char **extension_points;
Packit ae235b
Packit ae235b
      name = iterator->data;
Packit ae235b
      if (!is_valid_module_name (name))
Packit ae235b
	continue;
Packit ae235b
Packit ae235b
      path = g_build_filename (dirname, name, NULL);
Packit ae235b
      module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
Packit ae235b
      g_free (path);
Packit ae235b
Packit ae235b
      if (module)
Packit ae235b
	{
Packit ae235b
	  gchar *modulename;
Packit ae235b
	  gchar *symname;
Packit ae235b
Packit ae235b
	  modulename = _g_io_module_extract_name (name);
Packit ae235b
	  symname = g_strconcat ("g_io_", modulename, "_query", NULL);
Packit ae235b
	  g_module_symbol (module, symname, (gpointer) &query);
Packit ae235b
	  g_free (symname);
Packit ae235b
	  g_free (modulename);
Packit ae235b
Packit ae235b
	  if (!query)
Packit ae235b
	    {
Packit ae235b
	      /* Fallback to old name */
Packit ae235b
	      g_module_symbol (module, "g_io_module_query", (gpointer) &query);
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  if (query)
Packit ae235b
	    {
Packit ae235b
	      extension_points = query ();
Packit ae235b
Packit ae235b
	      if (extension_points)
Packit ae235b
		{
Packit ae235b
		  g_string_append_printf (data, "%s: ", name);
Packit ae235b
Packit ae235b
		  for (i = 0; extension_points[i] != NULL; i++)
Packit ae235b
		    g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
Packit ae235b
Packit ae235b
		  g_string_append (data, "\n");
Packit ae235b
		  g_strfreev (extension_points);
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  g_module_close (module);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_dir_close (dir);
Packit ae235b
  g_list_free_full (list, g_free);
Packit ae235b
Packit ae235b
  cachename = g_build_filename (dirname, "giomodule.cache", NULL);
Packit ae235b
Packit ae235b
  if (data->len > 0)
Packit ae235b
    {
Packit ae235b
      error = NULL;
Packit ae235b
Packit ae235b
      if (!g_file_set_contents (cachename, data->str, data->len, &error))
Packit ae235b
        {
Packit ae235b
          g_printerr ("Unable to create %s: %s\n", cachename, error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (g_unlink (cachename) != 0 && errno != ENOENT)
Packit ae235b
        {
Packit ae235b
          int errsv = errno;
Packit ae235b
          g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (cachename);
Packit ae235b
  g_string_free (data, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (gint   argc,
Packit ae235b
      gchar *argv[])
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  if (argc == 1)
Packit ae235b
    {
Packit ae235b
      g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
Packit ae235b
      g_print ("Will update giomodule.cache in the listed directories\n");
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  setlocale (LC_ALL, "");
Packit ae235b
Packit ae235b
  /* Be defensive and ensure we're linked to GObject */
Packit ae235b
  g_type_ensure (G_TYPE_OBJECT);
Packit ae235b
Packit ae235b
  for (i = 1; i < argc; i++)
Packit ae235b
    query_dir (argv[i]);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}