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() */
37 #include <vlc_plugin.h>
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
46 #if !defined(HAVE_DYNAMIC_PLUGINS)
47 /* no support for plugins */
48 #elif defined(HAVE_DL_DYLD)
49 # if defined(HAVE_MACH_O_DYLD_H)
50 # include <mach-o/dyld.h>
52 #elif defined(HAVE_DL_BEOS)
53 # if defined(HAVE_IMAGE_H)
56 #elif defined(HAVE_DL_WINDOWS)
58 #elif defined(HAVE_DL_DLOPEN)
59 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
62 # if defined(HAVE_SYS_DL_H)
65 #elif defined(HAVE_DL_SHL_LOAD)
66 # if defined(HAVE_DL_H)
71 #include "config/configuration.h"
73 #include "vlc_charset.h"
75 #include "modules/modules.h"
78 /*****************************************************************************
80 *****************************************************************************/
81 #ifdef HAVE_DYNAMIC_PLUGINS
82 static int CacheLoadConfig ( module_t *, FILE * );
83 static int CacheSaveConfig ( module_t *, FILE * );
84 static char * CacheName ( void );
87 * (only used to avoid breakage in dev version when cache structure changes) */
88 #define CACHE_SUBVERSION_NUM 3
90 /*****************************************************************************
91 * LoadPluginsCache: loads the plugins cache file
92 *****************************************************************************
93 * This function will load the plugin cache if present and valid. This cache
94 * will in turn be queried by AllocateAllPlugins() to see if it needs to
95 * actually load the dynamically loadable module.
96 * This allows us to only fully load plugins when they are actually used.
97 *****************************************************************************/
98 void CacheLoad( vlc_object_t *p_this )
100 char *psz_filename, *psz_cachedir = config_GetCacheDir();
102 int i, j, i_size, i_read;
103 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
104 char p_cachelang[6], p_lang[6];
106 module_cache_t **pp_cache = 0;
107 int32_t i_file_size, i_marker;
108 libvlc_global_data_t *p_libvlc_global = vlc_global();
110 if( !psz_cachedir ) /* XXX: this should never happen */
112 msg_Err( p_this, "Unable to get cache directory" );
116 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s",
117 psz_cachedir, CacheName() );
118 free( psz_cachedir );
124 if( p_libvlc_global->p_module_bank->b_cache_delete )
126 #if !defined( UNDER_CE )
127 unlink( psz_filename );
129 wchar_t psz_wf[MAX_PATH];
130 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
131 DeleteFile( psz_wf );
133 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
134 free( psz_filename );
138 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
140 file = utf8_fopen( psz_filename, "rb" );
143 msg_Warn( p_this, "could not open plugins cache file %s for reading",
145 free( psz_filename );
148 free( psz_filename );
150 /* Check the file size */
151 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
152 if( i_read != sizeof(i_file_size) )
154 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
160 fseek( file, 0, SEEK_END );
161 if( ftell( file ) != i_file_size )
163 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
164 "(corrupted size)" );
168 fseek( file, sizeof(i_file_size), SEEK_SET );
170 /* Check the file is a plugins cache */
171 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
172 i_read = fread( p_cachestring, 1, i_size, file );
173 if( i_read != i_size ||
174 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
176 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
181 /* Check Sub-version number */
182 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
183 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
185 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
186 "(corrupted header)" );
191 /* Check the language hasn't changed */
192 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
193 i_read = fread( p_cachelang, 1, i_size, file );
194 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
196 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
197 "(language changed)" );
202 /* Check header marker */
203 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
204 if( i_read != sizeof(i_marker) ||
205 i_marker != ftell( file ) - (int)sizeof(i_marker) )
207 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
208 "(corrupted header)" );
213 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
214 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
216 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
217 "(file too short)" );
223 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
224 malloc( i_cache * sizeof(void *) );
226 #define LOAD_IMMEDIATE(a) \
227 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
228 #define LOAD_STRING(a) \
231 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
232 || ( i_size > 16384 ) ) \
235 char *psz = malloc( i_size ); \
236 if( fread( psz, i_size, 1, file ) != 1 ) { \
240 if( psz[i_size-1] ) { \
248 for( i = 0; i < i_cache; i++ )
253 pp_cache[i] = malloc( sizeof(module_cache_t) );
254 p_libvlc_global->p_module_bank->i_loaded_cache++;
256 /* Load common info */
257 LOAD_STRING( pp_cache[i]->psz_file );
258 LOAD_IMMEDIATE( pp_cache[i]->i_time );
259 LOAD_IMMEDIATE( pp_cache[i]->i_size );
260 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
261 pp_cache[i]->b_used = false;
263 if( pp_cache[i]->b_junk ) continue;
265 pp_cache[i]->p_module = vlc_module_create( p_this );
267 /* Load additional infos */
268 free( pp_cache[i]->p_module->psz_object_name );
269 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
270 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
271 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
272 LOAD_STRING( pp_cache[i]->p_module->psz_help );
273 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
275 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
277 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
278 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
279 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
280 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
281 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
282 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
285 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
288 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
290 LOAD_IMMEDIATE( i_submodules );
292 while( i_submodules-- )
294 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
295 free( p_module->psz_object_name );
296 LOAD_STRING( p_module->psz_object_name );
297 LOAD_STRING( p_module->psz_shortname );
298 LOAD_STRING( p_module->psz_longname );
299 LOAD_STRING( p_module->psz_help );
300 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
302 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
304 LOAD_STRING( p_module->psz_capability );
305 LOAD_IMMEDIATE( p_module->i_score );
306 LOAD_IMMEDIATE( p_module->i_cpu );
307 LOAD_IMMEDIATE( p_module->b_unloadable );
308 LOAD_IMMEDIATE( p_module->b_reentrant );
317 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
320 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
327 static int CacheLoadConfig( module_t *p_module, FILE *file )
332 /* Calculate the structure length */
333 LOAD_IMMEDIATE( p_module->i_config_items );
334 LOAD_IMMEDIATE( p_module->i_bool_items );
336 LOAD_IMMEDIATE( i_lines );
338 /* Allocate memory */
342 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
343 if( p_module->p_config == NULL )
345 p_module->confsize = 0;
346 msg_Err( p_module, "config error: can't duplicate p_config" );
350 p_module->confsize = i_lines;
352 /* Do the duplication job */
353 for (size_t i = 0; i < i_lines; i++ )
355 LOAD_IMMEDIATE( p_module->p_config[i] );
357 LOAD_STRING( p_module->p_config[i].psz_type );
358 LOAD_STRING( p_module->p_config[i].psz_name );
359 LOAD_STRING( p_module->p_config[i].psz_text );
360 LOAD_STRING( p_module->p_config[i].psz_longtext );
361 LOAD_STRING( p_module->p_config[i].psz_oldname );
362 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
364 if (IsConfigStringType (p_module->p_config[i].i_type))
366 LOAD_STRING (p_module->p_config[i].orig.psz);
367 p_module->p_config[i].value.psz =
368 (p_module->p_config[i].orig.psz != NULL)
369 ? strdup (p_module->p_config[i].orig.psz) : NULL;
370 p_module->p_config[i].saved.psz = NULL;
374 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
375 sizeof (p_module->p_config[i].value));
376 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
377 sizeof (p_module->p_config[i].saved));
380 p_module->p_config[i].b_dirty = false;
382 p_module->p_config[i].p_lock = &p_module->object_lock;
384 if( p_module->p_config[i].i_list )
386 if( p_module->p_config[i].ppsz_list )
389 p_module->p_config[i].ppsz_list =
390 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
391 if( p_module->p_config[i].ppsz_list )
393 for( j = 0; j < p_module->p_config[i].i_list; j++ )
394 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
395 p_module->p_config[i].ppsz_list[j] = NULL;
398 if( p_module->p_config[i].ppsz_list_text )
401 p_module->p_config[i].ppsz_list_text =
402 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
403 if( p_module->p_config[i].ppsz_list_text )
405 for( j = 0; j < p_module->p_config[i].i_list; j++ )
406 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
407 p_module->p_config[i].ppsz_list_text[j] = NULL;
410 if( p_module->p_config[i].pi_list )
412 p_module->p_config[i].pi_list =
413 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
414 if( p_module->p_config[i].pi_list )
416 for (int j = 0; j < p_module->p_config[i].i_list; j++)
417 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
422 if( p_module->p_config[i].i_action )
424 p_module->p_config[i].ppf_action =
425 malloc( p_module->p_config[i].i_action * sizeof(void *) );
426 p_module->p_config[i].ppsz_action_text =
427 malloc( p_module->p_config[i].i_action * sizeof(char *) );
429 for (int j = 0; j < p_module->p_config[i].i_action; j++)
431 p_module->p_config[i].ppf_action[j] = 0;
432 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
436 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
446 /*****************************************************************************
447 * SavePluginsCache: saves the plugins cache to a file
448 *****************************************************************************/
449 void CacheSave( vlc_object_t *p_this )
451 static char const psz_tag[] =
452 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
453 "# This file is a cache directory tag created by VLC.\r\n"
454 "# For information about cache directory tags, see:\r\n"
455 "# http://www.brynosaurus.com/cachedir/\r\n";
457 char *psz_cachedir = config_GetCacheDir();
460 module_cache_t **pp_cache;
461 uint32_t i_file_size = 0;
462 libvlc_global_data_t *p_libvlc_global = vlc_global();
464 if( !psz_cachedir ) /* XXX: this should never happen */
466 msg_Err( p_this, "unable to get cache directory" );
470 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
471 config_CreateDir( p_this, psz_cachedir );
473 snprintf( psz_filename, sizeof( psz_filename ),
474 "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
475 file = utf8_fopen( psz_filename, "wb" );
478 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
479 clearerr (file); /* what else can we do? */
483 snprintf( psz_filename, sizeof( psz_filename ),
484 "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
485 free( psz_cachedir );
486 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
488 file = utf8_fopen( psz_filename, "wb" );
492 /* Empty space for file size */
493 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
496 /* Contains version number */
497 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
500 /* Sub-version number (to avoid breakage in the dev version when cache
501 * structure changes) */
502 i_file_size = CACHE_SUBVERSION_NUM;
503 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
507 if (fprintf (file, "%5.5s", _("C")) == EOF)
511 i_file_size = ftell( file );
512 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
515 i_cache = p_libvlc_global->p_module_bank->i_cache;
516 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
518 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
521 #define SAVE_IMMEDIATE( a ) \
522 if (fwrite (&a, sizeof(a), 1, file) != 1) \
524 #define SAVE_STRING( a ) \
526 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
527 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
528 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
532 for( i = 0; i < i_cache; i++ )
534 uint32_t i_submodule;
536 /* Save common info */
537 SAVE_STRING( pp_cache[i]->psz_file );
538 SAVE_IMMEDIATE( pp_cache[i]->i_time );
539 SAVE_IMMEDIATE( pp_cache[i]->i_size );
540 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
542 if( pp_cache[i]->b_junk ) continue;
544 /* Save additional infos */
545 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
546 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
547 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
548 SAVE_STRING( pp_cache[i]->p_module->psz_help );
549 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
551 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
553 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
554 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
555 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
556 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
557 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
558 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
561 if (CacheSaveConfig (pp_cache[i]->p_module, file))
564 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
566 i_submodule = vlc_internals( pp_cache[i]->p_module )->i_children;
567 SAVE_IMMEDIATE( i_submodule );
568 for( i_submodule = 0;
569 i_submodule < (unsigned)vlc_internals( pp_cache[i]->p_module)->i_children;
573 (module_t *)vlc_internals( pp_cache[i]->p_module )->pp_children[i_submodule];
575 SAVE_STRING( p_module->psz_object_name );
576 SAVE_STRING( p_module->psz_shortname );
577 SAVE_STRING( p_module->psz_longname );
578 SAVE_STRING( p_module->psz_help );
579 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
581 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
583 SAVE_STRING( p_module->psz_capability );
584 SAVE_IMMEDIATE( p_module->i_score );
585 SAVE_IMMEDIATE( p_module->i_cpu );
586 SAVE_IMMEDIATE( p_module->b_unloadable );
587 SAVE_IMMEDIATE( p_module->b_reentrant );
591 /* Fill-up file size */
592 i_file_size = ftell( file );
593 fseek( file, 0, SEEK_SET );
594 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
597 if (fclose (file) == 0)
598 return; /* success! */
602 msg_Warn (p_this, "could not write plugins cache %s (%m)",
611 static int CacheSaveConfig( module_t *p_module, FILE *file )
613 uint32_t i_lines = p_module->confsize;
615 SAVE_IMMEDIATE( p_module->i_config_items );
616 SAVE_IMMEDIATE( p_module->i_bool_items );
617 SAVE_IMMEDIATE( i_lines );
619 for (size_t i = 0; i < i_lines ; i++)
621 SAVE_IMMEDIATE( p_module->p_config[i] );
623 SAVE_STRING( p_module->p_config[i].psz_type );
624 SAVE_STRING( p_module->p_config[i].psz_name );
625 SAVE_STRING( p_module->p_config[i].psz_text );
626 SAVE_STRING( p_module->p_config[i].psz_longtext );
627 SAVE_STRING( p_module->p_config[i].psz_oldname );
628 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
630 if (IsConfigStringType (p_module->p_config[i].i_type))
631 SAVE_STRING( p_module->p_config[i].orig.psz );
633 if( p_module->p_config[i].i_list )
635 if( p_module->p_config[i].ppsz_list )
637 for (int j = 0; j < p_module->p_config[i].i_list; j++)
638 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
641 if( p_module->p_config[i].ppsz_list_text )
643 for (int j = 0; j < p_module->p_config[i].i_list; j++)
644 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
646 if( p_module->p_config[i].pi_list )
648 for (int j = 0; j < p_module->p_config[i].i_list; j++)
649 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
653 for (int j = 0; j < p_module->p_config[i].i_action; j++)
654 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
656 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
664 /*****************************************************************************
665 * CacheName: Return the cache file name for this platform.
666 *****************************************************************************/
667 static char *CacheName( void )
669 static char psz_cachename[32];
671 /* Code int size, pointer size and endianness in the filename */
672 int32_t x = 0xbe00001e;
673 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
674 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
675 return psz_cachename;
678 /*****************************************************************************
679 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
680 *****************************************************************************/
681 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
686 p_cache->pf_activate = p_module->pf_activate;
687 p_cache->pf_deactivate = p_module->pf_deactivate;
688 p_cache->handle = p_module->handle;
690 for( i_submodule = 0; i_submodule < vlc_internals( p_module )->i_children; i_submodule++ )
692 module_t *p_child = (module_t*)vlc_internals( p_module )->pp_children[i_submodule];
693 module_t *p_cchild = (module_t*)vlc_internals( p_cache )->pp_children[i_submodule];
694 p_cchild->pf_activate = p_child->pf_activate;
695 p_cchild->pf_deactivate = p_child->pf_deactivate;
698 p_cache->b_loaded = true;
699 p_module->b_loaded = false;
702 /*****************************************************************************
703 * CacheFind: finds the cache entry corresponding to a file
704 *****************************************************************************/
705 module_cache_t *CacheFind( const char *psz_file,
706 int64_t i_time, int64_t i_size )
708 module_cache_t **pp_cache;
710 libvlc_global_data_t *p_libvlc_global = vlc_global();
712 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
713 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
715 for( i = 0; i < i_cache; i++ )
717 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
718 pp_cache[i]->i_time == i_time &&
719 pp_cache[i]->i_size == i_size ) return pp_cache[i];
725 #endif /* HAVE_DYNAMIC_PLUGINS */