2 * Copyright (C) 2006 Josep Torra <josep@fluendo.com>
3 * 2006 Mathieu Garcia <matthieu@fluendo.com>
4 * 2006,2007 Stefan Kost <ensonic@users.sf.net>
5 * 2008 Sebastian Dröge <slomo@circular-chaos.org>
7 * gstregistrybinary.c: GstRegistryBinary object, support routines
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * - keep registry binary blob and reference strings
27 * - don't free/unmmap contents when leaving gst_registry_binary_read_cache()
28 * - free at gst_deinit() / _priv_gst_registry_cleanup() ?
30 * - GST_PLUGIN_FLAG_CONST
31 * - GstPluginFeature, GstIndexFactory, GstElementFactory
32 * - needs Flags (GST_PLUGIN_FEATURE_FLAG_CONST)
33 * - can we turn loaded into flag?
34 * - why do we collect a list of binary chunks and not write immediately
35 * - because we need to process subchunks, before we can set e.g. nr_of_items
50 #if defined (_MSC_VER) && _MSC_VER >= 1400
54 #include <gst/gst_private.h>
55 #include <gst/gstconfig.h>
56 #include <gst/gstelement.h>
57 #include <gst/gsttypefind.h>
58 #include <gst/gsttypefindfactory.h>
59 #include <gst/gstdevicemonitorfactory.h>
60 #include <gst/gsturi.h>
61 #include <gst/gstinfo.h>
62 #include <gst/gstenumtypes.h>
63 #include <gst/gstpadtemplate.h>
65 #include <gst/gstregistrychunks.h>
66 #include <gst/gstregistrybinary.h>
68 #include <glib/gstdio.h> /* for g_stat(), g_mapped_file(), ... */
70 #include "glib-compat-private.h"
73 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
76 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
77 if (inptr + sizeof(element) >= endptr) \
79 outptr = (element *) inptr; \
80 inptr += sizeof (element); \
83 #define ALIGNMENT (sizeof (void *))
84 #define alignment(_address) (gsize)_address%ALIGNMENT
85 #define align(_ptr) _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
90 /* On win32, we can't use g_mkstmp(), because of cross-DLL file I/O problems.
91 * So, we just create the entire binary registry in memory, then write it out
92 * with g_file_set_contents(), which creates a temporary file internally
95 typedef struct BinaryRegistryCache
100 } BinaryRegistryCache;
102 static BinaryRegistryCache *
103 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
105 BinaryRegistryCache *cache = g_slice_new0 (BinaryRegistryCache);
106 cache->location = location;
111 gst_registry_binary_cache_write (BinaryRegistryCache * cache,
112 unsigned long offset, const void *data, int length)
114 cache->len = MAX (offset + length, cache->len);
115 cache->mem = g_realloc (cache->mem, cache->len);
117 memcpy (cache->mem + offset, data, length);
123 gst_registry_binary_cache_finish (BinaryRegistryCache * cache, gboolean success)
126 GError *error = NULL;
127 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
128 cache->len, &error)) {
129 /* Probably the directory didn't exist; create it */
131 dir = g_path_get_dirname (cache->location);
132 g_mkdir_with_parents (dir, 0777);
135 g_error_free (error);
138 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
139 cache->len, &error)) {
140 /* Probably the directory didn't exist; create it */
142 dir = g_path_get_dirname (cache->location);
143 g_mkdir_with_parents (dir, 0777);
146 g_error_free (error);
149 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
150 cache->len, &error)) {
151 GST_ERROR ("Failed to write to cache file: %s", error->message);
152 g_error_free (error);
159 g_slice_free (BinaryRegistryCache, cache);
164 typedef struct BinaryRegistryCache
166 const char *location;
168 unsigned long currentoffset;
170 } BinaryRegistryCache;
172 static BinaryRegistryCache *
173 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
175 BinaryRegistryCache *cache = g_slice_new0 (BinaryRegistryCache);
177 cache->location = location;
178 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
179 cache->cache_fd = g_mkstemp (cache->tmp_location);
180 if (cache->cache_fd == -1) {
185 /* oops, I bet the directory doesn't exist */
186 dir = g_path_get_dirname (location);
187 g_mkdir_with_parents (dir, 0777);
189 ret = g_stat (dir, &statbuf);
190 if (ret != -1 && (statbuf.st_mode & 0700) != 0700) {
196 /* the previous g_mkstemp call overwrote the XXXXXX placeholder ... */
197 g_free (cache->tmp_location);
198 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
199 cache->cache_fd = g_mkstemp (cache->tmp_location);
201 if (cache->cache_fd == -1) {
202 GST_DEBUG ("g_mkstemp() failed: %s", g_strerror (errno));
203 g_free (cache->tmp_location);
204 g_slice_free (BinaryRegistryCache, cache);
208 ret = g_stat (cache->tmp_location, &statbuf);
209 if (ret != -1 && (statbuf.st_mode & 0600) != 0600) {
210 g_chmod (cache->tmp_location, 0600);
218 gst_registry_binary_cache_write (BinaryRegistryCache * cache,
219 unsigned long offset, const void *data, int length)
222 if (offset != cache->currentoffset) {
223 if (lseek (cache->cache_fd, offset, SEEK_SET) < 0) {
224 GST_ERROR ("Seeking to new offset failed: %s", g_strerror (errno));
227 GST_LOG ("Seeked from offset %lu to %lu", offset, cache->currentoffset);
228 cache->currentoffset = offset;
231 written = write (cache->cache_fd, data, length);
232 if (written != length) {
233 GST_ERROR ("Failed to write to cache file");
235 cache->currentoffset += written;
241 gst_registry_binary_cache_finish (BinaryRegistryCache * cache, gboolean success)
243 /* only fsync if we're actually going to use and rename the file below */
244 if (success && fsync (cache->cache_fd) < 0)
247 if (close (cache->cache_fd) < 0)
251 goto fail_after_close;
253 /* Only do the rename if we wrote the entire file successfully */
254 if (g_rename (cache->tmp_location, cache->location) < 0) {
255 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
259 g_free (cache->tmp_location);
260 g_slice_free (BinaryRegistryCache, cache);
261 GST_INFO ("Wrote binary registry cache");
267 g_unlink (cache->tmp_location);
268 g_free (cache->tmp_location);
269 g_slice_free (BinaryRegistryCache, cache);
274 GST_ERROR ("fsync() failed: %s", g_strerror (errno));
275 goto fail_after_close;
279 GST_ERROR ("close() failed: %s", g_strerror (errno));
280 goto fail_after_close;
284 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
285 goto fail_after_close;
291 * gst_registry_binary_write_chunk:
293 * Write from a memory location to the registry cache file
295 * Returns: %TRUE for success
297 inline static gboolean
298 gst_registry_binary_write_chunk (BinaryRegistryCache * cache,
299 GstRegistryChunk * chunk, unsigned long *file_position)
301 gchar padder[ALIGNMENT] = { 0, };
304 /* Padding to insert the struct that requiere word alignment */
305 if ((chunk->align) && (alignment (*file_position) != 0)) {
306 padsize = ALIGNMENT - alignment (*file_position);
307 if (gst_registry_binary_cache_write (cache, *file_position,
308 padder, padsize) != padsize) {
309 GST_ERROR ("Failed to write binary registry padder");
312 *file_position += padsize;
315 if (gst_registry_binary_cache_write (cache, *file_position,
316 chunk->data, chunk->size) != chunk->size) {
317 GST_ERROR ("Failed to write binary registry element");
321 *file_position += chunk->size;
328 * gst_registry_binary_initialize_magic:
330 * Initialize the GstBinaryRegistryMagic, setting both our magic number and
331 * gstreamer major/minor version
333 inline static gboolean
334 gst_registry_binary_initialize_magic (GstBinaryRegistryMagic * m)
336 memset (m, 0, sizeof (GstBinaryRegistryMagic));
338 if (!strncpy (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
339 GST_MAGIC_BINARY_REGISTRY_LEN)
340 || !strncpy (m->version, GST_MAGIC_BINARY_VERSION_STR,
341 GST_MAGIC_BINARY_VERSION_LEN)) {
342 GST_ERROR ("Failed to write magic to the registry magic structure");
350 * gst_registry_binary_write_cache:
351 * @registry: a #GstRegistry
352 * @location: a filename
354 * Write the @registry to a cache to file at given @location.
356 * Returns: %TRUE on success.
359 priv_gst_registry_binary_write_cache (GstRegistry * registry, GList * plugins,
360 const char *location)
363 GstBinaryRegistryMagic magic;
364 GList *to_write = NULL;
365 unsigned long file_position = 0;
366 BinaryRegistryCache *cache;
368 GST_INFO ("Building binary registry cache image");
370 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
372 if (!gst_registry_binary_initialize_magic (&magic))
375 /* iterate trough the list of plugins and fit them into binary structures */
376 for (walk = plugins; walk != NULL; walk = walk->next) {
377 GstPlugin *plugin = GST_PLUGIN (walk->data);
379 if (!plugin->filename)
382 if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_CACHED)) {
385 if (g_stat (plugin->filename, &statbuf) < 0 ||
386 plugin->file_mtime != statbuf.st_mtime ||
387 plugin->file_size != statbuf.st_size)
391 if (!_priv_gst_registry_chunks_save_plugin (&to_write, registry, plugin)) {
392 GST_ERROR ("Can't write binary plugin information for \"%s\"",
397 _priv_gst_registry_chunks_save_global_header (&to_write, registry,
398 priv_gst_plugin_loading_get_whitelist_hash ());
400 GST_INFO ("Writing binary registry cache");
402 cache = gst_registry_binary_cache_init (registry, location);
407 if (gst_registry_binary_cache_write (cache, file_position,
408 &magic, sizeof (GstBinaryRegistryMagic)) !=
409 sizeof (GstBinaryRegistryMagic)) {
410 GST_ERROR ("Failed to write binary registry magic");
413 file_position += sizeof (GstBinaryRegistryMagic);
415 /* write out data chunks */
416 for (walk = to_write; walk; walk = g_list_next (walk)) {
417 GstRegistryChunk *cur = walk->data;
420 res = gst_registry_binary_write_chunk (cache, cur, &file_position);
422 _priv_gst_registry_chunk_free (cur);
427 g_list_free (to_write);
429 if (!gst_registry_binary_cache_finish (cache, TRUE))
437 for (walk = to_write; walk; walk = g_list_next (walk)) {
438 GstRegistryChunk *cur = walk->data;
441 _priv_gst_registry_chunk_free (cur);
443 g_list_free (to_write);
446 (void) gst_registry_binary_cache_finish (cache, FALSE);
456 /* Registry loading */
459 * gst_registry_binary_check_magic:
461 * Check GstBinaryRegistryMagic validity.
462 * Return < 0 if something is wrong, -2 means
463 * that just the version of the registry is out of
464 * date, -1 is a general failure.
467 gst_registry_binary_check_magic (gchar ** in, gsize size)
469 GstBinaryRegistryMagic *m;
472 GST_DEBUG ("Reading/casting for GstBinaryRegistryMagic at address %p", *in);
473 unpack_element (*in, m, GstBinaryRegistryMagic, (*in + size), fail);
475 if (strncmp (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
476 GST_MAGIC_BINARY_REGISTRY_LEN) != 0) {
478 ("Binary registry magic is different : %02x%02x%02x%02x != %02x%02x%02x%02x",
479 GST_MAGIC_BINARY_REGISTRY_STR[0] & 0xff,
480 GST_MAGIC_BINARY_REGISTRY_STR[1] & 0xff,
481 GST_MAGIC_BINARY_REGISTRY_STR[2] & 0xff,
482 GST_MAGIC_BINARY_REGISTRY_STR[3] & 0xff, m->magic[0] & 0xff,
483 m->magic[1] & 0xff, m->magic[2] & 0xff, m->magic[3] & 0xff);
486 if (strncmp (m->version, GST_MAGIC_BINARY_VERSION_STR,
487 GST_MAGIC_BINARY_VERSION_LEN)) {
488 GST_WARNING ("Binary registry magic version is different : %s != %s",
489 GST_MAGIC_BINARY_VERSION_STR, m->version);
496 GST_WARNING ("Not enough data for binary registry magic structure");
501 * gst_registry_binary_read_cache:
502 * @registry: a #GstRegistry
503 * @location: a filename
505 * Read the contents of the binary cache file at @location into @registry.
507 * Returns: %TRUE on success.
510 priv_gst_registry_binary_read_cache (GstRegistry * registry,
511 const char *location)
513 GMappedFile *mapped = NULL;
514 gchar *contents = NULL;
518 gboolean res = FALSE;
519 guint32 filter_env_hash = 0;
520 gint check_magic_result;
521 #ifndef GST_DISABLE_GST_DEBUG
522 GTimer *timer = NULL;
526 /* make sure these types exist */
527 GST_TYPE_ELEMENT_FACTORY;
528 GST_TYPE_TYPE_FIND_FACTORY;
529 GST_TYPE_DEVICE_MONITOR_FACTORY;
531 #ifndef GST_DISABLE_GST_DEBUG
532 timer = g_timer_new ();
535 mapped = g_mapped_file_new (location, FALSE, &err);
536 if (G_UNLIKELY (err != NULL)) {
537 GST_INFO ("Unable to mmap file %s : %s", location, err->message);
542 if (mapped == NULL) {
543 /* Error mmap-ing the cache, try a plain memory read */
545 g_file_get_contents (location, &contents, &size, &err);
547 GST_INFO ("Unable to read file %s : %s", location, err->message);
548 #ifndef GST_DISABLE_GST_DEBUG
549 g_timer_destroy (timer);
555 /* This can't fail if g_mapped_file_new() succeeded */
556 contents = g_mapped_file_get_contents (mapped);
557 size = g_mapped_file_get_length (mapped);
560 /* in is a cursor pointer, we initialize it with the begin of registry and is updated on each read */
562 GST_DEBUG ("File data at address %p", in);
563 if (G_UNLIKELY (size < sizeof (GstBinaryRegistryMagic))) {
564 GST_ERROR ("No or broken registry header for file at %s", location);
568 /* check if header is valid */
569 if (G_UNLIKELY ((check_magic_result =
570 gst_registry_binary_check_magic (&in, size)) < 0)) {
572 if (check_magic_result == -1)
574 ("Binary registry type not recognized (invalid magic) for file at %s",
579 if (!_priv_gst_registry_chunks_load_global_header (registry, &in,
580 contents + size, &filter_env_hash)) {
581 GST_ERROR ("Couldn't read global header chunk");
585 if (filter_env_hash != priv_gst_plugin_loading_get_whitelist_hash ()) {
586 GST_INFO_OBJECT (registry, "Plugin loading filter environment changed, "
587 "ignoring plugin cache to force update with new filter environment");
591 /* check if there are plugins in the file */
592 if (G_UNLIKELY (!(((gsize) in + sizeof (GstRegistryChunkPluginElement)) <
593 (gsize) contents + size))) {
594 GST_INFO ("No binary plugins structure to read");
595 /* empty file, this is not an error */
597 gchar *end = contents + size;
598 /* read as long as we still have space for a GstRegistryChunkPluginElement */
600 ((gsize) in + sizeof (GstRegistryChunkPluginElement)) <
601 (gsize) contents + size;) {
602 GST_DEBUG ("reading binary registry %" G_GSIZE_FORMAT "(%x)/%"
603 G_GSIZE_FORMAT, (gsize) in - (gsize) contents,
604 (guint) ((gsize) in - (gsize) contents), size);
605 if (!_priv_gst_registry_chunks_load_plugin (registry, &in, end, NULL)) {
606 GST_ERROR ("Problem while reading binary registry %s", location);
614 #ifndef GST_DISABLE_GST_DEBUG
615 g_timer_stop (timer);
616 seconds = g_timer_elapsed (timer, NULL);
619 GST_INFO ("loaded %s in %lf seconds", location, seconds);
622 /* TODO: once we re-use the pointers to registry contents, return here */
625 #ifndef GST_DISABLE_GST_DEBUG
626 g_timer_destroy (timer);
629 g_mapped_file_unref (mapped);