1 /*****************************************************************************
2 * cache.c: Plugins cache
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
34 #include <sys/types.h>
41 #include <vlc_common.h>
44 #include <vlc_plugin.h>
47 #include "config/configuration.h"
51 #include "modules/modules.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 #ifdef HAVE_DYNAMIC_PLUGINS
59 * (only used to avoid breakage in dev version when cache structure changes) */
60 #define CACHE_SUBVERSION_NUM 22
63 #define CACHE_NAME "plugins.dat"
64 /* Magic for the cache filename */
65 #define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
68 void CacheDelete( vlc_object_t *obj, const char *dir )
72 assert( dir != NULL );
74 if( asprintf( &path, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
76 msg_Dbg( obj, "removing plugins cache file %s", path );
81 #define LOAD_IMMEDIATE(a) \
82 if (fread (&(a), sizeof (char), sizeof (a), file) != sizeof (a)) \
84 #define LOAD_FLAG(a) \
93 static int CacheLoadString (char **p, FILE *file)
98 LOAD_IMMEDIATE (size);
107 psz = malloc (size+1);
108 if (unlikely(psz == NULL))
110 if (fread (psz, 1, size, file) != size)
121 #define LOAD_STRING(a) \
122 if (CacheLoadString (&(a), file)) goto error
124 static int CacheLoadConfig (module_config_t *cfg, FILE *file)
126 LOAD_IMMEDIATE (cfg->i_type);
127 LOAD_IMMEDIATE (cfg->i_short);
128 LOAD_FLAG (cfg->b_advanced);
129 LOAD_FLAG (cfg->b_internal);
130 LOAD_FLAG (cfg->b_unsaveable);
131 LOAD_FLAG (cfg->b_safe);
132 LOAD_FLAG (cfg->b_removed);
133 LOAD_STRING (cfg->psz_type);
134 LOAD_STRING (cfg->psz_name);
135 LOAD_STRING (cfg->psz_text);
136 LOAD_STRING (cfg->psz_longtext);
137 LOAD_IMMEDIATE (cfg->list_count);
139 if (IsConfigStringType (cfg->i_type))
141 LOAD_STRING (cfg->orig.psz);
142 if (cfg->orig.psz != NULL)
143 cfg->value.psz = strdup (cfg->orig.psz);
145 cfg->value.psz = NULL;
148 cfg->list.psz = xmalloc (cfg->list_count * sizeof (char *));
149 else /* TODO: fix config_GetPszChoices() instead of this hack: */
150 LOAD_IMMEDIATE(cfg->list.psz_cb);
151 for (unsigned i = 0; i < cfg->list_count; i++)
153 LOAD_STRING (cfg->list.psz[i]);
154 if (cfg->list.psz[i] == NULL /* NULL -> empty string */
155 && (cfg->list.psz[i] = calloc (1, 1)) == NULL)
161 LOAD_IMMEDIATE (cfg->orig);
162 LOAD_IMMEDIATE (cfg->min);
163 LOAD_IMMEDIATE (cfg->max);
164 cfg->value = cfg->orig;
167 cfg->list.i = xmalloc (cfg->list_count * sizeof (int));
168 else /* TODO: fix config_GetPszChoices() instead of this hack: */
169 LOAD_IMMEDIATE(cfg->list.i_cb);
170 for (unsigned i = 0; i < cfg->list_count; i++)
171 LOAD_IMMEDIATE (cfg->list.i[i]);
173 cfg->list_text = xmalloc (cfg->list_count * sizeof (char *));
174 for (unsigned i = 0; i < cfg->list_count; i++)
176 LOAD_STRING (cfg->list_text[i]);
177 if (cfg->list_text[i] == NULL /* NULL -> empty string */
178 && (cfg->list_text[i] = calloc (1, 1)) == NULL)
184 return -1; /* FIXME: leaks */
187 static int CacheLoadModuleConfig (module_t *module, FILE *file)
191 /* Calculate the structure length */
192 LOAD_IMMEDIATE (module->i_config_items);
193 LOAD_IMMEDIATE (module->i_bool_items);
194 LOAD_IMMEDIATE (lines);
196 /* Allocate memory */
199 module->p_config = malloc (lines * sizeof (module_config_t));
200 if (unlikely(module->p_config == NULL))
202 module->confsize = 0;
207 module->p_config = NULL;
208 module->confsize = lines;
210 /* Do the duplication job */
211 for (size_t i = 0; i < lines; i++)
212 if (CacheLoadConfig (module->p_config + i, file))
216 return -1; /* FIXME: leaks */
221 * Loads a plugins cache file.
223 * This function will load the plugin cache if present and valid. This cache
224 * will in turn be queried by AllocateAllPlugins() to see if it needs to
225 * actually load the dynamically loadable module.
226 * This allows us to only fully load plugins when they are actually used.
228 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
233 char p_cachestring[sizeof(CACHE_STRING)];
237 assert( dir != NULL );
240 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
243 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
245 file = vlc_fopen( psz_filename, "rb" );
248 msg_Warn( p_this, "cannot read %s (%m)",
250 free( psz_filename );
253 free( psz_filename );
255 /* Check the file is a plugins cache */
256 i_size = sizeof(CACHE_STRING) - 1;
257 i_read = fread( p_cachestring, 1, i_size, file );
258 if( i_read != i_size ||
259 memcmp( p_cachestring, CACHE_STRING, i_size ) )
261 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
266 #ifdef DISTRO_VERSION
267 /* Check for distribution specific version */
268 char p_distrostring[sizeof( DISTRO_VERSION )];
269 i_size = sizeof( DISTRO_VERSION ) - 1;
270 i_read = fread( p_distrostring, 1, i_size, file );
271 if( i_read != i_size ||
272 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
274 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
280 /* Check Sub-version number */
281 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
282 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
284 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
285 "(corrupted header)" );
290 /* Check header marker */
291 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
292 if( i_read != sizeof(i_marker) ||
293 i_marker != ftell( file ) - (int)sizeof(i_marker) )
295 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
296 "(corrupted header)" );
301 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
303 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
304 "(file too short)" );
309 module_cache_t *cache = NULL;
311 for (size_t count = 0; count < i_cache;)
316 module = vlc_module_create (NULL);
318 /* Load additional infos */
319 LOAD_STRING(module->psz_shortname);
320 LOAD_STRING(module->psz_longname);
321 LOAD_STRING(module->psz_help);
323 LOAD_IMMEDIATE(module->i_shortcuts);
324 if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
328 module->pp_shortcuts =
329 xmalloc (sizeof (char **) * module->i_shortcuts);
330 for (unsigned j = 0; j < module->i_shortcuts; j++)
331 LOAD_STRING(module->pp_shortcuts[j]);
334 LOAD_STRING(module->psz_capability);
335 LOAD_IMMEDIATE(module->i_score);
336 LOAD_IMMEDIATE(module->b_unloadable);
339 if (CacheLoadModuleConfig (module, file) != VLC_SUCCESS)
342 LOAD_STRING(module->domain);
343 if (module->domain != NULL)
344 vlc_bindtextdomain (module->domain);
346 LOAD_IMMEDIATE( i_submodules );
348 while( i_submodules-- )
350 module_t *submodule = vlc_module_create (module);
351 free (submodule->pp_shortcuts);
352 LOAD_STRING(submodule->psz_shortname);
353 LOAD_STRING(submodule->psz_longname);
355 LOAD_IMMEDIATE(submodule->i_shortcuts);
356 if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
360 submodule->pp_shortcuts =
361 xmalloc (sizeof (char **) * submodule->i_shortcuts);
362 for (unsigned j = 0; j < submodule->i_shortcuts; j++)
363 LOAD_STRING(submodule->pp_shortcuts[j]);
366 LOAD_STRING(submodule->psz_capability);
367 LOAD_IMMEDIATE(submodule->i_score);
373 /* Load common info */
377 LOAD_IMMEDIATE(st.st_mtime);
378 LOAD_IMMEDIATE(st.st_size);
380 CacheAdd (&cache, &count, path, &st, module);
382 /* TODO: deal with errors */
390 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
397 #define SAVE_IMMEDIATE( a ) \
398 if (fwrite (&(a), sizeof(a), 1, file) != 1) \
400 #define SAVE_FLAG(a) \
406 static int CacheSaveString (FILE *file, const char *str)
408 uint16_t size = (str != NULL) ? strlen (str) : 0;
410 SAVE_IMMEDIATE (size);
411 if (size != 0 && fwrite (str, 1, size, file) != size)
419 #define SAVE_STRING( a ) \
420 if (CacheSaveString (file, (a))) \
423 static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
425 SAVE_IMMEDIATE (cfg->i_type);
426 SAVE_IMMEDIATE (cfg->i_short);
427 SAVE_FLAG (cfg->b_advanced);
428 SAVE_FLAG (cfg->b_internal);
429 SAVE_FLAG (cfg->b_unsaveable);
430 SAVE_FLAG (cfg->b_safe);
431 SAVE_FLAG (cfg->b_removed);
432 SAVE_STRING (cfg->psz_type);
433 SAVE_STRING (cfg->psz_name);
434 SAVE_STRING (cfg->psz_text);
435 SAVE_STRING (cfg->psz_longtext);
436 SAVE_IMMEDIATE (cfg->list_count);
438 if (IsConfigStringType (cfg->i_type))
440 SAVE_STRING (cfg->orig.psz);
441 if (cfg->list_count == 0)
442 SAVE_IMMEDIATE (cfg->list.psz_cb); /* XXX: see CacheLoadConfig() */
443 for (unsigned i = 0; i < cfg->list_count; i++)
444 SAVE_STRING (cfg->list.psz[i]);
448 SAVE_IMMEDIATE (cfg->orig);
449 SAVE_IMMEDIATE (cfg->min);
450 SAVE_IMMEDIATE (cfg->max);
451 if (cfg->list_count == 0)
452 SAVE_IMMEDIATE (cfg->list.i_cb); /* XXX: see CacheLoadConfig() */
453 for (unsigned i = 0; i < cfg->list_count; i++)
454 SAVE_IMMEDIATE (cfg->list.i[i]);
456 for (unsigned i = 0; i < cfg->list_count; i++)
457 SAVE_STRING (cfg->list_text[i]);
464 static int CacheSaveModuleConfig (FILE *file, const module_t *module)
466 uint16_t lines = module->confsize;
468 SAVE_IMMEDIATE (module->i_config_items);
469 SAVE_IMMEDIATE (module->i_bool_items);
470 SAVE_IMMEDIATE (lines);
472 for (size_t i = 0; i < lines; i++)
473 if (CacheSaveConfig (file, module->p_config + i))
481 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
484 * Saves a module cache to disk, and release cache data from memory.
486 void CacheSave (vlc_object_t *p_this, const char *dir,
487 module_cache_t *entries, size_t n)
489 char *filename = NULL, *tmpname = NULL;
491 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
494 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
496 msg_Dbg (p_this, "saving plugins cache %s", filename);
498 FILE *file = vlc_fopen (tmpname, "wb");
501 if (errno != EACCES && errno != ENOENT)
502 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
506 if (CacheSaveBank (file, entries, n))
508 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
511 vlc_unlink (tmpname);
515 #if !defined( _WIN32 ) && !defined( __OS2__ )
516 vlc_rename (tmpname, filename); /* atomically replace old cache */
519 vlc_unlink (filename);
521 vlc_rename (tmpname, filename);
527 for (size_t i = 0; i < n; i++)
528 free (entries[i].path);
532 static int CacheSaveSubmodule (FILE *, const module_t *);
534 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
537 uint32_t i_file_size = 0;
539 /* Contains version number */
540 if (fputs (CACHE_STRING, file) == EOF)
542 #ifdef DISTRO_VERSION
543 /* Allow binary maintaner to pass a string to detect new binary version*/
544 if (fputs( DISTRO_VERSION, file ) == EOF)
547 /* Sub-version number (to avoid breakage in the dev version when cache
548 * structure changes) */
549 i_file_size = CACHE_SUBVERSION_NUM;
550 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
554 i_file_size = ftell( file );
555 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
558 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
561 for (unsigned i = 0; i < i_cache; i++)
563 module_t *module = cache[i].p_module;
564 uint32_t i_submodule;
566 /* Save additional infos */
567 SAVE_STRING(module->psz_shortname);
568 SAVE_STRING(module->psz_longname);
569 SAVE_STRING(module->psz_help);
570 SAVE_IMMEDIATE(module->i_shortcuts);
571 for (unsigned j = 0; j < module->i_shortcuts; j++)
572 SAVE_STRING(module->pp_shortcuts[j]);
574 SAVE_STRING(module->psz_capability);
575 SAVE_IMMEDIATE(module->i_score);
576 SAVE_IMMEDIATE(module->b_unloadable);
579 if (CacheSaveModuleConfig (file, module))
582 SAVE_STRING(module->domain);
584 i_submodule = module->submodule_count;
585 SAVE_IMMEDIATE( i_submodule );
586 if (CacheSaveSubmodule (file, module->submodule))
589 /* Save common info */
590 SAVE_STRING(cache[i].path);
591 SAVE_IMMEDIATE(cache[i].mtime);
592 SAVE_IMMEDIATE(cache[i].size);
595 if (fflush (file)) /* flush libc buffers */
597 return 0; /* success! */
603 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
607 if( CacheSaveSubmodule( file, p_module->next ) )
610 SAVE_STRING( p_module->psz_shortname );
611 SAVE_STRING( p_module->psz_longname );
612 SAVE_IMMEDIATE( p_module->i_shortcuts );
613 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
614 SAVE_STRING( p_module->pp_shortcuts[j] );
616 SAVE_STRING( p_module->psz_capability );
617 SAVE_IMMEDIATE( p_module->i_score );
624 /*****************************************************************************
625 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
626 *****************************************************************************/
627 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
631 p_cache->pf_activate = p_module->pf_activate;
632 p_cache->pf_deactivate = p_module->pf_deactivate;
633 p_cache->handle = p_module->handle;
635 /* FIXME: This looks too simplistic an algorithm to me. What if the module
636 * file was altered such that the number of order of submodules was
637 * altered... after VLC started -- Courmisch, 09/2008 */
638 module_t *p_child = p_module->submodule,
639 *p_cchild = p_cache->submodule;
640 while( p_child && p_cchild )
642 p_cchild->pf_activate = p_child->pf_activate;
643 p_cchild->pf_deactivate = p_child->pf_deactivate;
644 p_child = p_child->next;
645 p_cchild = p_cchild->next;
648 p_cache->b_loaded = true;
649 p_module->b_loaded = false;
653 * Looks up a plugin file in a table of cached plugins.
655 module_t *CacheFind (module_cache_t *cache, size_t count,
656 const char *path, const struct stat *st)
660 if (cache->path != NULL
661 && !strcmp (cache->path, path)
662 && cache->mtime == st->st_mtime
663 && cache->size == st->st_size)
665 module_t *module = cache->p_module;
666 cache->p_module = NULL;
676 /** Adds entry to the cache */
677 int CacheAdd (module_cache_t **cachep, size_t *countp,
678 const char *path, const struct stat *st, module_t *module)
680 module_cache_t *cache = *cachep;
681 const size_t count = *countp;
683 cache = realloc (cache, (count + 1) * sizeof (*cache));
684 if (unlikely(cache == NULL))
689 /* NOTE: strdup() could be avoided, but it would be a bit ugly */
690 cache->path = strdup (path);
691 cache->mtime = st->st_mtime;
692 cache->size = st->st_size;
693 cache->p_module = module;
698 #endif /* HAVE_DYNAMIC_PLUGINS */