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 *****************************************************************************/
30 #include <stdlib.h> /* free(), strtol() */
31 #include <stdio.h> /* sprintf() */
32 #include <string.h> /* strdup() */
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
41 #if !defined(HAVE_DYNAMIC_PLUGINS)
42 /* no support for plugins */
43 #elif defined(HAVE_DL_DYLD)
44 # if defined(HAVE_MACH_O_DYLD_H)
45 # include <mach-o/dyld.h>
47 #elif defined(HAVE_DL_BEOS)
48 # if defined(HAVE_IMAGE_H)
51 #elif defined(HAVE_DL_WINDOWS)
53 #elif defined(HAVE_DL_DLOPEN)
54 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
57 # if defined(HAVE_SYS_DL_H)
60 #elif defined(HAVE_DL_SHL_LOAD)
61 # if defined(HAVE_DL_H)
66 #include "config/config.h"
68 #include "vlc_charset.h"
70 #include "modules/modules.h"
73 /*****************************************************************************
75 *****************************************************************************/
76 #ifdef HAVE_DYNAMIC_PLUGINS
77 static int CacheLoadConfig ( module_t *, FILE * );
78 static int CacheSaveConfig ( module_t *, FILE * );
79 static char * CacheName ( void );
82 * (only used to avoid breakage in dev version when cache structure changes) */
83 #define CACHE_SUBVERSION_NUM 3
85 /*****************************************************************************
86 * LoadPluginsCache: loads the plugins cache file
87 *****************************************************************************
88 * This function will load the plugin cache if present and valid. This cache
89 * will in turn be queried by AllocateAllPlugins() to see if it needs to
90 * actually load the dynamically loadable module.
91 * This allows us to only fully load plugins when they are actually used.
92 *****************************************************************************/
93 void CacheLoad( vlc_object_t *p_this )
95 char *psz_filename, *psz_cachedir;
97 int i, j, i_size, i_read;
98 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
99 char p_cachelang[6], p_lang[6];
101 module_cache_t **pp_cache = 0;
102 int32_t i_file_size, i_marker;
103 libvlc_global_data_t *p_libvlc_global = vlc_global();
105 psz_cachedir = p_this->p_libvlc->psz_cachedir;
106 if( !psz_cachedir ) /* XXX: this should never happen */
108 msg_Err( p_this, "Unable to get cache directory" );
112 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s",
113 psz_cachedir, CacheName() );
116 msg_Err( p_this, "out of memory" );
120 if( p_libvlc_global->p_module_bank->b_cache_delete )
122 #if !defined( UNDER_CE )
123 unlink( psz_filename );
125 wchar_t psz_wf[MAX_PATH];
126 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
127 DeleteFile( psz_wf );
129 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
130 free( psz_filename );
134 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
136 file = utf8_fopen( psz_filename, "rb" );
139 msg_Warn( p_this, "could not open plugins cache file %s for reading",
141 free( psz_filename );
144 free( psz_filename );
146 /* Check the file size */
147 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
148 if( i_read != sizeof(i_file_size) )
150 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
156 fseek( file, 0, SEEK_END );
157 if( ftell( file ) != i_file_size )
159 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
160 "(corrupted size)" );
164 fseek( file, sizeof(i_file_size), SEEK_SET );
166 /* Check the file is a plugins cache */
167 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
168 i_read = fread( p_cachestring, 1, i_size, file );
169 if( i_read != i_size ||
170 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
172 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
177 /* Check Sub-version number */
178 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
179 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
181 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
182 "(corrupted header)" );
187 /* Check the language hasn't changed */
188 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
189 i_read = fread( p_cachelang, 1, i_size, file );
190 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
192 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
193 "(language changed)" );
198 /* Check header marker */
199 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
200 if( i_read != sizeof(i_marker) ||
201 i_marker != ftell( file ) - (int)sizeof(i_marker) )
203 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
204 "(corrupted header)" );
209 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
210 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
212 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
213 "(file too short)" );
219 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
220 malloc( i_cache * sizeof(void *) );
222 #define LOAD_IMMEDIATE(a) \
223 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
224 #define LOAD_STRING(a) \
227 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
228 || ( i_size > 16384 ) ) \
231 char *psz = malloc( i_size ); \
232 if( fread( psz, i_size, 1, file ) != 1 ) { \
236 if( psz[i_size-1] ) { \
244 for( i = 0; i < i_cache; i++ )
249 pp_cache[i] = malloc( sizeof(module_cache_t) );
250 p_libvlc_global->p_module_bank->i_loaded_cache++;
252 /* Load common info */
253 LOAD_STRING( pp_cache[i]->psz_file );
254 LOAD_IMMEDIATE( pp_cache[i]->i_time );
255 LOAD_IMMEDIATE( pp_cache[i]->i_size );
256 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
257 pp_cache[i]->b_used = VLC_FALSE;
259 if( pp_cache[i]->b_junk ) continue;
261 pp_cache[i]->p_module = vlc_module_create( p_this );
263 /* Load additional infos */
264 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
265 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
266 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
267 LOAD_STRING( pp_cache[i]->p_module->psz_help );
268 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
270 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
272 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
273 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
274 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
275 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
276 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
277 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
280 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
283 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
285 LOAD_IMMEDIATE( i_submodules );
287 while( i_submodules-- )
289 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
290 LOAD_STRING( p_module->psz_object_name );
291 LOAD_STRING( p_module->psz_shortname );
292 LOAD_STRING( p_module->psz_longname );
293 LOAD_STRING( p_module->psz_help );
294 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
296 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
298 LOAD_STRING( p_module->psz_capability );
299 LOAD_IMMEDIATE( p_module->i_score );
300 LOAD_IMMEDIATE( p_module->i_cpu );
301 LOAD_IMMEDIATE( p_module->b_unloadable );
302 LOAD_IMMEDIATE( p_module->b_reentrant );
311 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
314 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
321 static int CacheLoadConfig( module_t *p_module, FILE *file )
326 /* Calculate the structure length */
327 LOAD_IMMEDIATE( p_module->i_config_items );
328 LOAD_IMMEDIATE( p_module->i_bool_items );
330 LOAD_IMMEDIATE( i_lines );
332 /* Allocate memory */
336 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
337 if( p_module->p_config == NULL )
339 p_module->confsize = 0;
340 msg_Err( p_module, "config error: can't duplicate p_config" );
344 p_module->confsize = i_lines;
346 /* Do the duplication job */
347 for (size_t i = 0; i < i_lines; i++ )
349 LOAD_IMMEDIATE( p_module->p_config[i] );
351 LOAD_STRING( p_module->p_config[i].psz_type );
352 LOAD_STRING( p_module->p_config[i].psz_name );
353 LOAD_STRING( p_module->p_config[i].psz_text );
354 LOAD_STRING( p_module->p_config[i].psz_longtext );
355 LOAD_STRING( p_module->p_config[i].psz_oldname );
356 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
358 if (IsConfigStringType (p_module->p_config[i].i_type))
360 LOAD_STRING (p_module->p_config[i].orig.psz);
361 p_module->p_config[i].value.psz =
362 (p_module->p_config[i].orig.psz != NULL)
363 ? strdup (p_module->p_config[i].orig.psz) : NULL;
364 p_module->p_config[i].saved.psz = NULL;
368 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
369 sizeof (p_module->p_config[i].value));
370 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
371 sizeof (p_module->p_config[i].saved));
374 p_module->p_config[i].b_dirty = VLC_FALSE;
376 p_module->p_config[i].p_lock = &p_module->object_lock;
378 if( p_module->p_config[i].i_list )
380 if( p_module->p_config[i].ppsz_list )
383 p_module->p_config[i].ppsz_list =
384 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
385 if( p_module->p_config[i].ppsz_list )
387 for( j = 0; j < p_module->p_config[i].i_list; j++ )
388 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
389 p_module->p_config[i].ppsz_list[j] = NULL;
392 if( p_module->p_config[i].ppsz_list_text )
395 p_module->p_config[i].ppsz_list_text =
396 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
397 if( p_module->p_config[i].ppsz_list_text )
399 for( j = 0; j < p_module->p_config[i].i_list; j++ )
400 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
401 p_module->p_config[i].ppsz_list_text[j] = NULL;
404 if( p_module->p_config[i].pi_list )
406 p_module->p_config[i].pi_list =
407 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
408 if( p_module->p_config[i].pi_list )
410 for (int j = 0; j < p_module->p_config[i].i_list; j++)
411 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
416 if( p_module->p_config[i].i_action )
418 p_module->p_config[i].ppf_action =
419 malloc( p_module->p_config[i].i_action * sizeof(void *) );
420 p_module->p_config[i].ppsz_action_text =
421 malloc( p_module->p_config[i].i_action * sizeof(char *) );
423 for (int j = 0; j < p_module->p_config[i].i_action; j++)
425 p_module->p_config[i].ppf_action[j] = 0;
426 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
430 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
440 /*****************************************************************************
441 * SavePluginsCache: saves the plugins cache to a file
442 *****************************************************************************/
443 void CacheSave( vlc_object_t *p_this )
445 static char const psz_tag[] =
446 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
447 "# This file is a cache directory tag created by VLC.\r\n"
448 "# For information about cache directory tags, see:\r\n"
449 "# http://www.brynosaurus.com/cachedir/\r\n";
454 module_cache_t **pp_cache;
455 uint32_t i_file_size = 0;
456 libvlc_global_data_t *p_libvlc_global = vlc_global();
458 psz_cachedir = p_this->p_libvlc->psz_cachedir;
459 if( !psz_cachedir ) /* XXX: this should never happen */
461 msg_Err( p_this, "unable to get cache directory" );
465 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
466 config_CreateDir( p_this, psz_cachedir );
468 snprintf( psz_filename, sizeof( psz_filename ),
469 "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
470 file = utf8_fopen( psz_filename, "wb" );
473 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
474 clearerr (file); /* what else can we do? */
478 snprintf( psz_filename, sizeof( psz_filename ),
479 "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
480 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
482 file = utf8_fopen( psz_filename, "wb" );
486 /* Empty space for file size */
487 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
490 /* Contains version number */
491 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
494 /* Sub-version number (to avoid breakage in the dev version when cache
495 * structure changes) */
496 i_file_size = CACHE_SUBVERSION_NUM;
497 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
501 if (fprintf (file, "%5.5s", _("C")) == EOF)
505 i_file_size = ftell( file );
506 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
509 i_cache = p_libvlc_global->p_module_bank->i_cache;
510 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
512 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
515 #define SAVE_IMMEDIATE( a ) \
516 if (fwrite (&a, sizeof(a), 1, file) != 1) \
518 #define SAVE_STRING( a ) \
520 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
521 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
522 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
526 for( i = 0; i < i_cache; i++ )
528 uint32_t i_submodule;
530 /* Save common info */
531 SAVE_STRING( pp_cache[i]->psz_file );
532 SAVE_IMMEDIATE( pp_cache[i]->i_time );
533 SAVE_IMMEDIATE( pp_cache[i]->i_size );
534 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
536 if( pp_cache[i]->b_junk ) continue;
538 /* Save additional infos */
539 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
540 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
541 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
542 SAVE_STRING( pp_cache[i]->p_module->psz_help );
543 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
545 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
547 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
548 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
549 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
550 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
551 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
552 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
555 if (CacheSaveConfig (pp_cache[i]->p_module, file))
558 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
560 i_submodule = pp_cache[i]->p_module->i_children;
561 SAVE_IMMEDIATE( i_submodule );
562 for( i_submodule = 0;
563 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
567 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
569 SAVE_STRING( p_module->psz_object_name );
570 SAVE_STRING( p_module->psz_shortname );
571 SAVE_STRING( p_module->psz_longname );
572 SAVE_STRING( p_module->psz_help );
573 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
575 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
577 SAVE_STRING( p_module->psz_capability );
578 SAVE_IMMEDIATE( p_module->i_score );
579 SAVE_IMMEDIATE( p_module->i_cpu );
580 SAVE_IMMEDIATE( p_module->b_unloadable );
581 SAVE_IMMEDIATE( p_module->b_reentrant );
585 /* Fill-up file size */
586 i_file_size = ftell( file );
587 fseek( file, 0, SEEK_SET );
588 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
591 if (fclose (file) == 0)
592 return; /* success! */
596 msg_Warn (p_this, "could not write plugins cache %s (%m)",
605 static int CacheSaveConfig( module_t *p_module, FILE *file )
607 uint32_t i_lines = p_module->confsize;
609 SAVE_IMMEDIATE( p_module->i_config_items );
610 SAVE_IMMEDIATE( p_module->i_bool_items );
611 SAVE_IMMEDIATE( i_lines );
613 for (size_t i = 0; i < i_lines ; i++)
615 SAVE_IMMEDIATE( p_module->p_config[i] );
617 SAVE_STRING( p_module->p_config[i].psz_type );
618 SAVE_STRING( p_module->p_config[i].psz_name );
619 SAVE_STRING( p_module->p_config[i].psz_text );
620 SAVE_STRING( p_module->p_config[i].psz_longtext );
621 SAVE_STRING( p_module->p_config[i].psz_oldname );
622 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
624 if (IsConfigStringType (p_module->p_config[i].i_type))
625 SAVE_STRING( p_module->p_config[i].orig.psz );
627 if( p_module->p_config[i].i_list )
629 if( p_module->p_config[i].ppsz_list )
631 for (int j = 0; j < p_module->p_config[i].i_list; j++)
632 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
635 if( p_module->p_config[i].ppsz_list_text )
637 for (int j = 0; j < p_module->p_config[i].i_list; j++)
638 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
640 if( p_module->p_config[i].pi_list )
642 for (int j = 0; j < p_module->p_config[i].i_list; j++)
643 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
647 for (int j = 0; j < p_module->p_config[i].i_action; j++)
648 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
650 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
658 /*****************************************************************************
659 * CacheName: Return the cache file name for this platform.
660 *****************************************************************************/
661 static char *CacheName( void )
663 static char psz_cachename[32];
665 /* Code int size, pointer size and endianness in the filename */
666 int32_t x = 0xbe00001e;
667 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
668 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
669 return psz_cachename;
672 /*****************************************************************************
673 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
674 *****************************************************************************/
675 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
680 p_cache->pf_activate = p_module->pf_activate;
681 p_cache->pf_deactivate = p_module->pf_deactivate;
682 p_cache->handle = p_module->handle;
684 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
686 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
687 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
688 p_cchild->pf_activate = p_child->pf_activate;
689 p_cchild->pf_deactivate = p_child->pf_deactivate;
692 p_cache->b_loaded = VLC_TRUE;
693 p_module->b_loaded = VLC_FALSE;
696 /*****************************************************************************
697 * CacheFind: finds the cache entry corresponding to a file
698 *****************************************************************************/
699 module_cache_t *CacheFind( vlc_object_t *p_this, const char *psz_file,
700 int64_t i_time, int64_t i_size )
702 module_cache_t **pp_cache;
704 libvlc_global_data_t *p_libvlc_global = vlc_global();
706 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
707 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
709 for( i = 0; i < i_cache; i++ )
711 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
712 pp_cache[i]->i_time == i_time &&
713 pp_cache[i]->i_size == i_size ) return pp_cache[i];
719 #endif /* HAVE_DYNAMIC_PLUGINS */