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;
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 psz_cachedir = p_this->p_libvlc->psz_cachedir;
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() );
120 msg_Err( p_this, "out of memory" );
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 = VLC_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 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 LOAD_STRING( p_module->psz_object_name );
295 LOAD_STRING( p_module->psz_shortname );
296 LOAD_STRING( p_module->psz_longname );
297 LOAD_STRING( p_module->psz_help );
298 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
300 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
302 LOAD_STRING( p_module->psz_capability );
303 LOAD_IMMEDIATE( p_module->i_score );
304 LOAD_IMMEDIATE( p_module->i_cpu );
305 LOAD_IMMEDIATE( p_module->b_unloadable );
306 LOAD_IMMEDIATE( p_module->b_reentrant );
315 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
318 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
325 static int CacheLoadConfig( module_t *p_module, FILE *file )
330 /* Calculate the structure length */
331 LOAD_IMMEDIATE( p_module->i_config_items );
332 LOAD_IMMEDIATE( p_module->i_bool_items );
334 LOAD_IMMEDIATE( i_lines );
336 /* Allocate memory */
340 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
341 if( p_module->p_config == NULL )
343 p_module->confsize = 0;
344 msg_Err( p_module, "config error: can't duplicate p_config" );
348 p_module->confsize = i_lines;
350 /* Do the duplication job */
351 for (size_t i = 0; i < i_lines; i++ )
353 LOAD_IMMEDIATE( p_module->p_config[i] );
355 LOAD_STRING( p_module->p_config[i].psz_type );
356 LOAD_STRING( p_module->p_config[i].psz_name );
357 LOAD_STRING( p_module->p_config[i].psz_text );
358 LOAD_STRING( p_module->p_config[i].psz_longtext );
359 LOAD_STRING( p_module->p_config[i].psz_oldname );
360 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
362 if (IsConfigStringType (p_module->p_config[i].i_type))
364 LOAD_STRING (p_module->p_config[i].orig.psz);
365 p_module->p_config[i].value.psz =
366 (p_module->p_config[i].orig.psz != NULL)
367 ? strdup (p_module->p_config[i].orig.psz) : NULL;
368 p_module->p_config[i].saved.psz = NULL;
372 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
373 sizeof (p_module->p_config[i].value));
374 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
375 sizeof (p_module->p_config[i].saved));
378 p_module->p_config[i].b_dirty = VLC_FALSE;
380 p_module->p_config[i].p_lock = &p_module->object_lock;
382 if( p_module->p_config[i].i_list )
384 if( p_module->p_config[i].ppsz_list )
387 p_module->p_config[i].ppsz_list =
388 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
389 if( p_module->p_config[i].ppsz_list )
391 for( j = 0; j < p_module->p_config[i].i_list; j++ )
392 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
393 p_module->p_config[i].ppsz_list[j] = NULL;
396 if( p_module->p_config[i].ppsz_list_text )
399 p_module->p_config[i].ppsz_list_text =
400 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
401 if( p_module->p_config[i].ppsz_list_text )
403 for( j = 0; j < p_module->p_config[i].i_list; j++ )
404 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
405 p_module->p_config[i].ppsz_list_text[j] = NULL;
408 if( p_module->p_config[i].pi_list )
410 p_module->p_config[i].pi_list =
411 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
412 if( p_module->p_config[i].pi_list )
414 for (int j = 0; j < p_module->p_config[i].i_list; j++)
415 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
420 if( p_module->p_config[i].i_action )
422 p_module->p_config[i].ppf_action =
423 malloc( p_module->p_config[i].i_action * sizeof(void *) );
424 p_module->p_config[i].ppsz_action_text =
425 malloc( p_module->p_config[i].i_action * sizeof(char *) );
427 for (int j = 0; j < p_module->p_config[i].i_action; j++)
429 p_module->p_config[i].ppf_action[j] = 0;
430 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
434 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
444 /*****************************************************************************
445 * SavePluginsCache: saves the plugins cache to a file
446 *****************************************************************************/
447 void CacheSave( vlc_object_t *p_this )
449 static char const psz_tag[] =
450 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
451 "# This file is a cache directory tag created by VLC.\r\n"
452 "# For information about cache directory tags, see:\r\n"
453 "# http://www.brynosaurus.com/cachedir/\r\n";
458 module_cache_t **pp_cache;
459 uint32_t i_file_size = 0;
460 libvlc_global_data_t *p_libvlc_global = vlc_global();
462 psz_cachedir = p_this->p_libvlc->psz_cachedir;
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 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
486 file = utf8_fopen( psz_filename, "wb" );
490 /* Empty space for file size */
491 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
494 /* Contains version number */
495 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
498 /* Sub-version number (to avoid breakage in the dev version when cache
499 * structure changes) */
500 i_file_size = CACHE_SUBVERSION_NUM;
501 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
505 if (fprintf (file, "%5.5s", _("C")) == EOF)
509 i_file_size = ftell( file );
510 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
513 i_cache = p_libvlc_global->p_module_bank->i_cache;
514 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
516 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
519 #define SAVE_IMMEDIATE( a ) \
520 if (fwrite (&a, sizeof(a), 1, file) != 1) \
522 #define SAVE_STRING( a ) \
524 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
525 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
526 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
530 for( i = 0; i < i_cache; i++ )
532 uint32_t i_submodule;
534 /* Save common info */
535 SAVE_STRING( pp_cache[i]->psz_file );
536 SAVE_IMMEDIATE( pp_cache[i]->i_time );
537 SAVE_IMMEDIATE( pp_cache[i]->i_size );
538 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
540 if( pp_cache[i]->b_junk ) continue;
542 /* Save additional infos */
543 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
544 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
545 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
546 SAVE_STRING( pp_cache[i]->p_module->psz_help );
547 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
549 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
551 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
552 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
553 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
554 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
555 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
556 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
559 if (CacheSaveConfig (pp_cache[i]->p_module, file))
562 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
564 i_submodule = pp_cache[i]->p_module->i_children;
565 SAVE_IMMEDIATE( i_submodule );
566 for( i_submodule = 0;
567 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
571 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
573 SAVE_STRING( p_module->psz_object_name );
574 SAVE_STRING( p_module->psz_shortname );
575 SAVE_STRING( p_module->psz_longname );
576 SAVE_STRING( p_module->psz_help );
577 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
579 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
581 SAVE_STRING( p_module->psz_capability );
582 SAVE_IMMEDIATE( p_module->i_score );
583 SAVE_IMMEDIATE( p_module->i_cpu );
584 SAVE_IMMEDIATE( p_module->b_unloadable );
585 SAVE_IMMEDIATE( p_module->b_reentrant );
589 /* Fill-up file size */
590 i_file_size = ftell( file );
591 fseek( file, 0, SEEK_SET );
592 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
595 if (fclose (file) == 0)
596 return; /* success! */
600 msg_Warn (p_this, "could not write plugins cache %s (%m)",
609 static int CacheSaveConfig( module_t *p_module, FILE *file )
611 uint32_t i_lines = p_module->confsize;
613 SAVE_IMMEDIATE( p_module->i_config_items );
614 SAVE_IMMEDIATE( p_module->i_bool_items );
615 SAVE_IMMEDIATE( i_lines );
617 for (size_t i = 0; i < i_lines ; i++)
619 SAVE_IMMEDIATE( p_module->p_config[i] );
621 SAVE_STRING( p_module->p_config[i].psz_type );
622 SAVE_STRING( p_module->p_config[i].psz_name );
623 SAVE_STRING( p_module->p_config[i].psz_text );
624 SAVE_STRING( p_module->p_config[i].psz_longtext );
625 SAVE_STRING( p_module->p_config[i].psz_oldname );
626 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
628 if (IsConfigStringType (p_module->p_config[i].i_type))
629 SAVE_STRING( p_module->p_config[i].orig.psz );
631 if( p_module->p_config[i].i_list )
633 if( p_module->p_config[i].ppsz_list )
635 for (int j = 0; j < p_module->p_config[i].i_list; j++)
636 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
639 if( p_module->p_config[i].ppsz_list_text )
641 for (int j = 0; j < p_module->p_config[i].i_list; j++)
642 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
644 if( p_module->p_config[i].pi_list )
646 for (int j = 0; j < p_module->p_config[i].i_list; j++)
647 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
651 for (int j = 0; j < p_module->p_config[i].i_action; j++)
652 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
654 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
662 /*****************************************************************************
663 * CacheName: Return the cache file name for this platform.
664 *****************************************************************************/
665 static char *CacheName( void )
667 static char psz_cachename[32];
669 /* Code int size, pointer size and endianness in the filename */
670 int32_t x = 0xbe00001e;
671 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
672 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
673 return psz_cachename;
676 /*****************************************************************************
677 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
678 *****************************************************************************/
679 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
684 p_cache->pf_activate = p_module->pf_activate;
685 p_cache->pf_deactivate = p_module->pf_deactivate;
686 p_cache->handle = p_module->handle;
688 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
690 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
691 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
692 p_cchild->pf_activate = p_child->pf_activate;
693 p_cchild->pf_deactivate = p_child->pf_deactivate;
696 p_cache->b_loaded = VLC_TRUE;
697 p_module->b_loaded = VLC_FALSE;
700 /*****************************************************************************
701 * CacheFind: finds the cache entry corresponding to a file
702 *****************************************************************************/
703 module_cache_t *CacheFind( const char *psz_file,
704 int64_t i_time, int64_t i_size )
706 module_cache_t **pp_cache;
708 libvlc_global_data_t *p_libvlc_global = vlc_global();
710 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
711 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
713 for( i = 0; i < i_cache; i++ )
715 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
716 pp_cache[i]->i_time == i_time &&
717 pp_cache[i]->i_size == i_size ) return pp_cache[i];
723 #endif /* HAVE_DYNAMIC_PLUGINS */