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 12
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 );
82 /*****************************************************************************
83 * LoadPluginsCache: loads the plugins cache file
84 *****************************************************************************
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.
89 *****************************************************************************/
90 void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
95 char p_cachestring[sizeof(CACHE_STRING)];
97 module_cache_t **pp_cache = NULL;
98 int32_t i_file_size, i_marker;
100 assert( dir != NULL );
102 if( !p_bank->b_cache )
105 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
108 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
110 file = vlc_fopen( psz_filename, "rb" );
113 msg_Warn( p_this, "cannot read %s (%m)",
115 free( psz_filename );
118 free( psz_filename );
120 /* Check the file size */
121 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
122 if( i_read != sizeof(i_file_size) )
124 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
130 fseek( file, 0, SEEK_END );
131 if( ftell( file ) != i_file_size )
133 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
134 "(corrupted size)" );
138 fseek( file, sizeof(i_file_size), SEEK_SET );
140 /* Check the file is a plugins cache */
141 i_size = sizeof(CACHE_STRING) - 1;
142 i_read = fread( p_cachestring, 1, i_size, file );
143 if( i_read != i_size ||
144 memcmp( p_cachestring, CACHE_STRING, i_size ) )
146 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
151 #ifdef DISTRO_VERSION
152 /* Check for distribution specific version */
153 char p_distrostring[sizeof( DISTRO_VERSION )];
154 i_size = sizeof( DISTRO_VERSION ) - 1;
155 i_read = fread( p_distrostring, 1, i_size, file );
156 if( i_read != i_size ||
157 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
159 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
165 /* Check Sub-version number */
166 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
167 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
169 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
170 "(corrupted header)" );
175 /* Check header marker */
176 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
177 if( i_read != sizeof(i_marker) ||
178 i_marker != ftell( file ) - (int)sizeof(i_marker) )
180 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
181 "(corrupted header)" );
186 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
188 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
189 "(file too short)" );
196 size_t i_already = p_bank->i_loaded_cache;
197 pp_cache = realloc( p_bank->pp_loaded_cache,
198 (i_already + i_cache) * sizeof(*pp_cache) );
199 if( unlikely(pp_cache == NULL) )
200 i_cache = 0; /* don't load */
203 p_bank->pp_loaded_cache = pp_cache;
204 pp_cache += i_already;
208 #define LOAD_IMMEDIATE(a) \
209 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
210 #define LOAD_STRING(a) \
213 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
214 || ( i_size > 16384 ) ) \
217 char *psz = xmalloc( i_size ); \
218 if( fread( psz, i_size, 1, file ) != 1 ) { \
222 if( psz[i_size-1] ) { \
230 for( size_t i = 0; i < i_cache; i++ )
235 pp_cache[i] = xmalloc( sizeof(module_cache_t) );
237 /* Load common info */
238 LOAD_STRING( pp_cache[i]->psz_file );
239 LOAD_IMMEDIATE( pp_cache[i]->i_time );
240 LOAD_IMMEDIATE( pp_cache[i]->i_size );
242 pp_cache[i]->p_module = vlc_module_create( p_this );
244 /* Load additional infos */
245 free( pp_cache[i]->p_module->psz_object_name );
246 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
247 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
248 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
249 LOAD_STRING( pp_cache[i]->p_module->psz_help );
251 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
252 if( pp_cache[i]->p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
254 else if( pp_cache[i]->p_module->i_shortcuts == 0 )
255 pp_cache[i]->p_module->pp_shortcuts = NULL;
258 pp_cache[i]->p_module->pp_shortcuts =
259 xmalloc( sizeof( char ** ) * pp_cache[i]->p_module->i_shortcuts );
260 for( unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++ )
261 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
264 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
265 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
266 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
267 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
270 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
273 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
274 LOAD_STRING( pp_cache[i]->p_module->domain );
275 if( pp_cache[i]->p_module->domain != NULL )
276 vlc_bindtextdomain( pp_cache[i]->p_module->domain );
278 LOAD_IMMEDIATE( i_submodules );
280 while( i_submodules-- )
282 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
283 free( p_module->psz_object_name );
284 free( p_module->pp_shortcuts );
285 LOAD_STRING( p_module->psz_object_name );
286 LOAD_STRING( p_module->psz_shortname );
287 LOAD_STRING( p_module->psz_longname );
288 LOAD_STRING( p_module->psz_help );
290 LOAD_IMMEDIATE( p_module->i_shortcuts );
291 if( p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
293 else if( p_module->i_shortcuts == 0 )
294 p_module->pp_shortcuts = NULL;
297 p_module->pp_shortcuts = xmalloc( sizeof( char ** ) * p_module->i_shortcuts );
298 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
299 LOAD_STRING( p_module->pp_shortcuts[j] );
302 LOAD_STRING( p_module->psz_capability );
303 LOAD_IMMEDIATE( p_module->i_score );
304 LOAD_IMMEDIATE( p_module->b_unloadable );
305 LOAD_STRING( p_module->domain );
308 p_bank->i_loaded_cache += i_cache;
315 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
323 static int CacheLoadConfig( module_t *p_module, FILE *file )
328 /* Calculate the structure length */
329 LOAD_IMMEDIATE( p_module->i_config_items );
330 LOAD_IMMEDIATE( p_module->i_bool_items );
332 LOAD_IMMEDIATE( i_lines );
334 /* Allocate memory */
338 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
339 if( p_module->p_config == NULL )
341 p_module->confsize = 0;
345 p_module->confsize = i_lines;
347 /* Do the duplication job */
348 for (size_t i = 0; i < i_lines; i++ )
350 LOAD_IMMEDIATE( p_module->p_config[i] );
352 LOAD_STRING( p_module->p_config[i].psz_type );
353 LOAD_STRING( p_module->p_config[i].psz_name );
354 LOAD_STRING( p_module->p_config[i].psz_text );
355 LOAD_STRING( p_module->p_config[i].psz_longtext );
356 LOAD_STRING( p_module->p_config[i].psz_oldname );
357 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
359 if (IsConfigStringType (p_module->p_config[i].i_type))
361 LOAD_STRING (p_module->p_config[i].orig.psz);
362 p_module->p_config[i].value.psz =
363 (p_module->p_config[i].orig.psz != NULL)
364 ? strdup (p_module->p_config[i].orig.psz) : NULL;
367 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
368 sizeof (p_module->p_config[i].value));
370 p_module->p_config[i].b_dirty = false;
372 if( p_module->p_config[i].i_list )
374 if( p_module->p_config[i].ppsz_list )
377 p_module->p_config[i].ppsz_list =
378 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
379 if( p_module->p_config[i].ppsz_list )
381 for( j = 0; j < p_module->p_config[i].i_list; j++ )
382 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
383 p_module->p_config[i].ppsz_list[j] = NULL;
386 if( p_module->p_config[i].ppsz_list_text )
389 p_module->p_config[i].ppsz_list_text =
390 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
391 if( p_module->p_config[i].ppsz_list_text )
393 for( j = 0; j < p_module->p_config[i].i_list; j++ )
394 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
395 p_module->p_config[i].ppsz_list_text[j] = NULL;
398 if( p_module->p_config[i].pi_list )
400 p_module->p_config[i].pi_list =
401 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
402 if( p_module->p_config[i].pi_list )
404 for (int j = 0; j < p_module->p_config[i].i_list; j++)
405 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
410 if( p_module->p_config[i].i_action )
412 p_module->p_config[i].ppf_action =
413 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
414 p_module->p_config[i].ppsz_action_text =
415 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
417 for (int j = 0; j < p_module->p_config[i].i_action; j++)
419 p_module->p_config[i].ppf_action[j] = NULL;
420 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
432 static int CacheSaveBank( FILE *file, module_cache_t *const *, size_t );
434 /*****************************************************************************
435 * SavePluginsCache: saves the plugins cache to a file
436 *****************************************************************************/
437 void CacheSave (vlc_object_t *p_this, const char *dir,
438 module_cache_t *const *pp_cache, size_t n)
440 char *filename, *tmpname;
442 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
445 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
450 msg_Dbg (p_this, "saving plugins cache %s", filename);
452 FILE *file = vlc_fopen (tmpname, "wb");
455 if (errno != EACCES && errno != ENOENT)
456 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
460 if (CacheSaveBank (file, pp_cache, n))
462 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
465 vlc_unlink (tmpname);
469 #if !defined( WIN32 ) && !defined( __OS2__ )
470 vlc_rename (tmpname, filename); /* atomically replace old cache */
473 vlc_unlink (filename);
475 vlc_rename (tmpname, filename);
482 static int CacheSaveConfig (FILE *, const module_t *);
483 static int CacheSaveSubmodule (FILE *, const module_t *);
485 static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
488 uint32_t i_file_size = 0;
490 /* Empty space for file size */
491 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
494 /* Contains version number */
495 if (fputs (CACHE_STRING, file) == EOF)
497 #ifdef DISTRO_VERSION
498 /* Allow binary maintaner to pass a string to detect new binary version*/
499 if (fputs( DISTRO_VERSION, file ) == EOF)
502 /* Sub-version number (to avoid breakage in the dev version when cache
503 * structure changes) */
504 i_file_size = CACHE_SUBVERSION_NUM;
505 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
509 i_file_size = ftell( file );
510 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
513 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
516 #define SAVE_IMMEDIATE( a ) \
517 if (fwrite (&a, sizeof(a), 1, file) != 1) \
519 #define SAVE_STRING( a ) \
521 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
522 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
523 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
527 for (unsigned i = 0; i < i_cache; i++)
529 uint32_t i_submodule;
531 /* Save common info */
532 SAVE_STRING( pp_cache[i]->psz_file );
533 SAVE_IMMEDIATE( pp_cache[i]->i_time );
534 SAVE_IMMEDIATE( pp_cache[i]->i_size );
536 /* Save additional infos */
537 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
538 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
539 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
540 SAVE_STRING( pp_cache[i]->p_module->psz_help );
541 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
542 for (unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++)
543 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
545 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
546 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
547 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
548 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
551 if (CacheSaveConfig (file, pp_cache[i]->p_module))
554 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
555 SAVE_STRING( pp_cache[i]->p_module->domain );
557 i_submodule = pp_cache[i]->p_module->submodule_count;
558 SAVE_IMMEDIATE( i_submodule );
559 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
563 /* Fill-up file size */
564 i_file_size = ftell( file );
565 fseek( file, 0, SEEK_SET );
566 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
567 || fflush (file)) /* flush libc buffers */
569 return 0; /* success! */
575 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
579 if( CacheSaveSubmodule( file, p_module->next ) )
582 SAVE_STRING( p_module->psz_object_name );
583 SAVE_STRING( p_module->psz_shortname );
584 SAVE_STRING( p_module->psz_longname );
585 SAVE_STRING( p_module->psz_help );
586 SAVE_IMMEDIATE( p_module->i_shortcuts );
587 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
588 SAVE_STRING( p_module->pp_shortcuts[j] );
590 SAVE_STRING( p_module->psz_capability );
591 SAVE_IMMEDIATE( p_module->i_score );
592 SAVE_IMMEDIATE( p_module->b_unloadable );
593 SAVE_STRING( p_module->domain );
601 static int CacheSaveConfig (FILE *file, const module_t *p_module)
603 uint32_t i_lines = p_module->confsize;
605 SAVE_IMMEDIATE( p_module->i_config_items );
606 SAVE_IMMEDIATE( p_module->i_bool_items );
607 SAVE_IMMEDIATE( i_lines );
609 for (size_t i = 0; i < i_lines ; i++)
611 SAVE_IMMEDIATE( p_module->p_config[i] );
613 SAVE_STRING( p_module->p_config[i].psz_type );
614 SAVE_STRING( p_module->p_config[i].psz_name );
615 SAVE_STRING( p_module->p_config[i].psz_text );
616 SAVE_STRING( p_module->p_config[i].psz_longtext );
617 SAVE_STRING( p_module->p_config[i].psz_oldname );
618 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
620 if (IsConfigStringType (p_module->p_config[i].i_type))
621 SAVE_STRING( p_module->p_config[i].orig.psz );
623 if( p_module->p_config[i].i_list )
625 if( p_module->p_config[i].ppsz_list )
627 for (int j = 0; j < p_module->p_config[i].i_list; j++)
628 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
631 if( p_module->p_config[i].ppsz_list_text )
633 for (int j = 0; j < p_module->p_config[i].i_list; j++)
634 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
636 if( p_module->p_config[i].pi_list )
638 for (int j = 0; j < p_module->p_config[i].i_list; j++)
639 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
643 for (int j = 0; j < p_module->p_config[i].i_action; j++)
644 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
652 /*****************************************************************************
653 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
654 *****************************************************************************/
655 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
659 p_cache->pf_activate = p_module->pf_activate;
660 p_cache->pf_deactivate = p_module->pf_deactivate;
661 p_cache->handle = p_module->handle;
663 /* FIXME: This looks too simplistic an algorithm to me. What if the module
664 * file was altered such that the number of order of submodules was
665 * altered... after VLC started -- Courmisch, 09/2008 */
666 module_t *p_child = p_module->submodule,
667 *p_cchild = p_cache->submodule;
668 while( p_child && p_cchild )
670 p_cchild->pf_activate = p_child->pf_activate;
671 p_cchild->pf_deactivate = p_child->pf_deactivate;
672 p_child = p_child->next;
673 p_cchild = p_cchild->next;
676 p_cache->b_loaded = true;
677 p_module->b_loaded = false;
680 /*****************************************************************************
681 * CacheFind: finds the cache entry corresponding to a file
682 *****************************************************************************/
683 module_cache_t *CacheFind( module_bank_t *p_bank, const char *psz_file,
684 int64_t i_time, int64_t i_size )
686 module_cache_t **pp_cache;
689 pp_cache = p_bank->pp_loaded_cache;
690 i_cache = p_bank->i_loaded_cache;
692 for( i = 0; i < i_cache; i++ )
694 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
695 pp_cache[i]->i_time == i_time &&
696 pp_cache[i]->i_size == i_size ) return pp_cache[i];
702 #endif /* HAVE_DYNAMIC_PLUGINS */