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>
47 #include "config/configuration.h"
51 #include "modules/modules.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 #ifdef HAVE_DYNAMIC_PLUGINS
58 static int CacheLoadConfig ( module_t *, FILE * );
61 * (only used to avoid breakage in dev version when cache structure changes) */
62 #define CACHE_SUBVERSION_NUM 15
65 #define CACHE_NAME "plugins.dat"
66 /* Magic for the cache filename */
67 #define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
70 void CacheDelete( vlc_object_t *obj, const char *dir )
74 assert( dir != NULL );
76 if( asprintf( &path, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
78 msg_Dbg( obj, "removing plugins cache file %s", path );
84 * Loads a plugins cache file.
86 * This function will load the plugin cache if present and valid. This cache
87 * will in turn be queried by AllocateAllPlugins() to see if it needs to
88 * actually load the dynamically loadable module.
89 * This allows us to only fully load plugins when they are actually used.
91 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r )
96 char p_cachestring[sizeof(CACHE_STRING)];
100 assert( dir != NULL );
103 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
106 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
108 file = vlc_fopen( psz_filename, "rb" );
111 msg_Warn( p_this, "cannot read %s (%m)",
113 free( psz_filename );
116 free( psz_filename );
118 /* Check the file is a plugins cache */
119 i_size = sizeof(CACHE_STRING) - 1;
120 i_read = fread( p_cachestring, 1, i_size, file );
121 if( i_read != i_size ||
122 memcmp( p_cachestring, CACHE_STRING, i_size ) )
124 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
129 #ifdef DISTRO_VERSION
130 /* Check for distribution specific version */
131 char p_distrostring[sizeof( DISTRO_VERSION )];
132 i_size = sizeof( DISTRO_VERSION ) - 1;
133 i_read = fread( p_distrostring, 1, i_size, file );
134 if( i_read != i_size ||
135 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
137 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
143 /* Check Sub-version number */
144 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
145 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
147 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
148 "(corrupted header)" );
153 /* Check header marker */
154 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
155 if( i_read != sizeof(i_marker) ||
156 i_marker != ftell( file ) - (int)sizeof(i_marker) )
158 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
159 "(corrupted header)" );
164 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
166 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
167 "(file too short)" );
172 module_cache_t **pp_cache = malloc( i_cache * sizeof(*pp_cache) );
173 if( pp_cache == NULL )
174 i_cache = 0; /* don't load anything */
176 #define LOAD_IMMEDIATE(a) \
177 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
178 #define LOAD_STRING(a) \
181 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
182 || ( i_size > 16384 ) ) \
185 char *psz = xmalloc( i_size ); \
186 if( fread( psz, i_size, 1, file ) != 1 ) { \
190 if( psz[i_size-1] ) { \
198 for( size_t i = 0; i < i_cache; i++ )
203 pp_cache[i] = xmalloc( sizeof(module_cache_t) );
205 /* Load common info */
206 LOAD_STRING( pp_cache[i]->path );
207 LOAD_IMMEDIATE( pp_cache[i]->mtime );
208 LOAD_IMMEDIATE( pp_cache[i]->size );
210 pp_cache[i]->p_module = vlc_module_create();
212 /* Load additional infos */
213 free( pp_cache[i]->p_module->psz_object_name );
214 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
215 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
216 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
217 LOAD_STRING( pp_cache[i]->p_module->psz_help );
219 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
220 if( pp_cache[i]->p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
222 else if( pp_cache[i]->p_module->i_shortcuts == 0 )
223 pp_cache[i]->p_module->pp_shortcuts = NULL;
226 pp_cache[i]->p_module->pp_shortcuts =
227 xmalloc( sizeof( char ** ) * pp_cache[i]->p_module->i_shortcuts );
228 for( unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++ )
229 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
232 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
233 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
234 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
237 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
240 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
241 LOAD_STRING( pp_cache[i]->p_module->domain );
242 if( pp_cache[i]->p_module->domain != NULL )
243 vlc_bindtextdomain( pp_cache[i]->p_module->domain );
245 LOAD_IMMEDIATE( i_submodules );
247 while( i_submodules-- )
249 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
250 free( p_module->psz_object_name );
251 free( p_module->pp_shortcuts );
252 LOAD_STRING( p_module->psz_object_name );
253 LOAD_STRING( p_module->psz_shortname );
254 LOAD_STRING( p_module->psz_longname );
255 LOAD_STRING( p_module->psz_help );
257 LOAD_IMMEDIATE( p_module->i_shortcuts );
258 if( p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
260 else if( p_module->i_shortcuts == 0 )
261 p_module->pp_shortcuts = NULL;
264 p_module->pp_shortcuts = xmalloc( sizeof( char ** ) * p_module->i_shortcuts );
265 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
266 LOAD_STRING( p_module->pp_shortcuts[j] );
269 LOAD_STRING( p_module->psz_capability );
270 LOAD_IMMEDIATE( p_module->i_score );
271 LOAD_IMMEDIATE( p_module->b_unloadable );
272 LOAD_STRING( p_module->domain );
282 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
290 static int CacheLoadConfig( module_t *p_module, FILE *file )
295 /* Calculate the structure length */
296 LOAD_IMMEDIATE( p_module->i_config_items );
297 LOAD_IMMEDIATE( p_module->i_bool_items );
299 LOAD_IMMEDIATE( i_lines );
301 /* Allocate memory */
305 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
306 if( p_module->p_config == NULL )
308 p_module->confsize = 0;
312 p_module->confsize = i_lines;
314 /* Do the duplication job */
315 for (size_t i = 0; i < i_lines; i++ )
317 LOAD_IMMEDIATE( p_module->p_config[i] );
319 LOAD_STRING( p_module->p_config[i].psz_type );
320 LOAD_STRING( p_module->p_config[i].psz_name );
321 LOAD_STRING( p_module->p_config[i].psz_text );
322 LOAD_STRING( p_module->p_config[i].psz_longtext );
323 LOAD_STRING( p_module->p_config[i].psz_oldname );
325 if (IsConfigStringType (p_module->p_config[i].i_type))
327 LOAD_STRING (p_module->p_config[i].orig.psz);
328 p_module->p_config[i].value.psz =
329 (p_module->p_config[i].orig.psz != NULL)
330 ? strdup (p_module->p_config[i].orig.psz) : NULL;
333 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
334 sizeof (p_module->p_config[i].value));
336 p_module->p_config[i].b_dirty = false;
338 if( p_module->p_config[i].i_list )
340 if( p_module->p_config[i].ppsz_list )
343 p_module->p_config[i].ppsz_list =
344 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
345 if( p_module->p_config[i].ppsz_list )
347 for( j = 0; j < p_module->p_config[i].i_list; j++ )
348 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
349 p_module->p_config[i].ppsz_list[j] = NULL;
352 if( p_module->p_config[i].ppsz_list_text )
355 p_module->p_config[i].ppsz_list_text =
356 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
357 if( p_module->p_config[i].ppsz_list_text )
359 for( j = 0; j < p_module->p_config[i].i_list; j++ )
360 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
361 p_module->p_config[i].ppsz_list_text[j] = NULL;
364 if( p_module->p_config[i].pi_list )
366 p_module->p_config[i].pi_list =
367 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
368 if( p_module->p_config[i].pi_list )
370 for (int j = 0; j < p_module->p_config[i].i_list; j++)
371 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
376 if( p_module->p_config[i].i_action )
378 p_module->p_config[i].ppf_action =
379 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
380 p_module->p_config[i].ppsz_action_text =
381 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
383 for (int j = 0; j < p_module->p_config[i].i_action; j++)
385 p_module->p_config[i].ppf_action[j] = NULL;
386 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
398 static int CacheSaveBank( FILE *file, module_cache_t *const *, size_t );
400 /*****************************************************************************
401 * SavePluginsCache: saves the plugins cache to a file
402 *****************************************************************************/
403 void CacheSave (vlc_object_t *p_this, const char *dir,
404 module_cache_t *const *pp_cache, size_t n)
406 char *filename, *tmpname;
408 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
411 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
416 msg_Dbg (p_this, "saving plugins cache %s", filename);
418 FILE *file = vlc_fopen (tmpname, "wb");
421 if (errno != EACCES && errno != ENOENT)
422 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
426 if (CacheSaveBank (file, pp_cache, n))
428 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
431 vlc_unlink (tmpname);
435 #if !defined( WIN32 ) && !defined( __OS2__ )
436 vlc_rename (tmpname, filename); /* atomically replace old cache */
439 vlc_unlink (filename);
441 vlc_rename (tmpname, filename);
448 static int CacheSaveConfig (FILE *, const module_t *);
449 static int CacheSaveSubmodule (FILE *, const module_t *);
451 static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
454 uint32_t i_file_size = 0;
456 /* Contains version number */
457 if (fputs (CACHE_STRING, file) == EOF)
459 #ifdef DISTRO_VERSION
460 /* Allow binary maintaner to pass a string to detect new binary version*/
461 if (fputs( DISTRO_VERSION, file ) == EOF)
464 /* Sub-version number (to avoid breakage in the dev version when cache
465 * structure changes) */
466 i_file_size = CACHE_SUBVERSION_NUM;
467 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
471 i_file_size = ftell( file );
472 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
475 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
478 #define SAVE_IMMEDIATE( a ) \
479 if (fwrite (&a, sizeof(a), 1, file) != 1) \
481 #define SAVE_STRING( a ) \
483 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
484 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
485 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
489 for (unsigned i = 0; i < i_cache; i++)
491 uint32_t i_submodule;
493 /* Save common info */
494 SAVE_STRING( pp_cache[i]->path );
495 SAVE_IMMEDIATE( pp_cache[i]->mtime );
496 SAVE_IMMEDIATE( pp_cache[i]->size );
498 /* Save additional infos */
499 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
500 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
501 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
502 SAVE_STRING( pp_cache[i]->p_module->psz_help );
503 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
504 for (unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++)
505 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
507 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
508 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
509 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
512 if (CacheSaveConfig (file, pp_cache[i]->p_module))
515 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
516 SAVE_STRING( pp_cache[i]->p_module->domain );
518 i_submodule = pp_cache[i]->p_module->submodule_count;
519 SAVE_IMMEDIATE( i_submodule );
520 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
524 if (fflush (file)) /* flush libc buffers */
526 return 0; /* success! */
532 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
536 if( CacheSaveSubmodule( file, p_module->next ) )
539 SAVE_STRING( p_module->psz_object_name );
540 SAVE_STRING( p_module->psz_shortname );
541 SAVE_STRING( p_module->psz_longname );
542 SAVE_STRING( p_module->psz_help );
543 SAVE_IMMEDIATE( p_module->i_shortcuts );
544 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
545 SAVE_STRING( p_module->pp_shortcuts[j] );
547 SAVE_STRING( p_module->psz_capability );
548 SAVE_IMMEDIATE( p_module->i_score );
549 SAVE_IMMEDIATE( p_module->b_unloadable );
550 SAVE_STRING( p_module->domain );
558 static int CacheSaveConfig (FILE *file, const module_t *p_module)
560 uint32_t i_lines = p_module->confsize;
562 SAVE_IMMEDIATE( p_module->i_config_items );
563 SAVE_IMMEDIATE( p_module->i_bool_items );
564 SAVE_IMMEDIATE( i_lines );
566 for (size_t i = 0; i < i_lines ; i++)
568 SAVE_IMMEDIATE( p_module->p_config[i] );
570 SAVE_STRING( p_module->p_config[i].psz_type );
571 SAVE_STRING( p_module->p_config[i].psz_name );
572 SAVE_STRING( p_module->p_config[i].psz_text );
573 SAVE_STRING( p_module->p_config[i].psz_longtext );
574 SAVE_STRING( p_module->p_config[i].psz_oldname );
576 if (IsConfigStringType (p_module->p_config[i].i_type))
577 SAVE_STRING( p_module->p_config[i].orig.psz );
579 if( p_module->p_config[i].i_list )
581 if( p_module->p_config[i].ppsz_list )
583 for (int j = 0; j < p_module->p_config[i].i_list; j++)
584 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
587 if( p_module->p_config[i].ppsz_list_text )
589 for (int j = 0; j < p_module->p_config[i].i_list; j++)
590 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
592 if( p_module->p_config[i].pi_list )
594 for (int j = 0; j < p_module->p_config[i].i_list; j++)
595 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
599 for (int j = 0; j < p_module->p_config[i].i_action; j++)
600 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
608 /*****************************************************************************
609 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
610 *****************************************************************************/
611 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
615 p_cache->pf_activate = p_module->pf_activate;
616 p_cache->pf_deactivate = p_module->pf_deactivate;
617 p_cache->handle = p_module->handle;
619 /* FIXME: This looks too simplistic an algorithm to me. What if the module
620 * file was altered such that the number of order of submodules was
621 * altered... after VLC started -- Courmisch, 09/2008 */
622 module_t *p_child = p_module->submodule,
623 *p_cchild = p_cache->submodule;
624 while( p_child && p_cchild )
626 p_cchild->pf_activate = p_child->pf_activate;
627 p_cchild->pf_deactivate = p_child->pf_deactivate;
628 p_child = p_child->next;
629 p_cchild = p_cchild->next;
632 p_cache->b_loaded = true;
633 p_module->b_loaded = false;
637 * Looks up a plugin file in a list of cached plugins.
639 module_t *CacheFind (module_bank_t *p_bank,
640 const char *path, const struct stat *st)
642 module_cache_t **cache = p_bank->pp_loaded_cache;
643 size_t n = p_bank->i_loaded_cache;
645 for( size_t i = 0; i < n; i++ )
646 if( !strcmp( cache[i]->path, path )
647 && cache[i]->mtime == st->st_mtime
648 && cache[i]->size == st->st_size)
650 module_t *module = cache[i]->p_module;
651 cache[i]->p_module = NULL;
658 #endif /* HAVE_DYNAMIC_PLUGINS */