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 *****************************************************************************/
34 #include <stdlib.h> /* free(), strtol() */
35 #include <stdio.h> /* sprintf() */
36 #include <string.h> /* strdup() */
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
45 #if !defined(HAVE_DYNAMIC_PLUGINS)
46 /* no support for plugins */
47 #elif defined(HAVE_DL_DYLD)
48 # if defined(HAVE_MACH_O_DYLD_H)
49 # include <mach-o/dyld.h>
51 #elif defined(HAVE_DL_BEOS)
52 # if defined(HAVE_IMAGE_H)
55 #elif defined(HAVE_DL_WINDOWS)
57 #elif defined(HAVE_DL_DLOPEN)
58 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
61 # if defined(HAVE_SYS_DL_H)
64 #elif defined(HAVE_DL_SHL_LOAD)
65 # if defined(HAVE_DL_H)
70 #include "config/configuration.h"
72 #include "vlc_charset.h"
74 #include "modules/modules.h"
77 /*****************************************************************************
79 *****************************************************************************/
80 #ifdef HAVE_DYNAMIC_PLUGINS
81 static int CacheLoadConfig ( module_t *, FILE * );
82 static int CacheSaveConfig ( module_t *, FILE * );
83 static char * CacheName ( void );
86 * (only used to avoid breakage in dev version when cache structure changes) */
87 #define CACHE_SUBVERSION_NUM 3
89 /*****************************************************************************
90 * LoadPluginsCache: loads the plugins cache file
91 *****************************************************************************
92 * This function will load the plugin cache if present and valid. This cache
93 * will in turn be queried by AllocateAllPlugins() to see if it needs to
94 * actually load the dynamically loadable module.
95 * This allows us to only fully load plugins when they are actually used.
96 *****************************************************************************/
97 void CacheLoad( vlc_object_t *p_this )
99 char *psz_filename, *psz_cachedir = config_GetCacheDir();
101 int i, j, i_size, i_read;
102 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
103 char p_cachelang[6], p_lang[6];
105 module_cache_t **pp_cache = 0;
106 int32_t i_file_size, i_marker;
107 libvlc_global_data_t *p_libvlc_global = vlc_global();
109 if( !psz_cachedir ) /* XXX: this should never happen */
111 msg_Err( p_this, "Unable to get cache directory" );
115 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s",
116 psz_cachedir, CacheName() );
117 free( psz_cachedir );
123 if( p_libvlc_global->p_module_bank->b_cache_delete )
125 #if !defined( UNDER_CE )
126 unlink( psz_filename );
128 wchar_t psz_wf[MAX_PATH];
129 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
130 DeleteFile( psz_wf );
132 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
133 free( psz_filename );
137 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
139 file = utf8_fopen( psz_filename, "rb" );
142 msg_Warn( p_this, "could not open plugins cache file %s for reading",
144 free( psz_filename );
147 free( psz_filename );
149 /* Check the file size */
150 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
151 if( i_read != sizeof(i_file_size) )
153 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
159 fseek( file, 0, SEEK_END );
160 if( ftell( file ) != i_file_size )
162 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
163 "(corrupted size)" );
167 fseek( file, sizeof(i_file_size), SEEK_SET );
169 /* Check the file is a plugins cache */
170 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
171 i_read = fread( p_cachestring, 1, i_size, file );
172 if( i_read != i_size ||
173 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
175 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
180 /* Check Sub-version number */
181 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
182 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
184 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
185 "(corrupted header)" );
190 /* Check the language hasn't changed */
191 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
192 i_read = fread( p_cachelang, 1, i_size, file );
193 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
195 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
196 "(language changed)" );
201 /* Check header marker */
202 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
203 if( i_read != sizeof(i_marker) ||
204 i_marker != ftell( file ) - (int)sizeof(i_marker) )
206 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
207 "(corrupted header)" );
212 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
213 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
215 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
216 "(file too short)" );
222 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
223 malloc( i_cache * sizeof(void *) );
225 #define LOAD_IMMEDIATE(a) \
226 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
227 #define LOAD_STRING(a) \
230 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
231 || ( i_size > 16384 ) ) \
234 char *psz = malloc( i_size ); \
235 if( fread( psz, i_size, 1, file ) != 1 ) { \
239 if( psz[i_size-1] ) { \
247 for( i = 0; i < i_cache; i++ )
252 pp_cache[i] = malloc( sizeof(module_cache_t) );
253 p_libvlc_global->p_module_bank->i_loaded_cache++;
255 /* Load common info */
256 LOAD_STRING( pp_cache[i]->psz_file );
257 LOAD_IMMEDIATE( pp_cache[i]->i_time );
258 LOAD_IMMEDIATE( pp_cache[i]->i_size );
259 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
260 pp_cache[i]->b_used = false;
262 if( pp_cache[i]->b_junk ) continue;
264 pp_cache[i]->p_module = vlc_module_create( p_this );
266 /* Load additional infos */
267 free( pp_cache[i]->p_module->psz_object_name );
268 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
269 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
270 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
271 LOAD_STRING( pp_cache[i]->p_module->psz_help );
272 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
274 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
276 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
277 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
278 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
279 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
280 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
281 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
284 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
287 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
289 LOAD_IMMEDIATE( i_submodules );
291 while( i_submodules-- )
293 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
294 free( p_module->psz_object_name );
295 LOAD_STRING( p_module->psz_object_name );
296 LOAD_STRING( p_module->psz_shortname );
297 LOAD_STRING( p_module->psz_longname );
298 LOAD_STRING( p_module->psz_help );
299 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
301 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
303 LOAD_STRING( p_module->psz_capability );
304 LOAD_IMMEDIATE( p_module->i_score );
305 LOAD_IMMEDIATE( p_module->i_cpu );
306 LOAD_IMMEDIATE( p_module->b_unloadable );
307 LOAD_IMMEDIATE( p_module->b_reentrant );
316 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
319 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
326 static int CacheLoadConfig( module_t *p_module, FILE *file )
331 /* Calculate the structure length */
332 LOAD_IMMEDIATE( p_module->i_config_items );
333 LOAD_IMMEDIATE( p_module->i_bool_items );
335 LOAD_IMMEDIATE( i_lines );
337 /* Allocate memory */
341 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
342 if( p_module->p_config == NULL )
344 p_module->confsize = 0;
345 msg_Err( p_module, "config error: can't duplicate p_config" );
349 p_module->confsize = i_lines;
351 /* Do the duplication job */
352 for (size_t i = 0; i < i_lines; i++ )
354 LOAD_IMMEDIATE( p_module->p_config[i] );
356 LOAD_STRING( p_module->p_config[i].psz_type );
357 LOAD_STRING( p_module->p_config[i].psz_name );
358 LOAD_STRING( p_module->p_config[i].psz_text );
359 LOAD_STRING( p_module->p_config[i].psz_longtext );
360 LOAD_STRING( p_module->p_config[i].psz_oldname );
361 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
363 if (IsConfigStringType (p_module->p_config[i].i_type))
365 LOAD_STRING (p_module->p_config[i].orig.psz);
366 p_module->p_config[i].value.psz =
367 (p_module->p_config[i].orig.psz != NULL)
368 ? strdup (p_module->p_config[i].orig.psz) : NULL;
369 p_module->p_config[i].saved.psz = NULL;
373 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
374 sizeof (p_module->p_config[i].value));
375 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
376 sizeof (p_module->p_config[i].saved));
379 p_module->p_config[i].b_dirty = false;
381 p_module->p_config[i].p_lock = &p_module->object_lock;
383 if( p_module->p_config[i].i_list )
385 if( p_module->p_config[i].ppsz_list )
388 p_module->p_config[i].ppsz_list =
389 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
390 if( p_module->p_config[i].ppsz_list )
392 for( j = 0; j < p_module->p_config[i].i_list; j++ )
393 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
394 p_module->p_config[i].ppsz_list[j] = NULL;
397 if( p_module->p_config[i].ppsz_list_text )
400 p_module->p_config[i].ppsz_list_text =
401 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
402 if( p_module->p_config[i].ppsz_list_text )
404 for( j = 0; j < p_module->p_config[i].i_list; j++ )
405 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
406 p_module->p_config[i].ppsz_list_text[j] = NULL;
409 if( p_module->p_config[i].pi_list )
411 p_module->p_config[i].pi_list =
412 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
413 if( p_module->p_config[i].pi_list )
415 for (int j = 0; j < p_module->p_config[i].i_list; j++)
416 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
421 if( p_module->p_config[i].i_action )
423 p_module->p_config[i].ppf_action =
424 malloc( p_module->p_config[i].i_action * sizeof(void *) );
425 p_module->p_config[i].ppsz_action_text =
426 malloc( p_module->p_config[i].i_action * sizeof(char *) );
428 for (int j = 0; j < p_module->p_config[i].i_action; j++)
430 p_module->p_config[i].ppf_action[j] = 0;
431 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
435 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
445 /*****************************************************************************
446 * SavePluginsCache: saves the plugins cache to a file
447 *****************************************************************************/
448 void CacheSave( vlc_object_t *p_this )
450 static char const psz_tag[] =
451 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
452 "# This file is a cache directory tag created by VLC.\r\n"
453 "# For information about cache directory tags, see:\r\n"
454 "# http://www.brynosaurus.com/cachedir/\r\n";
456 char *psz_cachedir = config_GetCacheDir();
459 module_cache_t **pp_cache;
460 uint32_t i_file_size = 0;
461 libvlc_global_data_t *p_libvlc_global = vlc_global();
463 if( !psz_cachedir ) /* XXX: this should never happen */
465 msg_Err( p_this, "unable to get cache directory" );
469 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
470 config_CreateDir( p_this, psz_cachedir );
472 snprintf( psz_filename, sizeof( psz_filename ),
473 "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
474 file = utf8_fopen( psz_filename, "wb" );
477 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
478 clearerr (file); /* what else can we do? */
482 snprintf( psz_filename, sizeof( psz_filename ),
483 "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
484 free( psz_cachedir );
485 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
487 file = utf8_fopen( psz_filename, "wb" );
491 /* Empty space for file size */
492 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
495 /* Contains version number */
496 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
499 /* Sub-version number (to avoid breakage in the dev version when cache
500 * structure changes) */
501 i_file_size = CACHE_SUBVERSION_NUM;
502 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
506 if (fprintf (file, "%5.5s", _("C")) == EOF)
510 i_file_size = ftell( file );
511 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
514 i_cache = p_libvlc_global->p_module_bank->i_cache;
515 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
517 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
520 #define SAVE_IMMEDIATE( a ) \
521 if (fwrite (&a, sizeof(a), 1, file) != 1) \
523 #define SAVE_STRING( a ) \
525 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
526 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
527 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
531 for( i = 0; i < i_cache; i++ )
533 uint32_t i_submodule;
535 /* Save common info */
536 SAVE_STRING( pp_cache[i]->psz_file );
537 SAVE_IMMEDIATE( pp_cache[i]->i_time );
538 SAVE_IMMEDIATE( pp_cache[i]->i_size );
539 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
541 if( pp_cache[i]->b_junk ) continue;
543 /* Save additional infos */
544 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
545 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
546 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
547 SAVE_STRING( pp_cache[i]->p_module->psz_help );
548 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
550 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
552 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
553 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
554 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
555 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
556 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
557 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
560 if (CacheSaveConfig (pp_cache[i]->p_module, file))
563 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
565 i_submodule = pp_cache[i]->p_module->i_children;
566 SAVE_IMMEDIATE( i_submodule );
567 for( i_submodule = 0;
568 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
572 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
574 SAVE_STRING( p_module->psz_object_name );
575 SAVE_STRING( p_module->psz_shortname );
576 SAVE_STRING( p_module->psz_longname );
577 SAVE_STRING( p_module->psz_help );
578 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
580 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
582 SAVE_STRING( p_module->psz_capability );
583 SAVE_IMMEDIATE( p_module->i_score );
584 SAVE_IMMEDIATE( p_module->i_cpu );
585 SAVE_IMMEDIATE( p_module->b_unloadable );
586 SAVE_IMMEDIATE( p_module->b_reentrant );
590 /* Fill-up file size */
591 i_file_size = ftell( file );
592 fseek( file, 0, SEEK_SET );
593 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
596 if (fclose (file) == 0)
597 return; /* success! */
601 msg_Warn (p_this, "could not write plugins cache %s (%m)",
610 static int CacheSaveConfig( module_t *p_module, FILE *file )
612 uint32_t i_lines = p_module->confsize;
614 SAVE_IMMEDIATE( p_module->i_config_items );
615 SAVE_IMMEDIATE( p_module->i_bool_items );
616 SAVE_IMMEDIATE( i_lines );
618 for (size_t i = 0; i < i_lines ; i++)
620 SAVE_IMMEDIATE( p_module->p_config[i] );
622 SAVE_STRING( p_module->p_config[i].psz_type );
623 SAVE_STRING( p_module->p_config[i].psz_name );
624 SAVE_STRING( p_module->p_config[i].psz_text );
625 SAVE_STRING( p_module->p_config[i].psz_longtext );
626 SAVE_STRING( p_module->p_config[i].psz_oldname );
627 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
629 if (IsConfigStringType (p_module->p_config[i].i_type))
630 SAVE_STRING( p_module->p_config[i].orig.psz );
632 if( p_module->p_config[i].i_list )
634 if( p_module->p_config[i].ppsz_list )
636 for (int j = 0; j < p_module->p_config[i].i_list; j++)
637 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
640 if( p_module->p_config[i].ppsz_list_text )
642 for (int j = 0; j < p_module->p_config[i].i_list; j++)
643 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
645 if( p_module->p_config[i].pi_list )
647 for (int j = 0; j < p_module->p_config[i].i_list; j++)
648 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
652 for (int j = 0; j < p_module->p_config[i].i_action; j++)
653 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
655 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
663 /*****************************************************************************
664 * CacheName: Return the cache file name for this platform.
665 *****************************************************************************/
666 static char *CacheName( void )
668 static char psz_cachename[32];
670 /* Code int size, pointer size and endianness in the filename */
671 int32_t x = 0xbe00001e;
672 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
673 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
674 return psz_cachename;
677 /*****************************************************************************
678 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
679 *****************************************************************************/
680 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
685 p_cache->pf_activate = p_module->pf_activate;
686 p_cache->pf_deactivate = p_module->pf_deactivate;
687 p_cache->handle = p_module->handle;
689 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
691 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
692 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
693 p_cchild->pf_activate = p_child->pf_activate;
694 p_cchild->pf_deactivate = p_child->pf_deactivate;
697 p_cache->b_loaded = true;
698 p_module->b_loaded = false;
701 /*****************************************************************************
702 * CacheFind: finds the cache entry corresponding to a file
703 *****************************************************************************/
704 module_cache_t *CacheFind( const char *psz_file,
705 int64_t i_time, int64_t i_size )
707 module_cache_t **pp_cache;
709 libvlc_global_data_t *p_libvlc_global = vlc_global();
711 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
712 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
714 for( i = 0; i < i_cache; i++ )
716 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
717 pp_cache[i]->i_time == i_time &&
718 pp_cache[i]->i_size == i_size ) return pp_cache[i];
724 #endif /* HAVE_DYNAMIC_PLUGINS */