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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, 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/gsturi.h>
60 #include <gst/gstinfo.h>
61 #include <gst/gstenumtypes.h>
62 #include <gst/gstpadtemplate.h>
64 #include <gst/gstregistrychunks.h>
65 #include <gst/gstregistrybinary.h>
67 #include <glib/gstdio.h> /* for g_stat(), g_mapped_file(), ... */
69 #include "glib-compat-private.h"
72 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
75 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
76 if (inptr + sizeof(element) >= endptr) \
78 outptr = (element *) inptr; \
79 inptr += sizeof (element); \
82 #define ALIGNMENT (sizeof (void *))
83 #define alignment(_address) (gsize)_address%ALIGNMENT
84 #define align(_ptr) _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
89 /* On win32, we can't use g_mkstmp(), because of cross-DLL file I/O problems.
90 * So, we just create the entire binary registry in memory, then write it out
91 * with g_file_set_contents(), which creates a temporary file internally
94 typedef struct BinaryRegistryCache
99 } BinaryRegistryCache;
101 static BinaryRegistryCache *
102 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
104 BinaryRegistryCache *cache = g_slice_new0 (BinaryRegistryCache);
105 cache->location = location;
110 gst_registry_binary_cache_write (BinaryRegistryCache * cache,
111 unsigned long offset, const void *data, int length)
113 cache->len = MAX (offset + length, cache->len);
114 cache->mem = g_realloc (cache->mem, cache->len);
116 memcpy (cache->mem + offset, data, length);
122 gst_registry_binary_cache_finish (BinaryRegistryCache * cache, gboolean success)
125 GError *error = NULL;
126 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
127 cache->len, &error)) {
128 /* Probably the directory didn't exist; create it */
130 dir = g_path_get_dirname (cache->location);
131 g_mkdir_with_parents (dir, 0777);
134 g_error_free (error);
137 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
138 cache->len, &error)) {
139 /* Probably the directory didn't exist; create it */
141 dir = g_path_get_dirname (cache->location);
142 g_mkdir_with_parents (dir, 0777);
145 g_error_free (error);
148 if (!g_file_set_contents (cache->location, (const gchar *) cache->mem,
149 cache->len, &error)) {
150 GST_ERROR ("Failed to write to cache file: %s", error->message);
151 g_error_free (error);
158 g_slice_free (BinaryRegistryCache, cache);
163 typedef struct BinaryRegistryCache
165 const char *location;
167 unsigned long currentoffset;
169 } BinaryRegistryCache;
171 static BinaryRegistryCache *
172 gst_registry_binary_cache_init (GstRegistry * registry, const char *location)
174 BinaryRegistryCache *cache = g_slice_new0 (BinaryRegistryCache);
176 cache->location = location;
177 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
178 cache->cache_fd = g_mkstemp (cache->tmp_location);
179 if (cache->cache_fd == -1) {
184 /* oops, I bet the directory doesn't exist */
185 dir = g_path_get_dirname (location);
186 g_mkdir_with_parents (dir, 0777);
188 ret = g_stat (dir, &statbuf);
189 if (ret != -1 && (statbuf.st_mode & 0700) != 0700) {
195 /* the previous g_mkstemp call overwrote the XXXXXX placeholder ... */
196 g_free (cache->tmp_location);
197 cache->tmp_location = g_strconcat (location, ".tmpXXXXXX", NULL);
198 cache->cache_fd = g_mkstemp (cache->tmp_location);
200 if (cache->cache_fd == -1) {
201 GST_DEBUG ("g_mkstemp() failed: %s", g_strerror (errno));
202 g_free (cache->tmp_location);
203 g_slice_free (BinaryRegistryCache, cache);
207 ret = g_stat (cache->tmp_location, &statbuf);
208 if (ret != -1 && (statbuf.st_mode & 0600) != 0600) {
209 g_chmod (cache->tmp_location, 0600);
217 gst_registry_binary_cache_write (BinaryRegistryCache * cache,
218 unsigned long offset, const void *data, int length)
221 if (offset != cache->currentoffset) {
222 if (lseek (cache->cache_fd, offset, SEEK_SET) != 0) {
223 GST_ERROR ("Seeking to new offset failed");
226 cache->currentoffset = offset;
229 written = write (cache->cache_fd, data, length);
230 if (written != length) {
231 GST_ERROR ("Failed to write to cache file");
233 cache->currentoffset += written;
239 gst_registry_binary_cache_finish (BinaryRegistryCache * cache, gboolean success)
241 /* only fsync if we're actually going to use and rename the file below */
242 if (success && fsync (cache->cache_fd) < 0)
245 if (close (cache->cache_fd) < 0)
249 /* Only do the rename if we wrote the entire file successfully */
250 if (g_rename (cache->tmp_location, cache->location) < 0) {
251 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
256 g_free (cache->tmp_location);
257 g_slice_free (BinaryRegistryCache, cache);
258 GST_INFO ("Wrote binary registry cache");
264 g_unlink (cache->tmp_location);
265 g_free (cache->tmp_location);
266 g_slice_free (BinaryRegistryCache, cache);
271 GST_ERROR ("fsync() failed: %s", g_strerror (errno));
272 goto fail_after_close;
276 GST_ERROR ("close() failed: %s", g_strerror (errno));
277 goto fail_after_close;
281 GST_ERROR ("g_rename() failed: %s", g_strerror (errno));
282 goto fail_after_close;
288 * gst_registry_binary_write_chunk:
290 * Write from a memory location to the registry cache file
292 * Returns: %TRUE for success
294 inline static gboolean
295 gst_registry_binary_write_chunk (BinaryRegistryCache * cache,
296 GstRegistryChunk * chunk, unsigned long *file_position)
298 gchar padder[ALIGNMENT] = { 0, };
301 /* Padding to insert the struct that requiere word alignment */
302 if ((chunk->align) && (alignment (*file_position) != 0)) {
303 padsize = ALIGNMENT - alignment (*file_position);
304 if (gst_registry_binary_cache_write (cache, *file_position,
305 padder, padsize) != padsize) {
306 GST_ERROR ("Failed to write binary registry padder");
309 *file_position += padsize;
312 if (gst_registry_binary_cache_write (cache, *file_position,
313 chunk->data, chunk->size) != chunk->size) {
314 GST_ERROR ("Failed to write binary registry element");
318 *file_position += chunk->size;
325 * gst_registry_binary_initialize_magic:
327 * Initialize the GstBinaryRegistryMagic, setting both our magic number and
328 * gstreamer major/minor version
330 inline static gboolean
331 gst_registry_binary_initialize_magic (GstBinaryRegistryMagic * m)
333 memset (m, 0, sizeof (GstBinaryRegistryMagic));
335 if (!strncpy (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
336 GST_MAGIC_BINARY_REGISTRY_LEN)
337 || !strncpy (m->version, GST_MAGIC_BINARY_VERSION_STR,
338 GST_MAGIC_BINARY_VERSION_LEN)) {
339 GST_ERROR ("Failed to write magic to the registry magic structure");
347 * gst_registry_binary_write_cache:
348 * @registry: a #GstRegistry
349 * @location: a filename
351 * Write the @registry to a cache to file at given @location.
353 * Returns: %TRUE on success.
356 priv_gst_registry_binary_write_cache (GstRegistry * registry,
357 const char *location)
360 GstBinaryRegistryMagic magic;
361 GList *to_write = NULL;
362 unsigned long file_position = 0;
363 BinaryRegistryCache *cache;
365 GST_INFO ("Building binary registry cache image");
367 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
369 if (!gst_registry_binary_initialize_magic (&magic))
372 /* iterate trough the list of plugins and fit them into binary structures */
373 for (walk = registry->plugins; walk; walk = g_list_next (walk)) {
374 GstPlugin *plugin = GST_PLUGIN (walk->data);
376 if (!plugin->filename)
379 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
382 if (g_stat (plugin->filename, &statbuf) < 0 ||
383 plugin->file_mtime != statbuf.st_mtime ||
384 plugin->file_size != statbuf.st_size)
388 if (!_priv_gst_registry_chunks_save_plugin (&to_write, registry, plugin)) {
389 GST_ERROR ("Can't write binary plugin information for \"%s\"",
394 _priv_gst_registry_chunks_save_global_header (&to_write, registry,
395 priv_gst_plugin_loading_get_whitelist_hash ());
397 GST_INFO ("Writing binary registry cache");
399 cache = gst_registry_binary_cache_init (registry, location);
404 if (gst_registry_binary_cache_write (cache, file_position,
405 &magic, sizeof (GstBinaryRegistryMagic)) !=
406 sizeof (GstBinaryRegistryMagic)) {
407 GST_ERROR ("Failed to write binary registry magic");
410 file_position += sizeof (GstBinaryRegistryMagic);
412 /* write out data chunks */
413 for (walk = to_write; walk; walk = g_list_next (walk)) {
414 GstRegistryChunk *cur = walk->data;
417 res = gst_registry_binary_write_chunk (cache, cur, &file_position);
419 _priv_gst_registry_chunk_free (cur);
424 g_list_free (to_write);
426 if (!gst_registry_binary_cache_finish (cache, TRUE))
434 for (walk = to_write; walk; walk = g_list_next (walk)) {
435 GstRegistryChunk *cur = walk->data;
438 _priv_gst_registry_chunk_free (cur);
440 g_list_free (to_write);
443 (void) gst_registry_binary_cache_finish (cache, FALSE);
453 /* Registry loading */
456 * gst_registry_binary_check_magic:
458 * Check GstBinaryRegistryMagic validity.
459 * Return < 0 if something is wrong, -2 means
460 * that just the version of the registry is out of
461 * date, -1 is a general failure.
464 gst_registry_binary_check_magic (gchar ** in, gsize size)
466 GstBinaryRegistryMagic *m;
469 GST_DEBUG ("Reading/casting for GstBinaryRegistryMagic at address %p", *in);
470 unpack_element (*in, m, GstBinaryRegistryMagic, (*in + size), fail);
472 if (strncmp (m->magic, GST_MAGIC_BINARY_REGISTRY_STR,
473 GST_MAGIC_BINARY_REGISTRY_LEN) != 0) {
475 ("Binary registry magic is different : %02x%02x%02x%02x != %02x%02x%02x%02x",
476 GST_MAGIC_BINARY_REGISTRY_STR[0] & 0xff,
477 GST_MAGIC_BINARY_REGISTRY_STR[1] & 0xff,
478 GST_MAGIC_BINARY_REGISTRY_STR[2] & 0xff,
479 GST_MAGIC_BINARY_REGISTRY_STR[3] & 0xff, m->magic[0] & 0xff,
480 m->magic[1] & 0xff, m->magic[2] & 0xff, m->magic[3] & 0xff);
483 if (strncmp (m->version, GST_MAGIC_BINARY_VERSION_STR,
484 GST_MAGIC_BINARY_VERSION_LEN)) {
485 GST_WARNING ("Binary registry magic version is different : %s != %s",
486 GST_MAGIC_BINARY_VERSION_STR, m->version);
493 GST_WARNING ("Not enough data for binary registry magic structure");
498 * gst_registry_binary_read_cache:
499 * @registry: a #GstRegistry
500 * @location: a filename
502 * Read the contents of the binary cache file at @location into @registry.
504 * Returns: %TRUE on success.
507 priv_gst_registry_binary_read_cache (GstRegistry * registry,
508 const char *location)
510 GMappedFile *mapped = NULL;
511 gchar *contents = NULL;
515 gboolean res = FALSE;
516 guint32 filter_env_hash = 0;
517 gint check_magic_result;
518 #ifndef GST_DISABLE_GST_DEBUG
519 GTimer *timer = NULL;
523 /* make sure these types exist */
524 GST_TYPE_ELEMENT_FACTORY;
525 GST_TYPE_TYPE_FIND_FACTORY;
526 GST_TYPE_INDEX_FACTORY;
528 #ifndef GST_DISABLE_GST_DEBUG
529 timer = g_timer_new ();
532 mapped = g_mapped_file_new (location, FALSE, &err);
533 if (G_UNLIKELY (err != NULL)) {
534 GST_INFO ("Unable to mmap file %s : %s", location, err->message);
539 if (mapped == NULL) {
540 /* Error mmap-ing the cache, try a plain memory read */
542 g_file_get_contents (location, &contents, &size, &err);
544 GST_INFO ("Unable to read file %s : %s", location, err->message);
545 #ifndef GST_DISABLE_GST_DEBUG
546 g_timer_destroy (timer);
552 /* This can't fail if g_mapped_file_new() succeeded */
553 contents = g_mapped_file_get_contents (mapped);
554 size = g_mapped_file_get_length (mapped);
557 /* in is a cursor pointer, we initialize it with the begin of registry and is updated on each read */
559 GST_DEBUG ("File data at address %p", in);
560 if (G_UNLIKELY (size < sizeof (GstBinaryRegistryMagic))) {
561 GST_ERROR ("No or broken registry header for file at %s", location);
565 /* check if header is valid */
566 if (G_UNLIKELY ((check_magic_result =
567 gst_registry_binary_check_magic (&in, size)) < 0)) {
569 if (check_magic_result == -1)
571 ("Binary registry type not recognized (invalid magic) for file at %s",
576 if (!_priv_gst_registry_chunks_load_global_header (registry, &in,
577 contents + size, &filter_env_hash)) {
578 GST_ERROR ("Couldn't read global header chunk");
582 if (filter_env_hash != priv_gst_plugin_loading_get_whitelist_hash ()) {
583 GST_INFO_OBJECT (registry, "Plugin loading filter environment changed, "
584 "ignoring plugin cache to force update with new filter environment");
588 /* check if there are plugins in the file */
589 if (G_UNLIKELY (!(((gsize) in + sizeof (GstRegistryChunkPluginElement)) <
590 (gsize) contents + size))) {
591 GST_INFO ("No binary plugins structure to read");
592 /* empty file, this is not an error */
594 gchar *end = contents + size;
595 /* read as long as we still have space for a GstRegistryChunkPluginElement */
597 ((gsize) in + sizeof (GstRegistryChunkPluginElement)) <
598 (gsize) contents + size;) {
599 GST_DEBUG ("reading binary registry %" G_GSIZE_FORMAT "(%x)/%"
600 G_GSIZE_FORMAT, (gsize) in - (gsize) contents,
601 (guint) ((gsize) in - (gsize) contents), size);
602 if (!_priv_gst_registry_chunks_load_plugin (registry, &in, end, NULL)) {
603 GST_ERROR ("Problem while reading binary registry %s", location);
611 #ifndef GST_DISABLE_GST_DEBUG
612 g_timer_stop (timer);
613 seconds = g_timer_elapsed (timer, NULL);
616 GST_INFO ("loaded %s in %lf seconds", location, seconds);
619 /* TODO: once we re-use the pointers to registry contents, return here */
622 #ifndef GST_DISABLE_GST_DEBUG
623 g_timer_destroy (timer);
626 g_mapped_file_unref (mapped);