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_current );
357 if (IsConfigStringType (p_module->p_config[i].i_type))
359 LOAD_STRING (p_module->p_config[i].orig.psz);
360 p_module->p_config[i].value.psz =
361 (p_module->p_config[i].orig.psz != NULL)
362 ? strdup (p_module->p_config[i].orig.psz) : NULL;
363 p_module->p_config[i].saved.psz = NULL;
367 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
368 sizeof (p_module->p_config[i].value));
369 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
370 sizeof (p_module->p_config[i].saved));
373 p_module->p_config[i].b_dirty = VLC_FALSE;
375 p_module->p_config[i].p_lock = &p_module->object_lock;
377 if( p_module->p_config[i].i_list )
379 if( p_module->p_config[i].ppsz_list )
382 p_module->p_config[i].ppsz_list =
383 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
384 if( p_module->p_config[i].ppsz_list )
386 for( j = 0; j < p_module->p_config[i].i_list; j++ )
387 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
388 p_module->p_config[i].ppsz_list[j] = NULL;
391 if( p_module->p_config[i].ppsz_list_text )
394 p_module->p_config[i].ppsz_list_text =
395 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
396 if( p_module->p_config[i].ppsz_list_text )
398 for( j = 0; j < p_module->p_config[i].i_list; j++ )
399 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
400 p_module->p_config[i].ppsz_list_text[j] = NULL;
403 if( p_module->p_config[i].pi_list )
405 p_module->p_config[i].pi_list =
406 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
407 if( p_module->p_config[i].pi_list )
409 for (int j = 0; j < p_module->p_config[i].i_list; j++)
410 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
415 if( p_module->p_config[i].i_action )
417 p_module->p_config[i].ppf_action =
418 malloc( p_module->p_config[i].i_action * sizeof(void *) );
419 p_module->p_config[i].ppsz_action_text =
420 malloc( p_module->p_config[i].i_action * sizeof(char *) );
422 for (int j = 0; j < p_module->p_config[i].i_action; j++)
424 p_module->p_config[i].ppf_action[j] = 0;
425 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
429 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
439 /*****************************************************************************
440 * SavePluginsCache: saves the plugins cache to a file
441 *****************************************************************************/
442 void CacheSave( vlc_object_t *p_this )
444 static char const psz_tag[] =
445 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
446 "# This file is a cache directory tag created by VLC.\r\n"
447 "# For information about cache directory tags, see:\r\n"
448 "# http://www.brynosaurus.com/cachedir/\r\n";
453 module_cache_t **pp_cache;
454 uint32_t i_file_size = 0;
455 libvlc_global_data_t *p_libvlc_global = vlc_global();
457 psz_cachedir = p_this->p_libvlc->psz_cachedir;
458 if( !psz_cachedir ) /* XXX: this should never happen */
460 msg_Err( p_this, "unable to get cache directory" );
464 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
465 config_CreateDir( p_this, psz_cachedir );
467 snprintf( psz_filename, sizeof( psz_filename ),
468 "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
469 file = utf8_fopen( psz_filename, "wb" );
472 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
473 clearerr (file); /* what else can we do? */
477 snprintf( psz_filename, sizeof( psz_filename ),
478 "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
479 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
481 file = utf8_fopen( psz_filename, "wb" );
485 /* Empty space for file size */
486 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
489 /* Contains version number */
490 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
493 /* Sub-version number (to avoid breakage in the dev version when cache
494 * structure changes) */
495 i_file_size = CACHE_SUBVERSION_NUM;
496 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
500 if (fprintf (file, "%5.5s", _("C")) == EOF)
504 i_file_size = ftell( file );
505 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
508 i_cache = p_libvlc_global->p_module_bank->i_cache;
509 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
511 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
514 #define SAVE_IMMEDIATE( a ) \
515 if (fwrite (&a, sizeof(a), 1, file) != 1) \
517 #define SAVE_STRING( a ) \
519 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
520 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
521 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
525 for( i = 0; i < i_cache; i++ )
527 uint32_t i_submodule;
529 /* Save common info */
530 SAVE_STRING( pp_cache[i]->psz_file );
531 SAVE_IMMEDIATE( pp_cache[i]->i_time );
532 SAVE_IMMEDIATE( pp_cache[i]->i_size );
533 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
535 if( pp_cache[i]->b_junk ) continue;
537 /* Save additional infos */
538 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
539 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
540 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
541 SAVE_STRING( pp_cache[i]->p_module->psz_help );
542 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
544 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
546 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
547 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
548 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
549 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
550 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
551 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
554 if (CacheSaveConfig (pp_cache[i]->p_module, file))
557 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
559 i_submodule = pp_cache[i]->p_module->i_children;
560 SAVE_IMMEDIATE( i_submodule );
561 for( i_submodule = 0;
562 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
566 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
568 SAVE_STRING( p_module->psz_object_name );
569 SAVE_STRING( p_module->psz_shortname );
570 SAVE_STRING( p_module->psz_longname );
571 SAVE_STRING( p_module->psz_help );
572 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
574 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
576 SAVE_STRING( p_module->psz_capability );
577 SAVE_IMMEDIATE( p_module->i_score );
578 SAVE_IMMEDIATE( p_module->i_cpu );
579 SAVE_IMMEDIATE( p_module->b_unloadable );
580 SAVE_IMMEDIATE( p_module->b_reentrant );
584 /* Fill-up file size */
585 i_file_size = ftell( file );
586 fseek( file, 0, SEEK_SET );
587 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
590 if (fclose (file) == 0)
591 return; /* success! */
595 msg_Warn (p_this, "could not write plugins cache %s (%m)",
604 static int CacheSaveConfig( module_t *p_module, FILE *file )
606 uint32_t i_lines = p_module->confsize;
608 SAVE_IMMEDIATE( p_module->i_config_items );
609 SAVE_IMMEDIATE( p_module->i_bool_items );
610 SAVE_IMMEDIATE( i_lines );
612 for (size_t i = 0; i < i_lines ; i++)
614 SAVE_IMMEDIATE( p_module->p_config[i] );
616 SAVE_STRING( p_module->p_config[i].psz_type );
617 SAVE_STRING( p_module->p_config[i].psz_name );
618 SAVE_STRING( p_module->p_config[i].psz_text );
619 SAVE_STRING( p_module->p_config[i].psz_longtext );
620 SAVE_STRING( p_module->p_config[i].psz_current );
621 if (IsConfigStringType (p_module->p_config[i].i_type))
622 SAVE_STRING( p_module->p_config[i].orig.psz );
624 if( p_module->p_config[i].i_list )
626 if( p_module->p_config[i].ppsz_list )
628 for (int j = 0; j < p_module->p_config[i].i_list; j++)
629 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
632 if( p_module->p_config[i].ppsz_list_text )
634 for (int j = 0; j < p_module->p_config[i].i_list; j++)
635 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
637 if( p_module->p_config[i].pi_list )
639 for (int j = 0; j < p_module->p_config[i].i_list; j++)
640 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
644 for (int j = 0; j < p_module->p_config[i].i_action; j++)
645 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
647 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
655 /*****************************************************************************
656 * CacheName: Return the cache file name for this platform.
657 *****************************************************************************/
658 static char *CacheName( void )
660 static char psz_cachename[32];
662 /* Code int size, pointer size and endianness in the filename */
663 int32_t x = 0xbe00001e;
664 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
665 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
666 return psz_cachename;
669 /*****************************************************************************
670 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
671 *****************************************************************************/
672 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
677 p_cache->pf_activate = p_module->pf_activate;
678 p_cache->pf_deactivate = p_module->pf_deactivate;
679 p_cache->handle = p_module->handle;
681 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
683 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
684 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
685 p_cchild->pf_activate = p_child->pf_activate;
686 p_cchild->pf_deactivate = p_child->pf_deactivate;
689 p_cache->b_loaded = VLC_TRUE;
690 p_module->b_loaded = VLC_FALSE;
693 /*****************************************************************************
694 * CacheFind: finds the cache entry corresponding to a file
695 *****************************************************************************/
696 module_cache_t *CacheFind( vlc_object_t *p_this, const char *psz_file,
697 int64_t i_time, int64_t i_size )
699 module_cache_t **pp_cache;
701 libvlc_global_data_t *p_libvlc_global = vlc_global();
703 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
704 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
706 for( i = 0; i < i_cache; i++ )
708 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
709 pp_cache[i]->i_time == i_time &&
710 pp_cache[i]->i_size == i_size ) return pp_cache[i];
716 #endif /* HAVE_DYNAMIC_PLUGINS */