Remove checking for and mentions of fork where possible.
[platform/upstream/gstreamer.git] / gst / gstregistry.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 David A. Schleef <ds@schleef.org>
5  *
6  * gstregistry.c: handle registry
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstregistry
26  * @short_description: Abstract base class for management of #GstPlugin objects
27  * @see_also: #GstPlugin, #GstPluginFeature
28  *
29  * One registry holds the metadata of a set of plugins.
30  *
31  * <emphasis role="bold">Design:</emphasis>
32  *
33  * The #GstRegistry object is a list of plugins and some functions for dealing
34  * with them. #GstPlugins are matched 1-1 with a file on disk, and may or may
35  * not be loaded at a given time. There may be multiple #GstRegistry objects,
36  * but the "default registry" is the only object that has any meaning to the
37  * core.
38  *
39  * The registry.xml file is actually a cache of plugin information. This is
40  * unlike versions prior to 0.10, where the registry file was the primary source
41  * of plugin information, and was created by the gst-register command.
42  *
43  * The primary source, at all times, of plugin information is each plugin file
44  * itself. Thus, if an application wants information about a particular plugin,
45  * or wants to search for a feature that satisfies given criteria, the primary
46  * means of doing so is to load every plugin and look at the resulting
47  * information that is gathered in the default registry. Clearly, this is a time
48  * consuming process, so we cache information in the registry.xml file.
49  *
50  * On startup, plugins are searched for in the plugin search path. This path can
51  * be set directly using the %GST_PLUGIN_PATH environment variable. The registry
52  * file is loaded from ~/.gstreamer-$GST_MAJORMINOR/registry-$ARCH.xml or the
53  * file listed in the %GST_REGISTRY env var. The only reason to change the
54  * registry location is for testing.
55  *
56  * For each plugin that is found in the plugin search path, there could be 3
57  * possibilities for cached information:
58  * <itemizedlist>
59  *   <listitem>
60  *     <para>the cache may not contain information about a given file.</para>
61  *   </listitem>
62  *   <listitem>
63  *     <para>the cache may have stale information.</para>
64  *   </listitem>
65  *   <listitem>
66  *     <para>the cache may have current information.</para>
67  *   </listitem>
68  * </itemizedlist>
69  *
70  * In the first two cases, the plugin is loaded and the cache updated. In
71  * addition to these cases, the cache may have entries for plugins that are not
72  * relevant to the current process. These are marked as not available to the
73  * current process. If the cache is updated for whatever reason, it is marked
74  * dirty.
75  *
76  * A dirty cache is written out at the end of initialization. Each entry is
77  * checked to make sure the information is minimally valid. If not, the entry is
78  * simply dropped.
79  *
80  * <emphasis role="bold">Implementation notes:</emphasis>
81  *
82  * The "cache" and "default registry" are different concepts and can represent
83  * different sets of plugins. For various reasons, at init time, the cache is
84  * stored in the default registry, and plugins not relevant to the current
85  * process are marked with the %GST_PLUGIN_FLAG_CACHED bit. These plugins are
86  * removed at the end of intitialization.
87  */
88
89 #ifdef HAVE_CONFIG_H
90 #include "config.h"
91 #endif
92 #include "gstconfig.h"
93 #include "gst_private.h"
94 #include <glib.h>
95 #include <sys/types.h>
96 #include <sys/stat.h>
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
100 #include <errno.h>
101 #include <stdio.h>
102 #include <string.h>
103
104 /* For g_stat () */
105 #include <glib/gstdio.h>
106
107 #include "gstinfo.h"
108 #include "gsterror.h"
109 #include "gstregistry.h"
110 #include "gstmarshal.h"
111 #include "gstfilter.h"
112
113 #include "gstpluginloader.h"
114
115 #include "gst-i18n-lib.h"
116
117 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
118
119 /* the one instance of the default registry and the mutex protecting the
120  * variable. */
121 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
122 static GstRegistry *_gst_registry_default = NULL;
123
124 /* defaults */
125 #define DEFAULT_FORK TRUE
126
127 /* control the behaviour of registry rebuild */
128 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
129 /* List of plugins that need preloading/reloading after scanning registry */
130 extern GSList *_priv_gst_preload_plugins;
131
132 #ifndef GST_DISABLE_REGISTRY
133 /*set to TRUE when registry needn't to be updated */
134 gboolean _priv_gst_disable_registry_update = FALSE;
135 extern GList *_priv_gst_plugin_paths;
136
137 /* Set to TRUE when the registry cache should be disabled */
138 gboolean _gst_disable_registry_cache = FALSE;
139 #endif
140
141 /* Element signals and args */
142 enum
143 {
144   PLUGIN_ADDED,
145   FEATURE_ADDED,
146   LAST_SIGNAL
147 };
148
149 static void gst_registry_finalize (GObject * object);
150
151 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
152
153 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
154     registry, const char *name);
155 static GstPlugin *gst_registry_lookup_locked (GstRegistry * registry,
156     const char *filename);
157
158 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
159 static GstObjectClass *parent_class = NULL;
160
161 static void
162 gst_registry_class_init (GstRegistryClass * klass)
163 {
164   GObjectClass *gobject_class;
165
166   gobject_class = (GObjectClass *) klass;
167
168   parent_class = g_type_class_peek_parent (klass);
169
170   /**
171    * GstRegistry::plugin-added:
172    * @registry: the registry that emitted the signal
173    * @plugin: the plugin that has been added
174    *
175    * Signals that a plugin has been added to the registry (possibly
176    * replacing a previously-added one by the same name)
177    */
178   gst_registry_signals[PLUGIN_ADDED] =
179       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
180       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
181       NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
182
183   /**
184    * GstRegistry::feature-added:
185    * @registry: the registry that emitted the signal
186    * @feature: the feature that has been added
187    *
188    * Signals that a feature has been added to the registry (possibly
189    * replacing a previously-added one by the same name)
190    */
191   gst_registry_signals[FEATURE_ADDED] =
192       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
193       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
194       NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
195
196   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_registry_finalize);
197 }
198
199 static void
200 gst_registry_init (GstRegistry * registry)
201 {
202   registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
203 }
204
205 static void
206 gst_registry_finalize (GObject * object)
207 {
208   GstRegistry *registry = GST_REGISTRY (object);
209   GList *plugins, *p;
210   GList *features, *f;
211
212   plugins = registry->plugins;
213   registry->plugins = NULL;
214
215   GST_DEBUG_OBJECT (registry, "registry finalize");
216   p = plugins;
217   while (p) {
218     GstPlugin *plugin = p->data;
219
220     if (plugin) {
221       GST_LOG_OBJECT (registry, "removing plugin %s",
222           gst_plugin_get_name (plugin));
223       gst_object_unref (plugin);
224     }
225     p = g_list_next (p);
226   }
227   g_list_free (plugins);
228
229   features = registry->features;
230   registry->features = NULL;
231
232   f = features;
233   while (f) {
234     GstPluginFeature *feature = f->data;
235
236     if (feature) {
237       GST_LOG_OBJECT (registry, "removing feature %p (%s)",
238           feature, gst_plugin_feature_get_name (feature));
239       gst_object_unref (feature);
240     }
241     f = g_list_next (f);
242   }
243   g_list_free (features);
244
245   g_hash_table_destroy (registry->feature_hash);
246   registry->feature_hash = NULL;
247
248   G_OBJECT_CLASS (parent_class)->finalize (object);
249 }
250
251 /**
252  * gst_registry_get_default:
253  *
254  * Retrieves the default registry. The caller does not own a reference on the
255  * registry, as it is alive as long as GStreamer is initialized.
256  *
257  * Returns: The default #GstRegistry.
258  */
259 GstRegistry *
260 gst_registry_get_default (void)
261 {
262   GstRegistry *registry;
263
264   g_static_mutex_lock (&_gst_registry_mutex);
265   if (G_UNLIKELY (!_gst_registry_default)) {
266     _gst_registry_default = g_object_new (GST_TYPE_REGISTRY, NULL);
267     gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
268   }
269   registry = _gst_registry_default;
270   g_static_mutex_unlock (&_gst_registry_mutex);
271
272   return registry;
273 }
274
275 /**
276  * gst_registry_add_path:
277  * @registry: the registry to add the path to
278  * @path: the path to add to the registry
279  *
280  * Add the given path to the registry. The syntax of the
281  * path is specific to the registry. If the path has already been
282  * added, do nothing.
283  */
284 void
285 gst_registry_add_path (GstRegistry * registry, const gchar * path)
286 {
287   g_return_if_fail (GST_IS_REGISTRY (registry));
288   g_return_if_fail (path != NULL);
289
290   if (strlen (path) == 0)
291     goto empty_path;
292
293   GST_OBJECT_LOCK (registry);
294   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
295     goto was_added;
296
297   GST_INFO ("Adding plugin path: \"%s\"", path);
298   registry->paths = g_list_append (registry->paths, g_strdup (path));
299   GST_OBJECT_UNLOCK (registry);
300
301   return;
302
303 empty_path:
304   {
305     GST_INFO ("Ignoring empty plugin path");
306     return;
307   }
308 was_added:
309   {
310     g_warning ("path %s already added to registry", path);
311     GST_OBJECT_UNLOCK (registry);
312     return;
313   }
314 }
315
316 /**
317  * gst_registry_get_path_list:
318  * @registry: the registry to get the pathlist of
319  *
320  * Get the list of paths for the given registry.
321  *
322  * Returns: A Glist of paths as strings. g_list_free after use.
323  *
324  * MT safe.
325  */
326 GList *
327 gst_registry_get_path_list (GstRegistry * registry)
328 {
329   GList *list;
330
331   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
332
333   GST_OBJECT_LOCK (registry);
334   /* We don't need to copy the strings, because they won't be deleted
335    * as long as the GstRegistry is around */
336   list = g_list_copy (registry->paths);
337   GST_OBJECT_UNLOCK (registry);
338
339   return list;
340 }
341
342
343 /**
344  * gst_registry_add_plugin:
345  * @registry: the registry to add the plugin to
346  * @plugin: the plugin to add
347  *
348  * Add the plugin to the registry. The plugin-added signal will be emitted.
349  * This function will sink @plugin.
350  *
351  * Returns: TRUE on success.
352  *
353  * MT safe.
354  */
355 gboolean
356 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
357 {
358   GstPlugin *existing_plugin;
359
360   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
361   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
362
363   GST_OBJECT_LOCK (registry);
364   existing_plugin = gst_registry_lookup_locked (registry, plugin->filename);
365   if (G_UNLIKELY (existing_plugin)) {
366     GST_DEBUG_OBJECT (registry,
367         "Replacing existing plugin %p with new plugin %p for filename \"%s\"",
368         existing_plugin, plugin, GST_STR_NULL (plugin->filename));
369     registry->plugins = g_list_remove (registry->plugins, existing_plugin);
370     gst_object_unref (existing_plugin);
371   }
372
373   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
374       plugin, GST_STR_NULL (plugin->filename));
375
376   registry->plugins = g_list_prepend (registry->plugins, plugin);
377
378   gst_object_ref_sink (plugin);
379   GST_OBJECT_UNLOCK (registry);
380
381   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
382       GST_STR_NULL (plugin->filename));
383   g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
384
385   return TRUE;
386 }
387
388 static void
389 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
390     GstPlugin * plugin)
391 {
392   GList *f;
393   const gchar *name;
394
395   g_return_if_fail (GST_IS_REGISTRY (registry));
396   g_return_if_fail (GST_IS_PLUGIN (plugin));
397
398   name = gst_plugin_get_name (plugin);
399
400   /* Remove all features for this plugin */
401   f = registry->features;
402   while (f != NULL) {
403     GList *next = g_list_next (f);
404     GstPluginFeature *feature = f->data;
405
406     if (G_UNLIKELY (feature && !strcmp (feature->plugin_name, name))) {
407       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
408           feature, gst_plugin_feature_get_name (feature), name);
409
410       registry->features = g_list_delete_link (registry->features, f);
411       g_hash_table_remove (registry->feature_hash, feature->name);
412       gst_object_unref (feature);
413     }
414     f = next;
415   }
416 }
417
418 /**
419  * gst_registry_remove_plugin:
420  * @registry: the registry to remove the plugin from
421  * @plugin: the plugin to remove
422  *
423  * Remove the plugin from the registry.
424  *
425  * MT safe.
426  */
427 void
428 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
429 {
430   g_return_if_fail (GST_IS_REGISTRY (registry));
431   g_return_if_fail (GST_IS_PLUGIN (plugin));
432
433   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
434       plugin, gst_plugin_get_name (plugin));
435
436   GST_OBJECT_LOCK (registry);
437   registry->plugins = g_list_remove (registry->plugins, plugin);
438   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
439   GST_OBJECT_UNLOCK (registry);
440   gst_object_unref (plugin);
441 }
442
443 /**
444  * gst_registry_add_feature:
445  * @registry: the registry to add the plugin to
446  * @feature: the feature to add
447  *
448  * Add the feature to the registry. The feature-added signal will be emitted.
449  * This function sinks @feature.
450  *
451  * Returns: TRUE on success.
452  *
453  * MT safe.
454  */
455 gboolean
456 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
457 {
458   GstPluginFeature *existing_feature;
459
460   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
461   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
462   g_return_val_if_fail (feature->name != NULL, FALSE);
463   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
464
465   GST_OBJECT_LOCK (registry);
466   existing_feature = gst_registry_lookup_feature_locked (registry,
467       feature->name);
468   if (G_UNLIKELY (existing_feature)) {
469     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
470         existing_feature, feature->name);
471     /* Remove the existing feature from the list now, before we insert the new
472      * one, but don't unref yet because the hash is still storing a reference to
473      * it. */
474     registry->features = g_list_remove (registry->features, existing_feature);
475   }
476
477   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
478
479   registry->features = g_list_prepend (registry->features, feature);
480   g_hash_table_replace (registry->feature_hash, feature->name, feature);
481
482   if (G_UNLIKELY (existing_feature)) {
483     /* We unref now. No need to remove the feature name from the hash table, it
484      * got replaced by the new feature */
485     gst_object_unref (existing_feature);
486   }
487
488   gst_object_ref_sink (feature);
489   GST_OBJECT_UNLOCK (registry);
490
491   GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
492   g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
493
494   return TRUE;
495 }
496
497 /**
498  * gst_registry_remove_feature:
499  * @registry: the registry to remove the feature from
500  * @feature: the feature to remove
501  *
502  * Remove the feature from the registry.
503  *
504  * MT safe.
505  */
506 void
507 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
508 {
509   g_return_if_fail (GST_IS_REGISTRY (registry));
510   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
511
512   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
513       feature, gst_plugin_feature_get_name (feature));
514
515   GST_OBJECT_LOCK (registry);
516   registry->features = g_list_remove (registry->features, feature);
517   g_hash_table_remove (registry->feature_hash, feature->name);
518   GST_OBJECT_UNLOCK (registry);
519   gst_object_unref (feature);
520 }
521
522 /**
523  * gst_registry_plugin_filter:
524  * @registry: registry to query
525  * @filter: the filter to use
526  * @first: only return first match
527  * @user_data: user data passed to the filter function
528  *
529  * Runs a filter against all plugins in the registry and returns a #GList with
530  * the results. If the first flag is set, only the first match is
531  * returned (as a list with a single object).
532  * Every plugin is reffed; use gst_plugin_list_free() after use, which
533  * will unref again.
534  *
535  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
536  *
537  * MT safe.
538  */
539 GList *
540 gst_registry_plugin_filter (GstRegistry * registry,
541     GstPluginFilter filter, gboolean first, gpointer user_data)
542 {
543   GList *list;
544   GList *g;
545
546   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
547
548   GST_OBJECT_LOCK (registry);
549   list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
550       user_data);
551   for (g = list; g; g = g->next) {
552     gst_object_ref (GST_PLUGIN_CAST (g->data));
553   }
554   GST_OBJECT_UNLOCK (registry);
555
556   return list;
557 }
558
559 /**
560  * gst_registry_feature_filter:
561  * @registry: registry to query
562  * @filter: the filter to use
563  * @first: only return first match
564  * @user_data: user data passed to the filter function
565  *
566  * Runs a filter against all features of the plugins in the registry
567  * and returns a GList with the results.
568  * If the first flag is set, only the first match is
569  * returned (as a list with a single object).
570  *
571  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
572  * after usage.
573  *
574  * MT safe.
575  */
576 GList *
577 gst_registry_feature_filter (GstRegistry * registry,
578     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
579 {
580   GList *list;
581   GList *g;
582
583   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
584
585   GST_OBJECT_LOCK (registry);
586   list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
587       user_data);
588   for (g = list; g; g = g->next) {
589     gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
590   }
591   GST_OBJECT_UNLOCK (registry);
592
593   return list;
594 }
595
596 /**
597  * gst_registry_find_plugin:
598  * @registry: the registry to search
599  * @name: the plugin name to find
600  *
601  * Find the plugin with the given name in the registry.
602  * The plugin will be reffed; caller is responsible for unreffing.
603  *
604  * Returns: The plugin with the given name or NULL if the plugin was not found.
605  * gst_object_unref() after usage.
606  *
607  * MT safe.
608  */
609 GstPlugin *
610 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
611 {
612   GList *walk;
613   GstPlugin *result = NULL;
614
615   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
616   g_return_val_if_fail (name != NULL, NULL);
617
618   walk = gst_registry_plugin_filter (registry,
619       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
620   if (walk) {
621     result = GST_PLUGIN_CAST (walk->data);
622
623     gst_object_ref (result);
624     gst_plugin_list_free (walk);
625   }
626
627   return result;
628 }
629
630 /**
631  * gst_registry_find_feature:
632  * @registry: the registry to search
633  * @name: the pluginfeature name to find
634  * @type: the pluginfeature type to find
635  *
636  * Find the pluginfeature with the given name and type in the registry.
637  *
638  * Returns: The pluginfeature with the given name and type or NULL
639  * if the plugin was not found. gst_object_unref() after usage.
640  *
641  * MT safe.
642  */
643 GstPluginFeature *
644 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
645     GType type)
646 {
647   GstPluginFeature *feature = NULL;
648   GList *walk;
649   GstTypeNameData data;
650
651   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
652   g_return_val_if_fail (name != NULL, NULL);
653   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
654
655   data.name = name;
656   data.type = type;
657
658   walk = gst_registry_feature_filter (registry,
659       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
660       TRUE, &data);
661
662   if (walk) {
663     feature = GST_PLUGIN_FEATURE_CAST (walk->data);
664
665     gst_object_ref (feature);
666     gst_plugin_feature_list_free (walk);
667   }
668
669   return feature;
670 }
671
672 /**
673  * gst_registry_get_feature_list:
674  * @registry: a #GstRegistry
675  * @type: a #GType.
676  *
677  * Retrieves a #GList of #GstPluginFeature of @type.
678  *
679  * Returns: a #GList of #GstPluginFeature of @type. Use
680  * gst_plugin_feature_list_free() after usage.
681  *
682  * MT safe.
683  */
684 GList *
685 gst_registry_get_feature_list (GstRegistry * registry, GType type)
686 {
687   GstTypeNameData data;
688
689   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
690   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
691
692   data.type = type;
693   data.name = NULL;
694
695   return gst_registry_feature_filter (registry,
696       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
697       FALSE, &data);
698 }
699
700 /**
701  * gst_registry_get_plugin_list:
702  * @registry: the registry to search
703  *
704  * Get a copy of all plugins registered in the given registry. The refcount
705  * of each element in the list in incremented.
706  *
707  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
708  *
709  * MT safe.
710  */
711 GList *
712 gst_registry_get_plugin_list (GstRegistry * registry)
713 {
714   GList *list;
715   GList *g;
716
717   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
718
719   GST_OBJECT_LOCK (registry);
720   list = g_list_copy (registry->plugins);
721   for (g = list; g; g = g->next) {
722     gst_object_ref (GST_PLUGIN_CAST (g->data));
723   }
724   GST_OBJECT_UNLOCK (registry);
725
726   return list;
727 }
728
729 static GstPluginFeature *
730 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
731 {
732   if (G_UNLIKELY (name == NULL))
733     return NULL;
734
735   return g_hash_table_lookup (registry->feature_hash, name);
736 }
737
738 /**
739  * gst_registry_lookup_feature:
740  * @registry: a #GstRegistry
741  * @name: a #GstPluginFeature name
742  *
743  * Find a #GstPluginFeature with @name in @registry.
744  *
745  * Returns: a #GstPluginFeature with its refcount incremented, use
746  * gst_object_unref() after usage.
747  *
748  * MT safe.
749  */
750 GstPluginFeature *
751 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
752 {
753   GstPluginFeature *feature;
754
755   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
756   g_return_val_if_fail (name != NULL, NULL);
757
758   GST_OBJECT_LOCK (registry);
759   feature = gst_registry_lookup_feature_locked (registry, name);
760   if (feature)
761     gst_object_ref (feature);
762   GST_OBJECT_UNLOCK (registry);
763
764   return feature;
765 }
766
767 static GstPlugin *
768 gst_registry_lookup_locked (GstRegistry * registry, const char *filename)
769 {
770   GList *g;
771   GstPlugin *plugin;
772   gchar *basename;
773
774   if (G_UNLIKELY (filename == NULL))
775     return NULL;
776
777   basename = g_path_get_basename (filename);
778   /* FIXME: use GTree speed up lookups */
779   for (g = registry->plugins; g; g = g_list_next (g)) {
780     plugin = GST_PLUGIN_CAST (g->data);
781     if (G_UNLIKELY (plugin->basename
782             && strcmp (basename, plugin->basename) == 0)) {
783       g_free (basename);
784       return plugin;
785     }
786   }
787
788   g_free (basename);
789   return NULL;
790 }
791
792 /**
793  * gst_registry_lookup:
794  * @registry: the registry to look up in
795  * @filename: the name of the file to look up
796  *
797  * Look up a plugin in the given registry with the given filename.
798  * If found, plugin is reffed.
799  *
800  * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
801  * after usage.
802  */
803 GstPlugin *
804 gst_registry_lookup (GstRegistry * registry, const char *filename)
805 {
806   GstPlugin *plugin;
807
808   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
809   g_return_val_if_fail (filename != NULL, NULL);
810
811   GST_OBJECT_LOCK (registry);
812   plugin = gst_registry_lookup_locked (registry, filename);
813   if (plugin)
814     gst_object_ref (plugin);
815   GST_OBJECT_UNLOCK (registry);
816
817   return plugin;
818 }
819
820 typedef enum
821 {
822   REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
823   REGISTRY_SCAN_HELPER_DISABLED,
824   REGISTRY_SCAN_HELPER_RUNNING
825 } GstRegistryScanHelperState;
826
827 typedef struct
828 {
829   GstRegistry *registry;
830   GstRegistryScanHelperState helper_state;
831   GstPluginLoader *helper;
832   gboolean changed;
833 } GstRegistryScanContext;
834
835 static void
836 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
837 {
838   gboolean do_fork;
839
840   context->registry = registry;
841
842   /* see if forking is enabled and set up the scan helper state accordingly */
843   do_fork = _gst_enable_registry_fork;
844   if (do_fork) {
845     const gchar *fork_env;
846
847     /* forking enabled, see if it is disabled with an env var */
848     if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
849       /* fork enabled for any value different from "no" */
850       do_fork = strcmp (fork_env, "no") != 0;
851     }
852   }
853
854   if (do_fork)
855     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
856   else
857     context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
858
859   context->helper = NULL;
860   context->changed = FALSE;
861 }
862
863 static void
864 clear_scan_context (GstRegistryScanContext * context)
865 {
866   if (context->helper) {
867     context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
868     context->helper = NULL;
869   }
870 }
871
872 static gboolean
873 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
874     const gchar * filename, off_t file_size, time_t file_mtime)
875 {
876   gboolean changed = FALSE;
877   GstPlugin *newplugin = NULL;
878
879   /* Have a plugin to load - see if the scan-helper needs starting */
880   if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
881     GST_DEBUG ("Starting plugin scanner for file %s", filename);
882     context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
883     if (context->helper != NULL)
884       context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
885     else
886       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
887   }
888
889   if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
890     GST_DEBUG ("Using scan-helper to load plugin %s", filename);
891     if (!_priv_gst_plugin_loader_funcs.load (context->helper,
892             filename, file_size, file_mtime)) {
893       g_warning ("External plugin loader failed...");
894       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
895     }
896   } else {
897     /* Load plugin the old fashioned way... */
898
899     /* We don't use a GError here because a failure to load some shared
900      * objects as plugins is normal (particularly in the uninstalled case)
901      */
902     newplugin = gst_plugin_load_file (filename, NULL);
903   }
904
905   if (newplugin) {
906     GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
907         newplugin);
908     newplugin->registered = TRUE;
909     gst_object_unref (newplugin);
910     changed = TRUE;
911   }
912
913   return changed;
914 }
915
916 static gboolean
917 gst_registry_scan_path_level (GstRegistryScanContext * context,
918     const gchar * path, int level)
919 {
920   GDir *dir;
921   const gchar *dirent;
922   gchar *filename;
923   GstPlugin *plugin;
924   gboolean changed = FALSE;
925
926   dir = g_dir_open (path, 0, NULL);
927   if (!dir)
928     return FALSE;
929
930   while ((dirent = g_dir_read_name (dir))) {
931     struct stat file_status;
932
933     filename = g_build_filename (path, dirent, NULL);
934     if (g_stat (filename, &file_status) < 0) {
935       /* Plugin will be removed from cache after the scan completes if it
936        * is still marked 'cached' */
937       g_free (filename);
938       continue;
939     }
940
941     if (file_status.st_mode & S_IFDIR) {
942       /* skip the .debug directory, these contain elf files that are not
943        * useful or worse, can crash dlopen () */
944       if (g_str_equal (dirent, ".debug") || g_str_equal (dirent, ".git")) {
945         GST_LOG_OBJECT (context->registry, "ignoring .debug or .git directory");
946         g_free (filename);
947         continue;
948       }
949       /* FIXME 0.11: Don't recurse into directories, this behaviour
950        * is inconsistent with other PATH environment variables
951        */
952       if (level > 0) {
953         GST_LOG_OBJECT (context->registry, "recursing into directory %s",
954             filename);
955         changed |= gst_registry_scan_path_level (context, filename, level - 1);
956       } else {
957         GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
958             "recursion level too deep", filename);
959       }
960       g_free (filename);
961       continue;
962     }
963     if (!(file_status.st_mode & S_IFREG)) {
964       GST_LOG_OBJECT (context->registry, "%s is not a regular file, ignoring",
965           filename);
966       g_free (filename);
967       continue;
968     }
969     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
970 #ifdef GST_EXTRA_MODULE_SUFFIX
971         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
972 #endif
973         ) {
974       GST_LOG_OBJECT (context->registry,
975           "extension is not recognized as module file, ignoring file %s",
976           filename);
977       g_free (filename);
978       continue;
979     }
980
981     GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
982         filename);
983
984     /* plug-ins are considered unique by basename; if the given name
985      * was already seen by the registry, we ignore it */
986     plugin = gst_registry_lookup (context->registry, filename);
987     if (plugin) {
988       gboolean env_vars_changed, deps_changed = FALSE;
989
990       if (plugin->registered) {
991         GST_DEBUG_OBJECT (context->registry,
992             "plugin already registered from path \"%s\"",
993             GST_STR_NULL (plugin->filename));
994         g_free (filename);
995         gst_object_unref (plugin);
996         continue;
997       }
998
999       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1000
1001       /* If a file with a certain basename is seen on a different path,
1002        * update the plugin to ensure the registry cache will reflect up
1003        * to date information */
1004
1005       if (plugin->file_mtime == file_status.st_mtime &&
1006           plugin->file_size == file_status.st_size && !env_vars_changed &&
1007           !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1008           !strcmp (plugin->filename, filename)) {
1009         GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1010         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1011         GST_LOG_OBJECT (context->registry,
1012             "marking plugin %p as registered as %s", plugin, filename);
1013         plugin->registered = TRUE;
1014       } else {
1015         GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1016             filename);
1017         GST_DEBUG_OBJECT (context->registry, "mtime %ld != %ld or size %"
1018             G_GINT64_FORMAT " != %" G_GINT64_FORMAT " or external dependency "
1019             "env_vars changed: %d or external dependencies changed: %d"
1020             " or old path %s != new path %s",
1021             plugin->file_mtime, file_status.st_mtime,
1022             (gint64) plugin->file_size, (gint64) file_status.st_size,
1023             env_vars_changed, deps_changed, plugin->filename, filename);
1024         gst_registry_remove_plugin (context->registry, plugin);
1025         changed |= gst_registry_scan_plugin_file (context, filename,
1026             file_status.st_size, file_status.st_mtime);
1027       }
1028       gst_object_unref (plugin);
1029
1030     } else {
1031       GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1032           filename);
1033       changed |= gst_registry_scan_plugin_file (context, filename,
1034           file_status.st_size, file_status.st_mtime);
1035     }
1036
1037     g_free (filename);
1038   }
1039
1040   g_dir_close (dir);
1041
1042   return changed;
1043 }
1044
1045 static gboolean
1046 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1047     const gchar * path)
1048 {
1049   gboolean changed;
1050
1051   GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1052   changed = gst_registry_scan_path_level (context, path, 10);
1053
1054   GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1055       changed);
1056   return changed;
1057 }
1058
1059 /**
1060  * gst_registry_scan_path:
1061  * @registry: the registry to add found plugins to
1062  * @path: the path to scan
1063  *
1064  * Scan the given path for plugins to add to the registry. The syntax of the
1065  * path is specific to the registry.
1066  *
1067  * Returns: %TRUE if registry changed
1068  */
1069 gboolean
1070 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1071 {
1072   GstRegistryScanContext context;
1073   gboolean result;
1074
1075   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1076   g_return_val_if_fail (path != NULL, FALSE);
1077
1078   init_scan_context (&context, registry);
1079
1080   result = gst_registry_scan_path_internal (&context, path);
1081
1082   clear_scan_context (&context);
1083   result |= context.changed;
1084
1085   return result;
1086 }
1087
1088 static gboolean
1089 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1090     gpointer user_data)
1091 {
1092   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1093 }
1094
1095 /**
1096  * gst_registry_get_feature_list_by_plugin:
1097  * @registry: a #GstRegistry.
1098  * @name: a plugin name.
1099  *
1100  * Retrieves a #GList of features of the plugin with name @name.
1101  *
1102  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1103  * after usage.
1104  */
1105 GList *
1106 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1107     const gchar * name)
1108 {
1109   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1110   g_return_val_if_fail (name != NULL, NULL);
1111
1112   return gst_registry_feature_filter (registry,
1113       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1114 }
1115
1116 /* Unref and delete the default registry */
1117 void
1118 _priv_gst_registry_cleanup ()
1119 {
1120   GstRegistry *registry;
1121
1122   g_static_mutex_lock (&_gst_registry_mutex);
1123   if ((registry = _gst_registry_default) != NULL) {
1124     _gst_registry_default = NULL;
1125   }
1126   g_static_mutex_unlock (&_gst_registry_mutex);
1127
1128   /* unref outside of the lock because we can. */
1129   if (registry)
1130     gst_object_unref (registry);
1131 }
1132
1133 /**
1134  * gst_default_registry_check_feature_version:
1135  * @feature_name: the name of the feature (e.g. "oggdemux")
1136  * @min_major: the minimum major version number
1137  * @min_minor: the minimum minor version number
1138  * @min_micro: the minimum micro version number
1139  *
1140  * Checks whether a plugin feature by the given name exists in the
1141  * default registry and whether its version is at least the
1142  * version required.
1143  *
1144  * Returns: #TRUE if the feature could be found and the version is
1145  * the same as the required version or newer, and #FALSE otherwise.
1146  */
1147 gboolean
1148 gst_default_registry_check_feature_version (const gchar * feature_name,
1149     guint min_major, guint min_minor, guint min_micro)
1150 {
1151   GstPluginFeature *feature;
1152   GstRegistry *registry;
1153   gboolean ret = FALSE;
1154
1155   g_return_val_if_fail (feature_name != NULL, FALSE);
1156
1157   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1158
1159   registry = gst_registry_get_default ();
1160   feature = gst_registry_lookup_feature (registry, feature_name);
1161   if (feature) {
1162     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1163         min_micro);
1164     gst_object_unref (feature);
1165   } else {
1166     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1167   }
1168
1169   return ret;
1170 }
1171
1172 static void
1173 load_plugin_func (gpointer data, gpointer user_data)
1174 {
1175   GstPlugin *plugin;
1176   const gchar *filename;
1177   GError *err = NULL;
1178
1179   filename = (const gchar *) data;
1180   GST_DEBUG ("Pre-loading plugin %s", filename);
1181
1182   plugin = gst_plugin_load_file (filename, &err);
1183
1184   if (plugin) {
1185     GST_INFO ("Loaded plugin: \"%s\"", filename);
1186
1187     gst_default_registry_add_plugin (plugin);
1188   } else {
1189     if (err) {
1190       /* Report error to user, and free error */
1191       GST_ERROR ("Failed to load plugin: %s", err->message);
1192       g_error_free (err);
1193     } else {
1194       GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1195     }
1196   }
1197 }
1198
1199 #ifndef GST_DISABLE_REGISTRY
1200 /* Unref all plugins marked 'cached', to clear old plugins that no
1201  * longer exist. Returns TRUE if any plugins were removed */
1202 static gboolean
1203 gst_registry_remove_cache_plugins (GstRegistry * registry)
1204 {
1205   GList *g;
1206   GList *g_next;
1207   GstPlugin *plugin;
1208   gboolean changed = FALSE;
1209
1210   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1211
1212   GST_OBJECT_LOCK (registry);
1213
1214   GST_DEBUG_OBJECT (registry, "removing cached plugins");
1215   g = registry->plugins;
1216   while (g) {
1217     g_next = g->next;
1218     plugin = g->data;
1219     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1220       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1221           GST_STR_NULL (plugin->filename));
1222       registry->plugins = g_list_delete_link (registry->plugins, g);
1223       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1224       gst_object_unref (plugin);
1225       changed = TRUE;
1226     }
1227     g = g_next;
1228   }
1229
1230   GST_OBJECT_UNLOCK (registry);
1231
1232   return changed;
1233 }
1234
1235 typedef enum
1236 {
1237   REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1238   REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1239   REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1240 } GstRegistryScanAndUpdateResult;
1241
1242 /*
1243  * scan_and_update_registry:
1244  * @default_registry: the #GstRegistry
1245  * @registry_file: registry filename
1246  * @write_changes: write registry if it has changed?
1247  *
1248  * Scans for registry changes and eventually updates the registry cache.
1249  *
1250  * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1251  *         or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1252  *         registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1253  *         it has been updated and the cache needs to be re-read.
1254  */
1255 static GstRegistryScanAndUpdateResult
1256 scan_and_update_registry (GstRegistry * default_registry,
1257     const gchar * registry_file, gboolean write_changes, GError ** error)
1258 {
1259   const gchar *plugin_path;
1260   gboolean changed = FALSE;
1261   GList *l;
1262   GstRegistryScanContext context;
1263
1264   GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1265
1266   init_scan_context (&context, default_registry);
1267
1268   /* It sounds tempting to just compare the mtime of directories with the mtime
1269    * of the registry cache, but it does not work. It would not catch updated
1270    * plugins, which might bring more or less features.
1271    */
1272
1273   /* scan paths specified via --gst-plugin-path */
1274   GST_DEBUG ("scanning paths added via --gst-plugin-path");
1275   for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1276     GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1277     changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1278   }
1279   /* keep plugin_paths around in case a re-scan is forced later on */
1280
1281   /* GST_PLUGIN_PATH specifies a list of directories to scan for
1282    * additional plugins.  These take precedence over the system plugins */
1283   plugin_path = g_getenv ("GST_PLUGIN_PATH");
1284   if (plugin_path) {
1285     char **list;
1286     int i;
1287
1288     GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1289     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1290     for (i = 0; list[i]; i++) {
1291       changed |= gst_registry_scan_path_internal (&context, list[i]);
1292     }
1293     g_strfreev (list);
1294   } else {
1295     GST_DEBUG ("GST_PLUGIN_PATH not set");
1296   }
1297
1298   /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1299    * loaded by default.  If not set, this defaults to the system-installed
1300    * path, and the plugins installed in the user's home directory */
1301   plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1302   if (plugin_path == NULL) {
1303     char *home_plugins;
1304
1305     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1306
1307     /* plugins in the user's home directory take precedence over
1308      * system-installed ones */
1309     home_plugins = g_build_filename (g_get_home_dir (),
1310         ".gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1311     GST_DEBUG ("scanning home plugins %s", home_plugins);
1312     changed |= gst_registry_scan_path_internal (&context, home_plugins);
1313     g_free (home_plugins);
1314
1315     /* add the main (installed) library path */
1316     GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1317     changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1318
1319 #ifdef G_OS_WIN32
1320     {
1321       char *base_dir;
1322       char *dir;
1323
1324       base_dir =
1325           g_win32_get_package_installation_directory_of_module
1326           (_priv_gst_dll_handle);
1327
1328       dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1329       GST_DEBUG ("scanning DLL dir %s", dir);
1330
1331       changed |= gst_registry_scan_path_internal (&context, dir);
1332
1333       g_free (dir);
1334       g_free (base_dir);
1335     }
1336 #endif
1337   } else {
1338     gchar **list;
1339     gint i;
1340
1341     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1342     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1343     for (i = 0; list[i]; i++) {
1344       changed |= gst_registry_scan_path_internal (&context, list[i]);
1345     }
1346     g_strfreev (list);
1347   }
1348
1349   clear_scan_context (&context);
1350   changed |= context.changed;
1351
1352   /* Remove cached plugins so stale info is cleared. */
1353   changed |= gst_registry_remove_cache_plugins (default_registry);
1354
1355   if (!changed) {
1356     GST_INFO ("Registry cache has not changed");
1357     return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1358   }
1359
1360   if (!write_changes) {
1361     GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1362     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1363   }
1364
1365   GST_INFO ("Registry cache changed. Writing new registry cache");
1366   if (!gst_registry_binary_write_cache (default_registry, registry_file)) {
1367     g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1368         _("Error writing registry cache to %s: %s"),
1369         registry_file, g_strerror (errno));
1370     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1371   }
1372
1373   GST_INFO ("Registry cache written successfully");
1374   return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1375 }
1376
1377 static gboolean
1378 ensure_current_registry (GError ** error)
1379 {
1380   gchar *registry_file;
1381   GstRegistry *default_registry;
1382   gboolean ret = TRUE;
1383   gboolean do_update = TRUE;
1384   gboolean have_cache = TRUE;
1385
1386   default_registry = gst_registry_get_default ();
1387   registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1388   if (registry_file == NULL) {
1389     registry_file = g_build_filename (g_get_home_dir (),
1390         ".gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1391   }
1392
1393   if (!_gst_disable_registry_cache) {
1394     GST_INFO ("reading registry cache: %s", registry_file);
1395     have_cache = gst_registry_binary_read_cache (default_registry,
1396         registry_file);
1397     /* Only ever read the registry cache once, then disable it for
1398      * subsequent updates during the program lifetime */
1399     _gst_disable_registry_cache = TRUE;
1400   }
1401
1402   if (have_cache) {
1403     do_update = !_priv_gst_disable_registry_update;
1404     if (do_update) {
1405       const gchar *update_env;
1406
1407       if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1408         /* do update for any value different from "no" */
1409         do_update = (strcmp (update_env, "no") != 0);
1410       }
1411     }
1412   }
1413
1414   if (do_update) {
1415     /* now check registry */
1416     GST_DEBUG ("Updating registry cache");
1417     scan_and_update_registry (default_registry, registry_file, TRUE, error);
1418   } else {
1419     GST_DEBUG ("Not updating registry cache (disabled)");
1420   }
1421
1422   g_free (registry_file);
1423   GST_INFO ("registry reading and updating done, result = %d", ret);
1424
1425   return ret;
1426 }
1427 #endif /* GST_DISABLE_REGISTRY */
1428
1429 /**
1430  * gst_registry_fork_is_enabled:
1431  *
1432  * By default GStreamer will perform scanning and rebuilding of the
1433  * registry file using a helper child process.
1434  *
1435  * Applications might want to disable this behaviour with the
1436  * gst_registry_fork_set_enabled() function, in which case new plugins
1437  * are scanned (and loaded) into the application process.
1438  *
1439  * Returns: %TRUE if GStreamer will use the child helper process when
1440  * rebuilding the registry.
1441  *
1442  * Since: 0.10.10
1443  */
1444 gboolean
1445 gst_registry_fork_is_enabled (void)
1446 {
1447   return _gst_enable_registry_fork;
1448 }
1449
1450 /**
1451  * gst_registry_fork_set_enabled:
1452  * @enabled: whether rebuilding the registry can use a temporary child helper process.
1453  *
1454  * Applications might want to disable/enable spawning of a child helper process
1455  * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1456  * information.
1457  *
1458  * Since: 0.10.10
1459  */
1460 void
1461 gst_registry_fork_set_enabled (gboolean enabled)
1462 {
1463   _gst_enable_registry_fork = enabled;
1464 }
1465
1466 /**
1467  * gst_update_registry:
1468  *
1469  * Forces GStreamer to re-scan its plugin paths and update the default
1470  * plugin registry.
1471  *
1472  * Applications will almost never need to call this function, it is only
1473  * useful if the application knows new plugins have been installed (or old
1474  * ones removed) since the start of the application (or, to be precise, the
1475  * first call to gst_init()) and the application wants to make use of any
1476  * newly-installed plugins without restarting the application.
1477  *
1478  * Applications should assume that the registry update is neither atomic nor
1479  * thread-safe and should therefore not have any dynamic pipelines running
1480  * (including the playbin and decodebin elements) and should also not create
1481  * any elements or access the GStreamer registry while the update is in
1482  * progress.
1483  *
1484  * Note that this function may block for a significant amount of time.
1485  *
1486  * Returns: %TRUE if the registry has been updated successfully (does not
1487  *          imply that there were changes), otherwise %FALSE.
1488  *
1489  * Since: 0.10.12
1490  */
1491 gboolean
1492 gst_update_registry (void)
1493 {
1494   gboolean res;
1495
1496 #ifndef GST_DISABLE_REGISTRY
1497   GError *err = NULL;
1498
1499   res = ensure_current_registry (&err);
1500   if (err) {
1501     GST_WARNING ("registry update failed: %s", err->message);
1502     g_error_free (err);
1503   } else {
1504     GST_LOG ("registry update succeeded");
1505   }
1506
1507 #else
1508   GST_WARNING ("registry update failed: %s", "registry disabled");
1509   res = TRUE;
1510 #endif /* GST_DISABLE_REGISTRY */
1511
1512   if (_priv_gst_preload_plugins) {
1513     GST_DEBUG ("Preloading indicated plugins...");
1514     g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1515   }
1516
1517   return res;
1518 }