1 /*****************************************************************************
2 * cache.c: Plugins cache
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include <vlc_common.h>
34 #include <stdlib.h> /* free(), strtol() */
35 #include <stdio.h> /* sprintf() */
36 #include <string.h> /* strdup() */
37 #include <vlc_plugin.h>
40 #include <sys/types.h>
46 #include "config/configuration.h"
50 #include "modules/modules.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 #ifdef HAVE_DYNAMIC_PLUGINS
57 static int CacheLoadConfig ( module_t *, FILE * );
60 * (only used to avoid breakage in dev version when cache structure changes) */
61 #define CACHE_SUBVERSION_NUM 14
64 #define CACHE_NAME "plugins.dat"
65 /* Magic for the cache filename */
66 #define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
69 void CacheDelete( vlc_object_t *obj, const char *dir )
73 assert( dir != NULL );
75 if( asprintf( &path, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
77 msg_Dbg( obj, "removing plugins cache file %s", path );
83 * Loads a plugins cache file.
85 * This function will load the plugin cache if present and valid. This cache
86 * will in turn be queried by AllocateAllPlugins() to see if it needs to
87 * actually load the dynamically loadable module.
88 * This allows us to only fully load plugins when they are actually used.
90 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r )
95 char p_cachestring[sizeof(CACHE_STRING)];
97 int32_t i_file_size, i_marker;
99 assert( dir != NULL );
102 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
105 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
107 file = vlc_fopen( psz_filename, "rb" );
110 msg_Warn( p_this, "cannot read %s (%m)",
112 free( psz_filename );
115 free( psz_filename );
117 /* Check the file size */
118 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
119 if( i_read != sizeof(i_file_size) )
121 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
127 fseek( file, 0, SEEK_END );
128 if( ftell( file ) != i_file_size )
130 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
131 "(corrupted size)" );
135 fseek( file, sizeof(i_file_size), SEEK_SET );
137 /* Check the file is a plugins cache */
138 i_size = sizeof(CACHE_STRING) - 1;
139 i_read = fread( p_cachestring, 1, i_size, file );
140 if( i_read != i_size ||
141 memcmp( p_cachestring, CACHE_STRING, i_size ) )
143 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
148 #ifdef DISTRO_VERSION
149 /* Check for distribution specific version */
150 char p_distrostring[sizeof( DISTRO_VERSION )];
151 i_size = sizeof( DISTRO_VERSION ) - 1;
152 i_read = fread( p_distrostring, 1, i_size, file );
153 if( i_read != i_size ||
154 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
156 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
162 /* Check Sub-version number */
163 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
164 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
166 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
167 "(corrupted header)" );
172 /* Check header marker */
173 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
174 if( i_read != sizeof(i_marker) ||
175 i_marker != ftell( file ) - (int)sizeof(i_marker) )
177 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
178 "(corrupted header)" );
183 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
185 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
186 "(file too short)" );
191 module_cache_t **pp_cache = malloc( i_cache * sizeof(*pp_cache) );
192 if( pp_cache == NULL )
193 i_cache = 0; /* don't load anything */
195 #define LOAD_IMMEDIATE(a) \
196 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
197 #define LOAD_STRING(a) \
200 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
201 || ( i_size > 16384 ) ) \
204 char *psz = xmalloc( i_size ); \
205 if( fread( psz, i_size, 1, file ) != 1 ) { \
209 if( psz[i_size-1] ) { \
217 for( size_t i = 0; i < i_cache; i++ )
222 pp_cache[i] = xmalloc( sizeof(module_cache_t) );
224 /* Load common info */
225 LOAD_STRING( pp_cache[i]->path );
226 LOAD_IMMEDIATE( pp_cache[i]->mtime );
227 LOAD_IMMEDIATE( pp_cache[i]->size );
229 pp_cache[i]->p_module = vlc_module_create();
231 /* Load additional infos */
232 free( pp_cache[i]->p_module->psz_object_name );
233 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
234 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
235 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
236 LOAD_STRING( pp_cache[i]->p_module->psz_help );
238 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
239 if( pp_cache[i]->p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
241 else if( pp_cache[i]->p_module->i_shortcuts == 0 )
242 pp_cache[i]->p_module->pp_shortcuts = NULL;
245 pp_cache[i]->p_module->pp_shortcuts =
246 xmalloc( sizeof( char ** ) * pp_cache[i]->p_module->i_shortcuts );
247 for( unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++ )
248 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
251 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
252 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
253 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
256 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
259 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
260 LOAD_STRING( pp_cache[i]->p_module->domain );
261 if( pp_cache[i]->p_module->domain != NULL )
262 vlc_bindtextdomain( pp_cache[i]->p_module->domain );
264 LOAD_IMMEDIATE( i_submodules );
266 while( i_submodules-- )
268 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
269 free( p_module->psz_object_name );
270 free( p_module->pp_shortcuts );
271 LOAD_STRING( p_module->psz_object_name );
272 LOAD_STRING( p_module->psz_shortname );
273 LOAD_STRING( p_module->psz_longname );
274 LOAD_STRING( p_module->psz_help );
276 LOAD_IMMEDIATE( p_module->i_shortcuts );
277 if( p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
279 else if( p_module->i_shortcuts == 0 )
280 p_module->pp_shortcuts = NULL;
283 p_module->pp_shortcuts = xmalloc( sizeof( char ** ) * p_module->i_shortcuts );
284 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
285 LOAD_STRING( p_module->pp_shortcuts[j] );
288 LOAD_STRING( p_module->psz_capability );
289 LOAD_IMMEDIATE( p_module->i_score );
290 LOAD_IMMEDIATE( p_module->b_unloadable );
291 LOAD_STRING( p_module->domain );
301 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
309 static int CacheLoadConfig( module_t *p_module, FILE *file )
314 /* Calculate the structure length */
315 LOAD_IMMEDIATE( p_module->i_config_items );
316 LOAD_IMMEDIATE( p_module->i_bool_items );
318 LOAD_IMMEDIATE( i_lines );
320 /* Allocate memory */
324 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
325 if( p_module->p_config == NULL )
327 p_module->confsize = 0;
331 p_module->confsize = i_lines;
333 /* Do the duplication job */
334 for (size_t i = 0; i < i_lines; i++ )
336 LOAD_IMMEDIATE( p_module->p_config[i] );
338 LOAD_STRING( p_module->p_config[i].psz_type );
339 LOAD_STRING( p_module->p_config[i].psz_name );
340 LOAD_STRING( p_module->p_config[i].psz_text );
341 LOAD_STRING( p_module->p_config[i].psz_longtext );
342 LOAD_STRING( p_module->p_config[i].psz_oldname );
344 if (IsConfigStringType (p_module->p_config[i].i_type))
346 LOAD_STRING (p_module->p_config[i].orig.psz);
347 p_module->p_config[i].value.psz =
348 (p_module->p_config[i].orig.psz != NULL)
349 ? strdup (p_module->p_config[i].orig.psz) : NULL;
352 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
353 sizeof (p_module->p_config[i].value));
355 p_module->p_config[i].b_dirty = false;
357 if( p_module->p_config[i].i_list )
359 if( p_module->p_config[i].ppsz_list )
362 p_module->p_config[i].ppsz_list =
363 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
364 if( p_module->p_config[i].ppsz_list )
366 for( j = 0; j < p_module->p_config[i].i_list; j++ )
367 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
368 p_module->p_config[i].ppsz_list[j] = NULL;
371 if( p_module->p_config[i].ppsz_list_text )
374 p_module->p_config[i].ppsz_list_text =
375 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
376 if( p_module->p_config[i].ppsz_list_text )
378 for( j = 0; j < p_module->p_config[i].i_list; j++ )
379 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
380 p_module->p_config[i].ppsz_list_text[j] = NULL;
383 if( p_module->p_config[i].pi_list )
385 p_module->p_config[i].pi_list =
386 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
387 if( p_module->p_config[i].pi_list )
389 for (int j = 0; j < p_module->p_config[i].i_list; j++)
390 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
395 if( p_module->p_config[i].i_action )
397 p_module->p_config[i].ppf_action =
398 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
399 p_module->p_config[i].ppsz_action_text =
400 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
402 for (int j = 0; j < p_module->p_config[i].i_action; j++)
404 p_module->p_config[i].ppf_action[j] = NULL;
405 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
417 static int CacheSaveBank( FILE *file, module_cache_t *const *, size_t );
419 /*****************************************************************************
420 * SavePluginsCache: saves the plugins cache to a file
421 *****************************************************************************/
422 void CacheSave (vlc_object_t *p_this, const char *dir,
423 module_cache_t *const *pp_cache, size_t n)
425 char *filename, *tmpname;
427 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
430 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
435 msg_Dbg (p_this, "saving plugins cache %s", filename);
437 FILE *file = vlc_fopen (tmpname, "wb");
440 if (errno != EACCES && errno != ENOENT)
441 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
445 if (CacheSaveBank (file, pp_cache, n))
447 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
450 vlc_unlink (tmpname);
454 #if !defined( WIN32 ) && !defined( __OS2__ )
455 vlc_rename (tmpname, filename); /* atomically replace old cache */
458 vlc_unlink (filename);
460 vlc_rename (tmpname, filename);
467 static int CacheSaveConfig (FILE *, const module_t *);
468 static int CacheSaveSubmodule (FILE *, const module_t *);
470 static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
473 uint32_t i_file_size = 0;
475 /* Empty space for file size */
476 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
479 /* Contains version number */
480 if (fputs (CACHE_STRING, file) == EOF)
482 #ifdef DISTRO_VERSION
483 /* Allow binary maintaner to pass a string to detect new binary version*/
484 if (fputs( DISTRO_VERSION, file ) == EOF)
487 /* Sub-version number (to avoid breakage in the dev version when cache
488 * structure changes) */
489 i_file_size = CACHE_SUBVERSION_NUM;
490 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
494 i_file_size = ftell( file );
495 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
498 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
501 #define SAVE_IMMEDIATE( a ) \
502 if (fwrite (&a, sizeof(a), 1, file) != 1) \
504 #define SAVE_STRING( a ) \
506 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
507 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
508 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
512 for (unsigned i = 0; i < i_cache; i++)
514 uint32_t i_submodule;
516 /* Save common info */
517 SAVE_STRING( pp_cache[i]->path );
518 SAVE_IMMEDIATE( pp_cache[i]->mtime );
519 SAVE_IMMEDIATE( pp_cache[i]->size );
521 /* Save additional infos */
522 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
523 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
524 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
525 SAVE_STRING( pp_cache[i]->p_module->psz_help );
526 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
527 for (unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++)
528 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
530 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
531 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
532 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
535 if (CacheSaveConfig (file, pp_cache[i]->p_module))
538 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
539 SAVE_STRING( pp_cache[i]->p_module->domain );
541 i_submodule = pp_cache[i]->p_module->submodule_count;
542 SAVE_IMMEDIATE( i_submodule );
543 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
547 /* Fill-up file size */
548 i_file_size = ftell( file );
549 fseek( file, 0, SEEK_SET );
550 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
551 || fflush (file)) /* flush libc buffers */
553 return 0; /* success! */
559 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
563 if( CacheSaveSubmodule( file, p_module->next ) )
566 SAVE_STRING( p_module->psz_object_name );
567 SAVE_STRING( p_module->psz_shortname );
568 SAVE_STRING( p_module->psz_longname );
569 SAVE_STRING( p_module->psz_help );
570 SAVE_IMMEDIATE( p_module->i_shortcuts );
571 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
572 SAVE_STRING( p_module->pp_shortcuts[j] );
574 SAVE_STRING( p_module->psz_capability );
575 SAVE_IMMEDIATE( p_module->i_score );
576 SAVE_IMMEDIATE( p_module->b_unloadable );
577 SAVE_STRING( p_module->domain );
585 static int CacheSaveConfig (FILE *file, const module_t *p_module)
587 uint32_t i_lines = p_module->confsize;
589 SAVE_IMMEDIATE( p_module->i_config_items );
590 SAVE_IMMEDIATE( p_module->i_bool_items );
591 SAVE_IMMEDIATE( i_lines );
593 for (size_t i = 0; i < i_lines ; i++)
595 SAVE_IMMEDIATE( p_module->p_config[i] );
597 SAVE_STRING( p_module->p_config[i].psz_type );
598 SAVE_STRING( p_module->p_config[i].psz_name );
599 SAVE_STRING( p_module->p_config[i].psz_text );
600 SAVE_STRING( p_module->p_config[i].psz_longtext );
601 SAVE_STRING( p_module->p_config[i].psz_oldname );
603 if (IsConfigStringType (p_module->p_config[i].i_type))
604 SAVE_STRING( p_module->p_config[i].orig.psz );
606 if( p_module->p_config[i].i_list )
608 if( p_module->p_config[i].ppsz_list )
610 for (int j = 0; j < p_module->p_config[i].i_list; j++)
611 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
614 if( p_module->p_config[i].ppsz_list_text )
616 for (int j = 0; j < p_module->p_config[i].i_list; j++)
617 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
619 if( p_module->p_config[i].pi_list )
621 for (int j = 0; j < p_module->p_config[i].i_list; j++)
622 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
626 for (int j = 0; j < p_module->p_config[i].i_action; j++)
627 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
635 /*****************************************************************************
636 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
637 *****************************************************************************/
638 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
642 p_cache->pf_activate = p_module->pf_activate;
643 p_cache->pf_deactivate = p_module->pf_deactivate;
644 p_cache->handle = p_module->handle;
646 /* FIXME: This looks too simplistic an algorithm to me. What if the module
647 * file was altered such that the number of order of submodules was
648 * altered... after VLC started -- Courmisch, 09/2008 */
649 module_t *p_child = p_module->submodule,
650 *p_cchild = p_cache->submodule;
651 while( p_child && p_cchild )
653 p_cchild->pf_activate = p_child->pf_activate;
654 p_cchild->pf_deactivate = p_child->pf_deactivate;
655 p_child = p_child->next;
656 p_cchild = p_cchild->next;
659 p_cache->b_loaded = true;
660 p_module->b_loaded = false;
663 /*****************************************************************************
664 * CacheFind: finds the cache entry corresponding to a file
665 *****************************************************************************/
666 module_t *CacheFind( module_bank_t *p_bank,
667 const char *path, time_t mtime, off_t size )
669 module_cache_t **cache = p_bank->pp_loaded_cache;
670 size_t n = p_bank->i_loaded_cache;
672 for( size_t i = 0; i < n; i++ )
673 if( !strcmp( cache[i]->path, path )
674 && cache[i]->mtime == mtime
675 && cache[i]->size == size )
677 module_t *module = cache[i]->p_module;
678 cache[i]->p_module = NULL;
685 #endif /* HAVE_DYNAMIC_PLUGINS */