Add another GST_STR_NULL instance
[platform/upstream/gstreamer.git] / gst / gstregistryxml.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  * gstregistryxml.c: GstRegistry object, support routines
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it ulnder 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 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
33 #include <fcntl.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <gst/gst_private.h>
39 #include <gst/gstelement.h>
40 #include <gst/gsttypefind.h>
41 #include <gst/gsttypefindfactory.h>
42 #include <gst/gsturi.h>
43 #include <gst/gstinfo.h>
44 #include <gst/gstenumtypes.h>
45 #include <gst/gstregistry.h>
46
47 #include <libxml/xmlreader.h>
48
49 #include "glib-compat.h"
50
51 #define BLOCK_SIZE 1024*10
52
53 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
54
55 #define CLASS(registry)  GST_XML_REGISTRY_CLASS (G_OBJECT_GET_CLASS (registry))
56
57
58 //static gboolean gst_registry_xml_load (GstRegistry * registry);
59 //static gboolean gst_registry_xml_save (GstRegistry * registry);
60 //static gboolean gst_registry_xml_rebuild (GstRegistry * registry);
61
62
63 #if 0
64 static void
65 gst_registry_xml_class_init (GstRegistryClass * klass)
66 {
67   GObjectClass *gobject_class;
68   GstRegistryClass *gstregistry_class;
69   GstRegistryClass *gstxmlregistry_class;
70
71   gobject_class = (GObjectClass *) klass;
72   gstregistry_class = (GstRegistryClass *) klass;
73   gstxmlregistry_class = (GstRegistryClass *) klass;
74
75   parent_class = g_type_class_ref (GST_TYPE_REGISTRY);
76
77   gobject_class->get_property =
78       GST_DEBUG_FUNCPTR (gst_registry_xml_get_property);
79   gobject_class->set_property =
80       GST_DEBUG_FUNCPTR (gst_registry_xml_set_property);
81
82   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LOCATION,
83       g_param_spec_string ("location", "Location",
84           "Location of the registry file", NULL, G_PARAM_READWRITE));
85
86   gstregistry_class->load = GST_DEBUG_FUNCPTR (gst_registry_xml_load);
87   gstregistry_class->save = GST_DEBUG_FUNCPTR (gst_registry_xml_save);
88   gstregistry_class->rebuild = GST_DEBUG_FUNCPTR (gst_registry_xml_rebuild);
89
90   gstregistry_class->load_plugin =
91       GST_DEBUG_FUNCPTR (gst_registry_xml_load_plugin);
92
93   gstxmlregistry_class->get_perms_func =
94       GST_DEBUG_FUNCPTR (gst_registry_xml_get_perms_func);
95   gstxmlregistry_class->open_func =
96       GST_DEBUG_FUNCPTR (gst_registry_xml_open_func);
97   gstxmlregistry_class->load_func =
98       GST_DEBUG_FUNCPTR (gst_registry_xml_load_func);
99 }
100
101 static void
102 gst_registry_xml_init (GstRegistry * registry)
103 {
104   registry->location = NULL;
105 }
106 #endif
107
108 /* this function returns the biggest of the path's mtime and ctime
109  * mtime is updated through an actual write (data)
110  * ctime is updated through changing inode information
111  * so this function returns the last time *anything* changed to this path
112  * it also sets the given boolean to TRUE if the given path is a directory
113  */
114 #if 0
115 static time_t
116 get_time (const char *path, gboolean * is_dir)
117 {
118   struct stat statbuf;
119
120   if (stat (path, &statbuf)) {
121     *is_dir = FALSE;
122     return 0;
123   }
124
125   if (is_dir)
126     *is_dir = S_ISDIR (statbuf.st_mode);
127
128   if (statbuf.st_mtime > statbuf.st_ctime)
129     return statbuf.st_mtime;
130   return statbuf.st_ctime;
131 }
132 #endif
133
134
135 #if 0
136 static void
137 gst_registry_xml_get_perms_func (GstRegistry * registry)
138 {
139   gchar *dirname;
140
141   /* if the dir does not exist, make it. if that can't be done, flags = 0x0.
142      if the file can be appended to, it's writable. if it can then be read,
143      it's readable.
144      After that check if it exists. */
145
146   if (make_dir (registry->location) != TRUE) {
147     /* we can't do anything with it, leave flags as 0x0 */
148     return;
149   }
150
151   dirname = g_path_get_dirname (registry->location);
152
153   if (g_file_test (registry->location, G_FILE_TEST_EXISTS)) {
154     GST_REGISTRY (registry)->flags |= GST_REGISTRY_EXISTS;
155   }
156
157   if (!access (dirname, W_OK)) {
158     GST_REGISTRY (registry)->flags |= GST_REGISTRY_WRITABLE;
159   }
160
161   if (!access (dirname, R_OK)) {
162     GST_REGISTRY (registry)->flags |= GST_REGISTRY_READABLE;
163   }
164
165   g_free (dirname);
166 }
167 #endif
168
169 #if 0
170 /* return TRUE iff regtime is more recent than the times of all the .so files
171  * in the plugin dirs; ie return TRUE if this path does not need to trigger
172  * a rebuild of registry
173  *
174  * - if it's a directory, recurse on subdirs
175  * - if it's a file
176  *   - if entry is not newer, return TRUE.
177  *   - if it's newer
178  *     - and it's a plugin, return FALSE
179  *     - otherwise return TRUE
180  */
181 static gboolean
182 plugin_times_older_than_recurse (gchar * path, time_t regtime)
183 {
184   DIR *dir;
185   struct dirent *dirent;
186   gboolean is_dir;
187   gchar *new_path;
188
189   time_t pathtime = get_time (path, &is_dir);
190
191   if (is_dir) {
192     dir = opendir (path);
193     if (dir) {
194       while ((dirent = readdir (dir))) {
195         /* don't want to recurse in place or backwards */
196         if (strcmp (dirent->d_name, ".") && strcmp (dirent->d_name, "..")) {
197           new_path = g_build_filename (path, dirent->d_name, NULL);
198           if (!plugin_times_older_than_recurse (new_path, regtime)) {
199             GST_CAT_INFO (GST_CAT_PLUGIN_LOADING,
200                 "path %s is more recent than registry time of %ld",
201                 new_path, (long) regtime);
202             g_free (new_path);
203             closedir (dir);
204             return FALSE;
205           }
206           g_free (new_path);
207         }
208       }
209       closedir (dir);
210     }
211     return TRUE;
212   }
213
214   /* it's a file */
215   if (pathtime <= regtime) {
216     return TRUE;
217   }
218
219   /* it's a file, and it's more recent */
220   if (g_str_has_suffix (path, ".so") || g_str_has_suffix (path, ".dll")) {
221     if (!gst_plugin_check_file (path, NULL))
222       return TRUE;
223
224     /* it's a newer GStreamer plugin */
225     GST_CAT_INFO (GST_CAT_PLUGIN_LOADING,
226         "%s looks like a plugin and is more recent than registry time of %ld",
227         path, (long) regtime);
228     return FALSE;
229   }
230   return TRUE;
231 }
232 #endif
233
234 #if 0
235 /* return TRUE iff regtime is more recent than the times of all the .so files
236  * in the plugin dirs; ie return TRUE if registry is up to date.
237  */
238 static gboolean
239 plugin_times_older_than (GList * paths, time_t regtime)
240 {
241   while (paths) {
242     GST_CAT_LOG (GST_CAT_PLUGIN_LOADING,
243         "comparing plugin times from %s with %ld",
244         (gchar *) paths->data, (long) regtime);
245     if (!plugin_times_older_than_recurse (paths->data, regtime))
246       return FALSE;
247     paths = g_list_next (paths);
248   }
249   GST_CAT_LOG (GST_CAT_PLUGIN_LOADING,
250       "everything's fine, no registry rebuild needed.");
251   return TRUE;
252 }
253 #endif
254
255 #if 0
256 static gboolean
257 gst_registry_xml_open_func (GstRegistry * registry, GstRegistryMode mode)
258 {
259   GstRegistry *gst_registry;
260   GList *paths;
261
262   gst_registry = GST_REGISTRY (registry);
263   paths = gst_registry->paths;
264   GST_CAT_DEBUG (GST_CAT_GST_INIT, "opening registry %s", registry->location);
265
266   g_return_val_if_fail (registry->open == FALSE, FALSE);
267
268   /* if it doesn't exist, first try to build it, and check if it worked
269    * if it's not readable, return false
270    * if it's out of date, rebuild it */
271   if (mode == GST_XML_REGISTRY_READ) {
272     if (!(gst_registry->flags & GST_REGISTRY_EXISTS)) {
273       /* if it's not writable, then don't bother */
274       if (!(gst_registry->flags & GST_REGISTRY_WRITABLE)) {
275         GST_CAT_INFO (GST_CAT_GST_INIT, "Registry isn't writable");
276         return FALSE;
277       }
278       GST_CAT_INFO (GST_CAT_GST_INIT,
279           "Registry doesn't exist, trying to build...");
280       gst_registry_rebuild (gst_registry);
281       gst_registry_save (gst_registry);
282       /* FIXME: verify that the flags actually get updated ! */
283       if (!(gst_registry->flags & GST_REGISTRY_EXISTS)) {
284         return FALSE;
285       }
286     }
287     /* at this point we know it exists */
288     g_return_val_if_fail (gst_registry->flags & GST_REGISTRY_READABLE, FALSE);
289
290     if (!plugin_times_older_than (paths, get_time (registry->location, NULL))) {
291       if (gst_registry->flags & GST_REGISTRY_WRITABLE) {
292         GST_CAT_INFO (GST_CAT_GST_INIT, "Registry out of date, rebuilding...");
293
294         gst_registry_rebuild (gst_registry);
295
296         gst_registry_save (gst_registry);
297
298         if (!plugin_times_older_than (paths, get_time (registry->location,
299                     NULL))) {
300           GST_CAT_INFO (GST_CAT_GST_INIT,
301               "Registry still out of date, something is wrong...");
302           return FALSE;
303         }
304       } else {
305         GST_CAT_INFO (GST_CAT_GST_INIT,
306             "Can't write to this registry and it's out of date, ignoring it");
307         return FALSE;
308       }
309     }
310
311     GST_CAT_DEBUG (GST_CAT_GST_INIT, "opening registry %s for reading",
312         registry->location);
313     registry->cache_file = fopen (registry->location, "r");
314   } else if (mode == GST_XML_REGISTRY_WRITE) {
315     char *tmploc;
316     int fd;
317
318     g_return_val_if_fail (gst_registry->flags & GST_REGISTRY_WRITABLE, FALSE);
319
320     tmploc = g_strconcat (registry->location, ".tmp", NULL);
321
322     GST_CAT_DEBUG (GST_CAT_GST_INIT, "opening registry %s for writing", tmploc);
323
324     if ((fd = open (tmploc, O_WRONLY | O_CREAT, 0644)) < 0) {
325       g_free (tmploc);
326       return FALSE;
327     }
328     g_free (tmploc);
329
330     registry->cache_file = fdopen (fd, "w");
331   }
332
333   if (!registry->cache_file)
334     return FALSE;
335
336   registry->open = TRUE;
337
338   return TRUE;
339 }
340 #endif
341
342 static gboolean
343 gst_registry_xml_save (GstRegistry * registry, gchar * format, ...)
344 {
345   va_list var_args;
346
347   va_start (var_args, format);
348
349   vfprintf (registry->cache_file, format, var_args);
350
351   va_end (var_args);
352
353   return TRUE;
354 }
355
356 static void
357 add_to_char_array (gchar *** array, gchar * value)
358 {
359   gchar **new;
360   gchar **old = *array;
361   gint i = 0;
362
363   /* expensive, but cycles are cheap... */
364   if (old)
365     while (old[i])
366       i++;
367   new = g_new0 (gchar *, i + 2);
368   new[i] = value;
369   while (i > 0) {
370     i--;
371     new[i] = old[i];
372   }
373   g_free (old);
374   *array = new;
375 }
376
377 /* read a string and copy it into the given location */
378 static gboolean
379 read_string (xmlTextReaderPtr reader, gchar ** write_to)
380 {
381   int depth = xmlTextReaderDepth (reader);
382   gboolean found = FALSE;
383
384   while (xmlTextReaderRead (reader) == 1) {
385     if (xmlTextReaderDepth (reader) == depth)
386       return found;
387     if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_TEXT) {
388       if (found)
389         return FALSE;
390       *write_to = g_strdup ((gchar *) xmlTextReaderConstValue (reader));
391       found = TRUE;
392     }
393   }
394   return FALSE;
395 }
396
397 static gboolean
398 read_uint (xmlTextReaderPtr reader, guint * write_to)
399 {
400   int depth = xmlTextReaderDepth (reader);
401   gboolean found = FALSE;
402
403   while (xmlTextReaderRead (reader) == 1) {
404     if (xmlTextReaderDepth (reader) == depth)
405       return found;
406     if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_TEXT) {
407       gchar *ret;
408       const gchar *s;
409
410       if (found) {
411         GST_DEBUG ("failed to read uint, multiple text nodes");
412         return FALSE;
413       }
414       s = (const gchar *) xmlTextReaderConstValue (reader);
415       *write_to = strtol (s, &ret, 0);
416       if (s == ret) {
417         GST_DEBUG ("failed to read uint, text didn't convert to int");
418         return FALSE;
419       }
420       found = TRUE;
421     }
422   }
423   GST_DEBUG ("failed to read uint, no text node");
424   return FALSE;
425 }
426
427 static gboolean
428 read_enum (xmlTextReaderPtr reader, GType enum_type, guint * write_to)
429 {
430   int depth = xmlTextReaderDepth (reader);
431   gboolean found = FALSE;
432
433   if (*write_to)
434     return FALSE;
435   while (xmlTextReaderRead (reader) == 1) {
436     if (xmlTextReaderDepth (reader) == depth)
437       return found;
438     if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_TEXT) {
439       GEnumClass *enum_class;
440       GEnumValue *value;
441
442       if (found)
443         return FALSE;
444       enum_class = g_type_class_ref (enum_type);
445       if (!enum_class)
446         return FALSE;
447       value =
448           g_enum_get_value_by_nick (enum_class,
449           (gchar *) xmlTextReaderConstValue (reader));
450       if (value) {
451         *write_to = value->value;
452         found = TRUE;
453       }
454       g_type_class_unref (enum_class);
455     }
456   }
457   return FALSE;
458 }
459
460 static GstStaticPadTemplate *
461 load_pad_template (xmlTextReaderPtr reader)
462 {
463   int ret;
464   int depth = xmlTextReaderDepth (reader);
465   gchar *name = NULL, *caps_str = NULL;
466   guint direction = 0, presence = 0;
467
468   while ((ret = xmlTextReaderRead (reader)) == 1) {
469     if (xmlTextReaderDepth (reader) == depth) {
470       GstStaticPadTemplate *template;
471
472       template = g_new0 (GstStaticPadTemplate, 1);
473       template->name_template = name;
474       template->presence = presence;
475       template->direction = direction;
476       template->static_caps.string = caps_str;
477
478       return template;
479     }
480     if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
481         xmlTextReaderDepth (reader) == depth + 1) {
482       const gchar *tag = (gchar *) xmlTextReaderConstName (reader);
483
484       if (g_str_equal (tag, "nametemplate")) {
485         read_string (reader, &name);
486       } else if (g_str_equal (tag, "direction")) {
487         read_enum (reader, GST_TYPE_PAD_DIRECTION, &direction);
488       } else if (g_str_equal (tag, "presence")) {
489         read_enum (reader, GST_TYPE_PAD_PRESENCE, &presence);
490       } else if (!strncmp (tag, "caps", 4)) {
491         read_string (reader, &caps_str);
492       }
493     }
494   }
495   g_free (name);
496   g_free (caps_str);
497
498   return NULL;
499 }
500
501 static GstPluginFeature *
502 load_feature (xmlTextReaderPtr reader)
503 {
504   int ret;
505   int depth = xmlTextReaderDepth (reader);
506   gchar *feature_name =
507       (gchar *) xmlTextReaderGetAttribute (reader, BAD_CAST "typename");
508   GstPluginFeature *feature;
509   GType type;
510
511   GST_DEBUG ("loading feature");
512
513   if (!feature_name)
514     return NULL;
515   type = g_type_from_name (feature_name);
516   g_free (feature_name);
517   feature_name = NULL;
518
519   if (!type) {
520     return NULL;
521   }
522   feature = g_object_new (type, NULL);
523   if (!feature) {
524     return NULL;
525   }
526   if (!GST_IS_PLUGIN_FEATURE (feature)) {
527     g_object_unref (feature);
528     return NULL;
529   }
530   while ((ret = xmlTextReaderRead (reader)) == 1) {
531     if (xmlTextReaderDepth (reader) == depth) {
532       GST_DEBUG ("loaded feature %p with name %s", feature, feature->name);
533       return feature;
534     }
535     if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
536         xmlTextReaderDepth (reader) == depth + 1) {
537       const gchar *tag = (gchar *) xmlTextReaderConstName (reader);
538
539       if (g_str_equal (tag, "name"))
540         read_string (reader, &feature->name);
541       if (g_str_equal (tag, "rank"))
542         read_uint (reader, &feature->rank);
543       if (GST_IS_ELEMENT_FACTORY (feature)) {
544         GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
545
546         if (g_str_equal (tag, "longname")) {
547           int ret;
548
549           ret = read_string (reader, &factory->details.longname);
550           GST_DEBUG ("longname ret=%d, name=%s",
551               ret, factory->details.longname);
552         } else if (g_str_equal (tag, "class")) {
553           read_string (reader, &factory->details.klass);
554         } else if (g_str_equal (tag, "description")) {
555           read_string (reader, &factory->details.description);
556         } else if (g_str_equal (tag, "author")) {
557           read_string (reader, &factory->details.author);
558         } else if (g_str_equal (tag, "uri_type")) {
559           gchar *s = NULL;
560
561           if (read_string (reader, &s)) {
562             if (g_ascii_strncasecmp (s, "sink", 4) == 0) {
563               factory->uri_type = GST_URI_SINK;
564             } else if (g_ascii_strncasecmp (s, "source", 5) == 0) {
565               factory->uri_type = GST_URI_SRC;
566             }
567             g_free (s);
568           }
569         } else if (g_str_equal (tag, "uri_protocol")) {
570           gchar *s = NULL;
571
572           if (read_string (reader, &s))
573             add_to_char_array (&factory->uri_protocols, s);
574         } else if (g_str_equal (tag, "interface")) {
575           gchar *s = NULL;
576
577           if (read_string (reader, &s)) {
578             __gst_element_factory_add_interface (factory, s);
579             /* add_interface strdup's s */
580             g_free (s);
581           }
582         } else if (g_str_equal (tag, "padtemplate")) {
583           GstStaticPadTemplate *template = load_pad_template (reader);
584
585           if (template) {
586             GST_LOG ("adding template %s to factory %s",
587                 GST_STR_NULL (GST_PAD_TEMPLATE_NAME_TEMPLATE (template)),
588                 GST_PLUGIN_FEATURE_NAME (feature));
589             __gst_element_factory_add_static_pad_template (factory, template);
590           }
591         }
592       } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
593         GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
594
595         if (g_str_equal (tag, "extension")) {
596           gchar *s = NULL;
597
598           if (read_string (reader, &s))
599             add_to_char_array (&factory->extensions, s);
600         } else if (g_str_equal (tag, "caps")) {
601           gchar *s = NULL;
602
603           if (read_string (reader, &s)) {
604             factory->caps = gst_caps_from_string (s);
605             g_free (s);
606           }
607         }
608       } else if (GST_IS_INDEX_FACTORY (feature)) {
609         GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
610
611         if (g_str_equal (tag, "longdesc"))
612           read_string (reader, &factory->longdesc);
613       }
614     }
615   }
616
617   g_assert_not_reached ();
618   return NULL;
619 }
620
621 static GstPlugin *
622 load_plugin (xmlTextReaderPtr reader, GList ** feature_list)
623 {
624   int ret;
625   GstPlugin *plugin;
626
627   *feature_list = NULL;
628
629   GST_DEBUG ("creating new plugin and parsing");
630
631   plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
632
633   plugin->flags |= GST_PLUGIN_FLAG_CACHED;
634   while ((ret = xmlTextReaderRead (reader)) == 1) {
635     if (xmlTextReaderDepth (reader) == 1) {
636       return plugin;
637     }
638     if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
639         xmlTextReaderDepth (reader) == 2) {
640       const gchar *tag = (gchar *) xmlTextReaderConstName (reader);
641
642       if (g_str_equal (tag, "name")) {
643         int ret;
644
645         ret = read_string (reader, &plugin->desc.name);
646         GST_DEBUG ("name ret=%d, name=%s", ret, plugin->desc.name);
647         if (!ret)
648           break;
649       } else if (g_str_equal (tag, "description")) {
650         if (!read_string (reader, &plugin->desc.description))
651           break;
652         GST_DEBUG ("description %s", plugin->desc.description);
653       } else if (g_str_equal (tag, "filename")) {
654         if (!read_string (reader, &plugin->filename))
655           break;
656         GST_DEBUG ("filename %s", plugin->filename);
657         plugin->basename = g_path_get_basename (plugin->filename);
658       } else if (g_str_equal (tag, "version")) {
659         if (!read_string (reader, &plugin->desc.version))
660           break;
661         GST_DEBUG ("version %s", plugin->desc.version);
662       } else if (g_str_equal (tag, "license")) {
663         if (!read_string (reader, &plugin->desc.license))
664           break;
665         GST_DEBUG ("license %s", plugin->desc.license);
666       } else if (g_str_equal (tag, "source")) {
667         if (!read_string (reader, &plugin->desc.source))
668           break;
669         GST_DEBUG ("source %s", plugin->desc.source);
670       } else if (g_str_equal (tag, "package")) {
671         if (!read_string (reader, &plugin->desc.package))
672           break;
673         GST_DEBUG ("package %s", plugin->desc.package);
674       } else if (g_str_equal (tag, "origin")) {
675         if (!read_string (reader, &plugin->desc.origin)) {
676           GST_DEBUG ("failed to read origin");
677           break;
678         }
679       } else if (g_str_equal (tag, "m32p")) {
680         char *s;
681
682         if (!read_string (reader, &s)) {
683           GST_DEBUG ("failed to read mtime");
684           break;
685         }
686         plugin->file_mtime = strtol (s, NULL, 0);
687         GST_DEBUG ("mtime %d", (int) plugin->file_mtime);
688         g_free (s);
689       } else if (g_str_equal (tag, "size")) {
690         unsigned int x;
691
692         if (read_uint (reader, &x)) {
693           plugin->file_size = x;
694           GST_DEBUG ("file_size %d", plugin->file_size);
695         } else {
696           GST_DEBUG ("failed to read size");
697         }
698       } else if (g_str_equal (tag, "feature")) {
699         GstPluginFeature *feature = load_feature (reader);
700
701         feature->plugin_name = g_strdup (plugin->desc.name);
702
703         if (feature) {
704           *feature_list = g_list_prepend (*feature_list, feature);
705         }
706       } else {
707         GST_DEBUG ("unknown tag %s", tag);
708       }
709     }
710   }
711   gst_object_unref (plugin);
712
713   GST_DEBUG ("problem reading plugin");
714
715   return NULL;
716 }
717
718 gboolean
719 gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
720 {
721   GTimer *timer;
722   gdouble seconds;
723   xmlTextReaderPtr reader;
724   int ret;
725   gboolean in_registry = FALSE;
726   FILE *file;
727
728   /* make sure these types exist */
729   GST_TYPE_ELEMENT_FACTORY;
730   GST_TYPE_TYPE_FIND_FACTORY;
731   GST_TYPE_INDEX_FACTORY;
732
733   timer = g_timer_new ();
734
735   file = fopen (location, "r");
736   if (file == NULL) {
737     g_timer_destroy (timer);
738     return FALSE;
739   }
740
741   reader = xmlReaderForFd (fileno (file), NULL, NULL, 0);
742   if (!reader) {
743     fclose (file);
744     g_timer_destroy (timer);
745     return FALSE;
746   }
747
748   while ((ret = xmlTextReaderRead (reader)) == 1) {
749     if (xmlTextReaderDepth (reader) == 0) {
750       in_registry = xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT &&
751           g_str_equal ("GST-PluginRegistry", xmlTextReaderConstName (reader));
752     } else if (in_registry) {
753       if (xmlTextReaderDepth (reader) == 1 &&
754           xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT) {
755         const gchar *tag = (const gchar *) xmlTextReaderConstName (reader);
756
757         if (g_str_equal (tag, "plugin")) {
758           GList *feature_list;
759           GstPlugin *plugin = load_plugin (reader, &feature_list);
760
761           if (plugin) {
762             GList *g;
763
764             GST_DEBUG ("adding plugin %s", plugin->desc.name);
765             gst_registry_add_plugin (registry, plugin);
766             for (g = feature_list; g; g = g_list_next (g)) {
767               gst_registry_add_feature (registry, GST_PLUGIN_FEATURE (g->data));
768             }
769             g_list_free (feature_list);
770           }
771         }
772       }
773     }
774   }
775   xmlFreeTextReader (reader);
776   if (ret != 0) {
777     GST_ERROR ("parsing registry cache: %s", location);
778     fclose (file);
779     g_timer_destroy (timer);
780     return FALSE;
781   }
782
783   g_timer_stop (timer);
784   seconds = g_timer_elapsed (timer, NULL);
785   g_timer_destroy (timer);
786
787   GST_INFO ("loaded %s in %f seconds", location, seconds);
788
789   fclose (file);
790
791   return TRUE;
792 }
793
794 #if 0
795 static gboolean
796 gst_registry_xml_load_plugin (GstRegistry * registry, GstPlugin * plugin)
797 {
798   GError *error = NULL;
799   GstPlugin *loaded_plugin;
800
801   /* FIXME: add gerror support */
802   loaded_plugin = gst_plugin_load_file (plugin->filename, &error);
803   if (!plugin) {
804     if (error) {
805       g_warning ("could not load plugin %s: %s", plugin->desc.name,
806           error->message);
807       g_error_free (error);
808     }
809     return FALSE;
810   } else if (loaded_plugin != plugin) {
811     g_critical ("how to remove plugins?");
812   }
813
814   return TRUE;
815 }
816 #endif
817
818 /*
819  * Save
820  */
821 #define PUT_ESCAPED(prefix,tag,value)                           \
822 G_STMT_START{                                                   \
823   const gchar *toconv = value;                                  \
824   if (toconv) {                                                 \
825     gchar *v = g_markup_escape_text (toconv, strlen (toconv));  \
826     gst_registry_xml_save (registry, prefix "<%s>%s</%s>\n", tag, v, tag);                      \
827     g_free (v);                                                 \
828   }                                                             \
829 }G_STMT_END
830
831
832 static gboolean
833 gst_registry_xml_save_caps (GstRegistry * registry, const GstCaps * caps)
834 {
835   /* we copy the caps here so we can simplify them before saving. This is a lot
836    * faster when loading them later on */
837   char *s;
838   GstCaps *copy = gst_caps_copy (caps);
839
840   gst_caps_do_simplify (copy);
841   s = gst_caps_to_string (copy);
842   gst_caps_unref (copy);
843
844   PUT_ESCAPED ("  ", "caps", s);
845   g_free (s);
846   return TRUE;
847 }
848
849 static gboolean
850 gst_registry_xml_save_pad_template (GstRegistry * registry,
851     GstStaticPadTemplate * template)
852 {
853   gchar *presence;
854
855   PUT_ESCAPED ("   ", "nametemplate", template->name_template);
856   gst_registry_xml_save (registry, "   <direction>%s</direction>\n",
857       (template->direction == GST_PAD_SINK ? "sink" : "src"));
858
859   switch (template->presence) {
860     case GST_PAD_ALWAYS:
861       presence = "always";
862       break;
863     case GST_PAD_SOMETIMES:
864       presence = "sometimes";
865       break;
866     case GST_PAD_REQUEST:
867       presence = "request";
868       break;
869     default:
870       presence = "unknown";
871       break;
872   }
873   gst_registry_xml_save (registry, "   <presence>%s</presence>\n", presence);
874
875   if (template->static_caps.string) {
876     gst_registry_xml_save (registry, "   <caps>%s</caps>\n",
877         template->static_caps.string);
878   }
879   return TRUE;
880 }
881
882 static gboolean
883 gst_registry_xml_save_feature (GstRegistry * registry,
884     GstPluginFeature * feature)
885 {
886   PUT_ESCAPED ("  ", "name", feature->name);
887
888   if (feature->rank > 0) {
889     gint rank = feature->rank;
890
891     gst_registry_xml_save (registry, "  <rank>%d</rank>\n", rank);
892   }
893
894   if (GST_IS_ELEMENT_FACTORY (feature)) {
895     GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
896     GList *walk;
897
898     PUT_ESCAPED ("  ", "longname", factory->details.longname);
899     PUT_ESCAPED ("  ", "class", factory->details.klass);
900     PUT_ESCAPED ("  ", "description", factory->details.description);
901     PUT_ESCAPED ("  ", "author", factory->details.author);
902
903     walk = factory->staticpadtemplates;
904
905     while (walk) {
906       GstStaticPadTemplate *template = walk->data;
907
908       gst_registry_xml_save (registry, "  <padtemplate>\n");
909       gst_registry_xml_save_pad_template (registry, template);
910       gst_registry_xml_save (registry, "  </padtemplate>\n");
911
912       walk = g_list_next (walk);
913     }
914
915     walk = factory->interfaces;
916     while (walk) {
917       PUT_ESCAPED ("  ", "interface", (gchar *) walk->data);
918       walk = g_list_next (walk);
919     }
920
921     if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
922       gchar **protocol;
923
924       PUT_ESCAPED ("  ", "uri_type",
925           factory->uri_type == GST_URI_SINK ? "sink" : "source");
926       g_assert (factory->uri_protocols);
927       protocol = factory->uri_protocols;
928       while (*protocol) {
929         PUT_ESCAPED ("  ", "uri_protocol", *protocol);
930         protocol++;
931       }
932     }
933   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
934     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
935     gint i = 0;
936
937     if (factory->caps) {
938       gst_registry_xml_save_caps (registry, factory->caps);
939     }
940     if (factory->extensions) {
941       while (factory->extensions[i]) {
942         PUT_ESCAPED ("  ", "extension", factory->extensions[i]);
943         i++;
944       }
945     }
946   } else if (GST_IS_INDEX_FACTORY (feature)) {
947     PUT_ESCAPED ("  ", "longdesc", GST_INDEX_FACTORY (feature)->longdesc);
948   }
949   return TRUE;
950 }
951
952 static gboolean
953 gst_registry_xml_save_plugin (GstRegistry * registry, GstPlugin * plugin)
954 {
955   GList *list;
956   GList *walk;
957   char s[100];
958
959   PUT_ESCAPED (" ", "name", plugin->desc.name);
960   PUT_ESCAPED (" ", "description", plugin->desc.description);
961   PUT_ESCAPED (" ", "filename", plugin->filename);
962   sprintf (s, "%d", (int) plugin->file_size);
963   PUT_ESCAPED (" ", "size", s);
964   sprintf (s, "%d", (int) plugin->file_mtime);
965   PUT_ESCAPED (" ", "m32p", s);
966   PUT_ESCAPED (" ", "version", plugin->desc.version);
967   PUT_ESCAPED (" ", "license", plugin->desc.license);
968   PUT_ESCAPED (" ", "source", plugin->desc.source);
969   PUT_ESCAPED (" ", "package", plugin->desc.package);
970   PUT_ESCAPED (" ", "origin", plugin->desc.origin);
971
972   list = gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
973
974   for (walk = list; walk; walk = g_list_next (walk)) {
975     GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
976
977     gst_registry_xml_save (registry, " <feature typename=\"%s\">\n",
978         g_type_name (G_OBJECT_TYPE (feature)));
979     gst_registry_xml_save_feature (registry, feature);
980     gst_registry_xml_save (registry, " </feature>\n");
981   }
982
983   gst_plugin_feature_list_free (list);
984
985   return TRUE;
986 }
987
988 gboolean
989 gst_registry_xml_write_cache (GstRegistry * registry, const char *location)
990 {
991   GList *walk;
992   char *tmp_location;
993
994   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
995
996   tmp_location = g_strconcat (location, ".tmp", NULL);
997   registry->cache_file = fopen (tmp_location, "w");
998   if (registry->cache_file == NULL) {
999     char *dir;
1000
1001     /* oops, I bet the directory doesn't exist */
1002     dir = g_path_get_dirname (location);
1003     g_mkdir_with_parents (dir, 0777);
1004     g_free (dir);
1005
1006     registry->cache_file = fopen (tmp_location, "w");
1007   }
1008   if (registry->cache_file == NULL) {
1009     return FALSE;
1010   }
1011
1012   gst_registry_xml_save (registry, "<?xml version=\"1.0\"?>\n");
1013   gst_registry_xml_save (registry, "<GST-PluginRegistry>\n");
1014
1015
1016   for (walk = g_list_last (registry->plugins); walk;
1017       walk = g_list_previous (walk)) {
1018     GstPlugin *plugin = GST_PLUGIN (walk->data);
1019
1020     if (!plugin->filename)
1021       continue;
1022
1023     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1024       int ret;
1025       struct stat statbuf;
1026
1027       ret = g_stat (plugin->filename, &statbuf);
1028       if (ret < 0)
1029         continue;
1030       if (plugin->file_mtime != statbuf.st_mtime ||
1031           plugin->file_size != statbuf.st_size) {
1032         continue;
1033       }
1034     }
1035
1036     gst_registry_xml_save (registry, "<plugin>\n");
1037     gst_registry_xml_save_plugin (registry, plugin);
1038     gst_registry_xml_save (registry, "</plugin>\n");
1039   }
1040   gst_registry_xml_save (registry, "</GST-PluginRegistry>\n");
1041
1042   fclose (registry->cache_file);
1043
1044   if (g_file_test (tmp_location, G_FILE_TEST_EXISTS)) {
1045 #ifdef WIN32
1046     remove (location);
1047 #endif
1048     rename (tmp_location, location);
1049   }
1050   g_free (tmp_location);
1051
1052   return TRUE;
1053 }
1054
1055 #if 0
1056 static GList *
1057 gst_registry_xml_rebuild_recurse (GstRegistry * registry,
1058     const gchar * directory)
1059 {
1060   GList *ret = NULL;
1061   gint dr_len, sf_len;
1062
1063   if (g_file_test (directory, G_FILE_TEST_IS_DIR)) {
1064     GDir *dir = g_dir_open (directory, 0, NULL);
1065
1066     if (dir) {
1067       const gchar *dirent;
1068
1069       while ((dirent = g_dir_read_name (dir))) {
1070         gchar *dirname;
1071
1072         if (*dirent == '=') {
1073           /* =build, =inst, etc. -- automake distcheck directories */
1074           continue;
1075         }
1076
1077         dirname = g_strjoin ("/", directory, dirent, NULL);
1078         ret =
1079             g_list_concat (ret, gst_registry_xml_rebuild_recurse (registry,
1080                 dirname));
1081         g_free (dirname);
1082       }
1083       g_dir_close (dir);
1084     }
1085   } else {
1086     dr_len = strlen (directory);
1087     sf_len = strlen (G_MODULE_SUFFIX);
1088     if (dr_len >= sf_len &&
1089         strcmp (directory + dr_len - sf_len, G_MODULE_SUFFIX) == 0) {
1090       ret = g_list_prepend (ret, g_strdup (directory));
1091     }
1092   }
1093
1094   return ret;
1095 }
1096
1097 static gboolean
1098 gst_registry_xml_rebuild (GstRegistry * registry)
1099 {
1100   GList *walk = NULL, *plugins = NULL, *prune = NULL;
1101   GError *error = NULL;
1102   guint length;
1103   GstPlugin *plugin;
1104   GstRegistry *registry = GST_XML_REGISTRY (registry);
1105
1106   walk = registry->paths;
1107
1108   while (walk) {
1109     gchar *path = (gchar *) walk->data;
1110
1111     GST_CAT_INFO (GST_CAT_PLUGIN_LOADING,
1112         "Rebuilding registry %p in directory %s...", registry, path);
1113
1114     plugins = g_list_concat (plugins,
1115         gst_registry_xml_rebuild_recurse (registry, path));
1116
1117     walk = g_list_next (walk);
1118   }
1119
1120   plugins = g_list_reverse (plugins);
1121
1122   do {
1123     length = g_list_length (plugins);
1124
1125     walk = plugins;
1126     while (walk) {
1127       g_assert (walk->data);
1128       plugin = gst_plugin_load_file ((gchar *) walk->data, NULL);
1129       if (plugin) {
1130         prune = g_list_prepend (prune, walk->data);
1131         gst_registry_add_plugin (registry, plugin);
1132       }
1133
1134       walk = g_list_next (walk);
1135     }
1136
1137     walk = prune;
1138     while (walk) {
1139       plugins = g_list_remove (plugins, walk->data);
1140       g_free (walk->data);
1141       walk = g_list_next (walk);
1142     }
1143     g_list_free (prune);
1144     prune = NULL;
1145   } while (g_list_length (plugins) != length);
1146
1147   walk = plugins;
1148   while (walk) {
1149     if ((plugin = gst_plugin_load_file ((gchar *) walk->data, &error))) {
1150       g_warning ("Bizarre behavior: plugin %s actually loaded",
1151           (gchar *) walk->data);
1152       gst_registry_add_plugin (registry, plugin);
1153     } else {
1154       GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "Plugin %s failed to load: %s",
1155           (gchar *) walk->data, error->message);
1156
1157       g_free (walk->data);
1158       g_error_free (error);
1159       error = NULL;
1160     }
1161
1162     walk = g_list_next (walk);
1163   }
1164   return TRUE;
1165 }
1166 #endif