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/gstdeviceproviderfactory.h>
60 #include <gst/gstdynamictypefactory.h>
61 #include <gst/gsturi.h>
62 #include <gst/gstinfo.h>
63 #include <gst/gstenumtypes.h>
64 #include <gst/gstpadtemplate.h>
66 #include <gst/gstregistrychunks.h>
67 #include <gst/gstregistrybinary.h>
69 #include <glib/gstdio.h> /* for g_stat(), g_mapped_file(), ... */
71 #include "glib-compat-private.h"
74 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
77 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
78 if (inptr + sizeof(element) >= endptr) \
80 outptr = (element *) inptr; \
81 inptr += sizeof (element); \
84 #define ALIGNMENT (sizeof (void *))
85 #define alignment(_address) (gsize)_address%ALIGNMENT
86 #define align(_ptr) _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
91 /* On win32, we can't use g_mkstmp(), because of cross-DLL file I/O problems.
92 * So, we just create the entire binary registry in memory, then write it out
93 * with g_file_set_contents(), which creates a temporary file internally
96 typedef struct BinaryRegistryCache
101 } BinaryRegistryCache;
103 static BinaryRegistryCache *
104 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
106 BinaryRegistryCache *cache = g_slice_new0 (BinaryRegistryCache);
107 cache->location = location;
112 gst_registry_binary_cache_write (BinaryRegistryCache * cache,
113 unsigned long offset, const void *data, int length)
115 cache->len = MAX (offset + length, cache->len);
116 cache->mem = g_realloc (cache->mem, cache->len);
118 memcpy (cache->mem + offset, data, length);
124 gst_registry_binary_cache_finish (BinaryRegistryCache * cache, gboolean success)
127 GError *error = NULL;
128 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
129 cache->len, &error)) {
130 /* Probably the directory didn't exist; create it */
132 dir = g_path_get_dirname (cache->location);
133 g_mkdir_with_parents (dir, 0777);
136 g_error_free (error);
139 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
140 cache->len, &error)) {
141 /* Probably the directory didn't exist; create it */
143 dir = g_path_get_dirname (cache->location);
144 g_mkdir_with_parents (dir, 0777);
147 g_error_free (error);
150 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
151 cache->len, &error)) {
152 GST_ERROR ("Failed to write to cache file: %s", error->message);
153 g_error_free (error);
160 g_slice_free (BinaryRegistryCache, cache);
165 typedef struct BinaryRegistryCache
167 const char *location;
169 unsigned long currentoffset;
171 } BinaryRegistryCache;
173 static BinaryRegistryCache *
174 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
176 BinaryRegistryCache *cache = g_slice_new0 (BinaryRegistryCache);
178 cache->location = location;
179 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
180 cache->cache_fd = g_mkstemp (cache->tmp_location);
181 if (cache->cache_fd == -1) {
186 /* oops, I bet the directory doesn't exist */
187 dir = g_path_get_dirname (location);
188 g_mkdir_with_parents (dir, 0777);
190 ret = g_stat (dir, &statbuf);
191 if (ret != -1 && (statbuf.st_mode & 0700) != 0700) {
197 /* the previous g_mkstemp call overwrote the XXXXXX placeholder ... */
198 g_free (cache->tmp_location);
199 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
200 cache->cache_fd = g_mkstemp (cache->tmp_location);
202 if (cache->cache_fd == -1) {
203 GST_DEBUG ("g_mkstemp() failed: %s", g_strerror (errno));
204 g_free (cache->tmp_location);
205 g_slice_free (BinaryRegistryCache, cache);
209 ret = g_stat (cache->tmp_location, &statbuf);
210 if (ret != -1 && (statbuf.st_mode & 0600) != 0600) {
211 g_chmod (cache->tmp_location, 0600);
219 gst_registry_binary_cache_write (BinaryRegistryCache * cache,
220 unsigned long offset, const void *data, int length)
223 if (offset != cache->currentoffset) {
224 if (lseek (cache->cache_fd, offset, SEEK_SET) < 0) {
225 GST_ERROR ("Seeking to new offset failed: %s", g_strerror (errno));
228 GST_LOG ("Seeked from offset %lu to %lu", offset, cache->currentoffset);
229 cache->currentoffset = offset;
232 written = write (cache->cache_fd, data, length);
233 if (written != length) {
234 GST_ERROR ("Failed to write to cache file");
236 cache->currentoffset += written;
242 gst_registry_binary_cache_finish (BinaryRegistryCache * cache, gboolean success)
244 /* only fsync if we're actually going to use and rename the file below */
245 if (success && fsync (cache->cache_fd) < 0)
248 if (close (cache->cache_fd) < 0)
252 goto fail_after_close;
254 /* Only do the rename if we wrote the entire file successfully */
255 if (g_rename (cache->tmp_location, cache->location) < 0) {
256 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
260 g_free (cache->tmp_location);
261 g_slice_free (BinaryRegistryCache, cache);
262 GST_INFO ("Wrote binary registry cache");
268 g_unlink (cache->tmp_location);
269 g_free (cache->tmp_location);
270 g_slice_free (BinaryRegistryCache, cache);
275 GST_ERROR ("fsync() failed: %s", g_strerror (errno));
276 goto fail_after_close;
280 GST_ERROR ("close() failed: %s", g_strerror (errno));
281 goto fail_after_close;
285 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
286 goto fail_after_close;
292 * gst_registry_binary_write_chunk:
294 * Write from a memory location to the registry cache file
296 * Returns: %TRUE for success
298 inline static gboolean
299 gst_registry_binary_write_chunk (BinaryRegistryCache * cache,
300 GstRegistryChunk * chunk, unsigned long *file_position)
302 gchar padder[ALIGNMENT] = { 0, };
305 /* Padding to insert the struct that requiere word alignment */
306 if ((chunk->align) && (alignment (*file_position) != 0)) {
307 padsize = ALIGNMENT - alignment (*file_position);
308 if (gst_registry_binary_cache_write (cache, *file_position,
309 padder, padsize) != padsize) {
310 GST_ERROR ("Failed to write binary registry padder");
313 *file_position += padsize;
316 if (gst_registry_binary_cache_write (cache, *file_position,
317 chunk->data, chunk->size) != chunk->size) {
318 GST_ERROR ("Failed to write binary registry element");
322 *file_position += chunk->size;
329 * gst_registry_binary_initialize_magic:
331 * Initialize the GstBinaryRegistryMagic, setting both our magic number and
332 * gstreamer major/minor version
334 inline static gboolean
335 gst_registry_binary_initialize_magic (GstBinaryRegistryMagic * m)
337 memset (m, 0, sizeof (GstBinaryRegistryMagic));
339 if (!strncpy (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
340 GST_MAGIC_BINARY_REGISTRY_LEN)
341 || !strncpy (m->version, GST_MAGIC_BINARY_VERSION_STR,
342 GST_MAGIC_BINARY_VERSION_LEN)) {
343 GST_ERROR ("Failed to write magic to the registry magic structure");
351 * gst_registry_binary_write_cache:
352 * @registry: a #GstRegistry
353 * @location: a filename
355 * Write the @registry to a cache to file at given @location.
357 * Returns: %TRUE on success.
360 priv_gst_registry_binary_write_cache (GstRegistry * registry, GList * plugins,
361 const char *location)
364 GstBinaryRegistryMagic magic;
365 GList *to_write = NULL;
366 unsigned long file_position = 0;
367 BinaryRegistryCache *cache;
369 GST_INFO ("Building binary registry cache image");
371 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
373 if (!gst_registry_binary_initialize_magic (&magic))
376 /* iterate trough the list of plugins and fit them into binary structures */
377 for (walk = plugins; walk != NULL; walk = walk->next) {
378 GstPlugin *plugin = GST_PLUGIN (walk->data);
380 if (!plugin->filename)
383 if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_CACHED)) {
386 if (g_stat (plugin->filename, &statbuf) < 0 ||
387 plugin->file_mtime != statbuf.st_mtime ||
388 plugin->file_size != statbuf.st_size)
392 if (!_priv_gst_registry_chunks_save_plugin (&to_write, registry, plugin)) {
393 GST_ERROR ("Can't write binary plugin information for \"%s\"",
398 _priv_gst_registry_chunks_save_global_header (&to_write, registry,
399 priv_gst_plugin_loading_get_whitelist_hash ());
401 GST_INFO ("Writing binary registry cache");
403 cache = gst_registry_binary_cache_init (registry, location);
408 if (gst_registry_binary_cache_write (cache, file_position,
409 &magic, sizeof (GstBinaryRegistryMagic)) !=
410 sizeof (GstBinaryRegistryMagic)) {
411 GST_ERROR ("Failed to write binary registry magic");
414 file_position += sizeof (GstBinaryRegistryMagic);
416 /* write out data chunks */
417 for (walk = to_write; walk; walk = g_list_next (walk)) {
418 GstRegistryChunk *cur = walk->data;
421 res = gst_registry_binary_write_chunk (cache, cur, &file_position);
423 _priv_gst_registry_chunk_free (cur);
428 g_list_free (to_write);
430 if (!gst_registry_binary_cache_finish (cache, TRUE))
438 for (walk = to_write; walk; walk = g_list_next (walk)) {
439 GstRegistryChunk *cur = walk->data;
442 _priv_gst_registry_chunk_free (cur);
444 g_list_free (to_write);
447 (void) gst_registry_binary_cache_finish (cache, FALSE);
457 /* Registry loading */
460 * gst_registry_binary_check_magic:
462 * Check GstBinaryRegistryMagic validity.
463 * Return < 0 if something is wrong, -2 means
464 * that just the version of the registry is out of
465 * date, -1 is a general failure.
468 gst_registry_binary_check_magic (gchar ** in, gsize size)
470 GstBinaryRegistryMagic *m;
473 GST_DEBUG ("Reading/casting for GstBinaryRegistryMagic at address %p", *in);
474 unpack_element (*in, m, GstBinaryRegistryMagic, (*in + size), fail);
476 if (strncmp (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
477 GST_MAGIC_BINARY_REGISTRY_LEN) != 0) {
479 ("Binary registry magic is different : %02x%02x%02x%02x != %02x%02x%02x%02x",
480 GST_MAGIC_BINARY_REGISTRY_STR[0] & 0xff,
481 GST_MAGIC_BINARY_REGISTRY_STR[1] & 0xff,
482 GST_MAGIC_BINARY_REGISTRY_STR[2] & 0xff,
483 GST_MAGIC_BINARY_REGISTRY_STR[3] & 0xff, m->magic[0] & 0xff,
484 m->magic[1] & 0xff, m->magic[2] & 0xff, m->magic[3] & 0xff);
487 if (strncmp (m->version, GST_MAGIC_BINARY_VERSION_STR,
488 GST_MAGIC_BINARY_VERSION_LEN)) {
489 GST_WARNING ("Binary registry magic version is different : %s != %s",
490 GST_MAGIC_BINARY_VERSION_STR, m->version);
497 GST_WARNING ("Not enough data for binary registry magic structure");
502 * gst_registry_binary_read_cache:
503 * @registry: a #GstRegistry
504 * @location: a filename
506 * Read the contents of the binary cache file at @location into @registry.
508 * Returns: %TRUE on success.
511 priv_gst_registry_binary_read_cache (GstRegistry * registry,
512 const char *location)
514 GMappedFile *mapped = NULL;
515 gchar *contents = NULL;
519 gboolean res = FALSE;
520 guint32 filter_env_hash = 0;
521 gint check_magic_result;
522 #ifndef GST_DISABLE_GST_DEBUG
523 GTimer *timer = NULL;
527 /* make sure these types exist */
528 GST_TYPE_ELEMENT_FACTORY;
529 GST_TYPE_TYPE_FIND_FACTORY;
530 GST_TYPE_DEVICE_PROVIDER_FACTORY;
531 GST_TYPE_DYNAMIC_TYPE_FACTORY;
533 #ifndef GST_DISABLE_GST_DEBUG
534 timer = g_timer_new ();
537 mapped = g_mapped_file_new (location, FALSE, &err);
538 if (G_UNLIKELY (err != NULL)) {
539 GST_INFO ("Unable to mmap file %s : %s", location, err->message);
544 if (mapped == NULL) {
545 /* Error mmap-ing the cache, try a plain memory read */
547 g_file_get_contents (location, &contents, &size, &err);
549 GST_INFO ("Unable to read file %s : %s", location, err->message);
550 #ifndef GST_DISABLE_GST_DEBUG
551 g_timer_destroy (timer);
557 /* This can't fail if g_mapped_file_new() succeeded */
558 contents = g_mapped_file_get_contents (mapped);
559 size = g_mapped_file_get_length (mapped);
562 /* in is a cursor pointer, we initialize it with the begin of registry and is updated on each read */
564 GST_DEBUG ("File data at address %p", in);
565 if (G_UNLIKELY (size < sizeof (GstBinaryRegistryMagic))) {
566 GST_ERROR ("No or broken registry header for file at %s", location);
570 /* check if header is valid */
571 if (G_UNLIKELY ((check_magic_result =
572 gst_registry_binary_check_magic (&in, size)) < 0)) {
574 if (check_magic_result == -1)
576 ("Binary registry type not recognized (invalid magic) for file at %s",
581 if (!_priv_gst_registry_chunks_load_global_header (registry, &in,
582 contents + size, &filter_env_hash)) {
583 GST_ERROR ("Couldn't read global header chunk");
587 if (filter_env_hash != priv_gst_plugin_loading_get_whitelist_hash ()) {
588 GST_INFO_OBJECT (registry, "Plugin loading filter environment changed, "
589 "ignoring plugin cache to force update with new filter environment");
593 /* check if there are plugins in the file */
594 if (G_UNLIKELY (!(((gsize) in + sizeof (GstRegistryChunkPluginElement)) <
595 (gsize) contents + size))) {
596 GST_INFO ("No binary plugins structure to read");
597 /* empty file, this is not an error */
599 gchar *end = contents + size;
600 /* read as long as we still have space for a GstRegistryChunkPluginElement */
602 ((gsize) in + sizeof (GstRegistryChunkPluginElement)) <
603 (gsize) contents + size;) {
604 GST_DEBUG ("reading binary registry %" G_GSIZE_FORMAT "(%x)/%"
605 G_GSIZE_FORMAT, (gsize) in - (gsize) contents,
606 (guint) ((gsize) in - (gsize) contents), size);
607 if (!_priv_gst_registry_chunks_load_plugin (registry, &in, end, NULL)) {
608 GST_ERROR ("Problem while reading binary registry %s", location);
616 #ifndef GST_DISABLE_GST_DEBUG
617 g_timer_stop (timer);
618 seconds = g_timer_elapsed (timer, NULL);
621 GST_INFO ("loaded %s in %lf seconds", location, seconds);
624 /* TODO: once we re-use the pointers to registry contents, return here */
627 #ifndef GST_DISABLE_GST_DEBUG
628 g_timer_destroy (timer);
631 g_mapped_file_unref (mapped);