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 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
31 * is set to 64. Don't try to be cleverer. */
32 #ifdef _FILE_OFFSET_BITS
33 #undef _FILE_OFFSET_BITS
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* sprintf() */
38 #include <string.h> /* strdup() */
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #if !defined(HAVE_DYNAMIC_PLUGINS)
55 /* no support for plugins */
56 #elif defined(HAVE_DL_DYLD)
57 # if defined(HAVE_MACH_O_DYLD_H)
58 # include <mach-o/dyld.h>
60 #elif defined(HAVE_DL_BEOS)
61 # if defined(HAVE_IMAGE_H)
64 #elif defined(HAVE_DL_WINDOWS)
66 #elif defined(HAVE_DL_DLOPEN)
67 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
70 # if defined(HAVE_SYS_DL_H)
73 #elif defined(HAVE_DL_SHL_LOAD)
74 # if defined(HAVE_DL_H)
79 #include "config/config.h"
81 #include "vlc_charset.h"
83 #include "modules/modules.h"
84 #include "modules/builtin.h"
87 /*****************************************************************************
89 *****************************************************************************/
90 #ifdef HAVE_DYNAMIC_PLUGINS
91 static int CacheLoadConfig ( module_t *, FILE * );
92 static void CacheSaveConfig ( module_t *, FILE * );
93 static char * CacheName ( void );
96 * (only used to avoid breakage in dev version when cache structure changes) */
97 #define CACHE_SUBVERSION_NUM 3
99 /*****************************************************************************
100 * LoadPluginsCache: loads the plugins cache file
101 *****************************************************************************
102 * This function will load the plugin cache if present and valid. This cache
103 * will in turn be queried by AllocateAllPlugins() to see if it needs to
104 * actually load the dynamically loadable module.
105 * This allows us to only fully load plugins when they are actually used.
106 *****************************************************************************/
107 void CacheLoad( vlc_object_t *p_this )
109 char *psz_filename, *psz_cachedir;
111 int i, j, i_size, i_read;
112 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
113 char p_cachelang[6], p_lang[6];
115 module_cache_t **pp_cache = 0;
116 int32_t i_file_size, i_marker;
117 libvlc_global_data_t *p_libvlc_global = vlc_global();
119 psz_cachedir = p_this->p_libvlc->psz_cachedir;
120 if( !psz_cachedir ) /* XXX: this should never happen */
122 msg_Err( p_this, "Unable to get cache directory" );
126 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s",
127 psz_cachedir, CacheName() );
130 msg_Err( p_this, "out of memory" );
134 if( p_libvlc_global->p_module_bank->b_cache_delete )
136 #if !defined( UNDER_CE )
137 unlink( psz_filename );
139 wchar_t psz_wf[MAX_PATH];
140 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
141 DeleteFile( psz_wf );
143 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
144 free( psz_filename );
148 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
150 file = utf8_fopen( psz_filename, "rb" );
153 msg_Warn( p_this, "could not open plugins cache file %s for reading",
155 free( psz_filename );
158 free( psz_filename );
160 /* Check the file size */
161 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
162 if( i_read != sizeof(i_file_size) )
164 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
170 fseek( file, 0, SEEK_END );
171 if( ftell( file ) != i_file_size )
173 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
174 "(corrupted size)" );
178 fseek( file, sizeof(i_file_size), SEEK_SET );
180 /* Check the file is a plugins cache */
181 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
182 i_read = fread( p_cachestring, 1, i_size, file );
183 if( i_read != i_size ||
184 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
186 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
191 /* Check Sub-version number */
192 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
193 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
195 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
196 "(corrupted header)" );
201 /* Check the language hasn't changed */
202 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
203 i_read = fread( p_cachelang, 1, i_size, file );
204 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
206 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
207 "(language changed)" );
212 /* Check header marker */
213 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
214 if( i_read != sizeof(i_marker) ||
215 i_marker != ftell( file ) - (int)sizeof(i_marker) )
217 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
218 "(corrupted header)" );
223 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
224 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
226 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
227 "(file too short)" );
233 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
234 malloc( i_cache * sizeof(void *) );
236 #define LOAD_IMMEDIATE(a) \
237 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
238 #define LOAD_STRING(a) \
241 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
242 || ( i_size > 16384 ) ) \
245 char *psz = malloc( i_size ); \
246 if( fread( psz, i_size, 1, file ) != 1 ) { \
250 if( psz[i_size-1] ) { \
258 for( i = 0; i < i_cache; i++ )
263 pp_cache[i] = malloc( sizeof(module_cache_t) );
264 p_libvlc_global->p_module_bank->i_loaded_cache++;
266 /* Load common info */
267 LOAD_STRING( pp_cache[i]->psz_file );
268 LOAD_IMMEDIATE( pp_cache[i]->i_time );
269 LOAD_IMMEDIATE( pp_cache[i]->i_size );
270 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
271 pp_cache[i]->b_used = VLC_FALSE;
273 if( pp_cache[i]->b_junk ) continue;
275 pp_cache[i]->p_module = vlc_module_create( p_this );
277 /* Load additional infos */
278 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
279 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
280 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
281 LOAD_STRING( pp_cache[i]->p_module->psz_help );
282 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
284 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
286 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
287 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
288 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
289 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
290 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
291 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
294 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
297 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
299 LOAD_IMMEDIATE( i_submodules );
301 while( i_submodules-- )
303 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
304 LOAD_STRING( p_module->psz_object_name );
305 LOAD_STRING( p_module->psz_shortname );
306 LOAD_STRING( p_module->psz_longname );
307 LOAD_STRING( p_module->psz_help );
308 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
310 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
312 LOAD_STRING( p_module->psz_capability );
313 LOAD_IMMEDIATE( p_module->i_score );
314 LOAD_IMMEDIATE( p_module->i_cpu );
315 LOAD_IMMEDIATE( p_module->b_unloadable );
316 LOAD_IMMEDIATE( p_module->b_reentrant );
325 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
328 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
335 static int CacheLoadConfig( module_t *p_module, FILE *file )
340 /* Calculate the structure length */
341 LOAD_IMMEDIATE( p_module->i_config_items );
342 LOAD_IMMEDIATE( p_module->i_bool_items );
344 LOAD_IMMEDIATE( i_lines );
346 /* Allocate memory */
350 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
351 if( p_module->p_config == NULL )
353 p_module->confsize = 0;
354 msg_Err( p_module, "config error: can't duplicate p_config" );
358 p_module->confsize = i_lines;
360 /* Do the duplication job */
361 for (size_t i = 0; i < i_lines; i++ )
363 LOAD_IMMEDIATE( p_module->p_config[i] );
365 LOAD_STRING( p_module->p_config[i].psz_type );
366 LOAD_STRING( p_module->p_config[i].psz_name );
367 LOAD_STRING( p_module->p_config[i].psz_text );
368 LOAD_STRING( p_module->p_config[i].psz_longtext );
369 LOAD_STRING( p_module->p_config[i].psz_current );
371 if (IsConfigStringType (p_module->p_config[i].i_type))
373 LOAD_STRING (p_module->p_config[i].orig.psz);
374 p_module->p_config[i].value.psz =
375 (p_module->p_config[i].orig.psz != NULL)
376 ? strdup (p_module->p_config[i].orig.psz) : NULL;
377 p_module->p_config[i].saved.psz = NULL;
381 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
382 sizeof (p_module->p_config[i].value));
383 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
384 sizeof (p_module->p_config[i].saved));
387 p_module->p_config[i].b_dirty = VLC_FALSE;
389 p_module->p_config[i].p_lock = &p_module->object_lock;
391 if( p_module->p_config[i].i_list )
393 if( p_module->p_config[i].ppsz_list )
396 p_module->p_config[i].ppsz_list =
397 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
398 if( p_module->p_config[i].ppsz_list )
400 for( j = 0; j < p_module->p_config[i].i_list; j++ )
401 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
402 p_module->p_config[i].ppsz_list[j] = NULL;
405 if( p_module->p_config[i].ppsz_list_text )
408 p_module->p_config[i].ppsz_list_text =
409 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
410 if( p_module->p_config[i].ppsz_list_text )
412 for( j = 0; j < p_module->p_config[i].i_list; j++ )
413 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
414 p_module->p_config[i].ppsz_list_text[j] = NULL;
417 if( p_module->p_config[i].pi_list )
419 p_module->p_config[i].pi_list =
420 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
421 if( p_module->p_config[i].pi_list )
423 for (int j = 0; j < p_module->p_config[i].i_list; j++)
424 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
429 if( p_module->p_config[i].i_action )
431 p_module->p_config[i].ppf_action =
432 malloc( p_module->p_config[i].i_action * sizeof(void *) );
433 p_module->p_config[i].ppsz_action_text =
434 malloc( p_module->p_config[i].i_action * sizeof(char *) );
436 for (int j = 0; j < p_module->p_config[i].i_action; j++)
438 p_module->p_config[i].ppf_action[j] = 0;
439 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
443 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
453 /*****************************************************************************
454 * SavePluginsCache: saves the plugins cache to a file
455 *****************************************************************************/
456 void CacheSave( vlc_object_t *p_this )
458 static char const psz_tag[] =
459 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
460 "# This file is a cache directory tag created by VLC.\r\n"
461 "# For information about cache directory tags, see:\r\n"
462 "# http://www.brynosaurus.com/cachedir/\r\n";
467 module_cache_t **pp_cache;
468 int32_t i_file_size = 0;
469 libvlc_global_data_t *p_libvlc_global = vlc_global();
471 psz_cachedir = p_this->p_libvlc->psz_cachedir;
472 if( !psz_cachedir ) /* XXX: this should never happen */
474 msg_Err( p_this, "Unable to get cache directory" );
478 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
479 config_CreateDir( p_this, psz_cachedir );
481 snprintf( psz_filename, sizeof( psz_filename ),
482 "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
483 file = utf8_fopen( psz_filename, "wb" );
486 fwrite( psz_tag, 1, strlen(psz_tag), file );
490 snprintf( psz_filename, sizeof( psz_filename ),
491 "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
492 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
494 file = utf8_fopen( psz_filename, "wb" );
497 msg_Warn( p_this, "could not open plugins cache file %s for writing",
502 /* Empty space for file size */
503 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
505 /* Contains version number */
506 fprintf( file, "%s", "cache " COPYRIGHT_MESSAGE );
508 /* Sub-version number (to avoid breakage in the dev version when cache
509 * structure changes) */
510 i_file_size = CACHE_SUBVERSION_NUM;
511 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
514 fprintf( file, "%5.5s", _("C") );
517 i_file_size = ftell( file );
518 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
520 i_cache = p_libvlc_global->p_module_bank->i_cache;
521 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
523 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
525 #define SAVE_IMMEDIATE(a) \
526 fwrite( &a, sizeof(char), sizeof(a), file )
527 #define SAVE_STRING(a) \
528 { i_size = a ? strlen( a ) + 1 : 0; \
529 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
530 if( a ) fwrite( a, sizeof(char), i_size, file ); \
533 for( i = 0; i < i_cache; i++ )
536 uint32_t i_submodule;
538 /* Save common info */
539 SAVE_STRING( pp_cache[i]->psz_file );
540 SAVE_IMMEDIATE( pp_cache[i]->i_time );
541 SAVE_IMMEDIATE( pp_cache[i]->i_size );
542 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
544 if( pp_cache[i]->b_junk ) continue;
546 /* Save additional infos */
547 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
548 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
549 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
550 SAVE_STRING( pp_cache[i]->p_module->psz_help );
551 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
553 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
555 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
556 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
557 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
558 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
559 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
560 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
563 CacheSaveConfig( pp_cache[i]->p_module, file );
565 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
567 i_submodule = pp_cache[i]->p_module->i_children;
568 SAVE_IMMEDIATE( i_submodule );
569 for( i_submodule = 0;
570 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
574 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
576 SAVE_STRING( p_module->psz_object_name );
577 SAVE_STRING( p_module->psz_shortname );
578 SAVE_STRING( p_module->psz_longname );
579 SAVE_STRING( p_module->psz_help );
580 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
582 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
584 SAVE_STRING( p_module->psz_capability );
585 SAVE_IMMEDIATE( p_module->i_score );
586 SAVE_IMMEDIATE( p_module->i_cpu );
587 SAVE_IMMEDIATE( p_module->b_unloadable );
588 SAVE_IMMEDIATE( p_module->b_reentrant );
592 /* Fill-up file size */
593 i_file_size = ftell( file );
594 fseek( file, 0, SEEK_SET );
595 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
600 static void CacheSaveConfig( module_t *p_module, FILE *file )
602 uint32_t i_lines = p_module->confsize;
605 SAVE_IMMEDIATE( p_module->i_config_items );
606 SAVE_IMMEDIATE( p_module->i_bool_items );
607 SAVE_IMMEDIATE( i_lines );
609 for (size_t i = 0; i < i_lines ; i++)
611 SAVE_IMMEDIATE( p_module->p_config[i] );
613 SAVE_STRING( p_module->p_config[i].psz_type );
614 SAVE_STRING( p_module->p_config[i].psz_name );
615 SAVE_STRING( p_module->p_config[i].psz_text );
616 SAVE_STRING( p_module->p_config[i].psz_longtext );
617 SAVE_STRING( p_module->p_config[i].psz_current );
618 if (IsConfigStringType (p_module->p_config[i].i_type))
619 SAVE_STRING( p_module->p_config[i].orig.psz );
621 if( p_module->p_config[i].i_list )
623 if( p_module->p_config[i].ppsz_list )
625 for (int j = 0; j < p_module->p_config[i].i_list; j++)
626 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
629 if( p_module->p_config[i].ppsz_list_text )
631 for (int j = 0; j < p_module->p_config[i].i_list; j++)
632 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
634 if( p_module->p_config[i].pi_list )
636 for (int j = 0; j < p_module->p_config[i].i_list; j++)
637 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
641 for (int j = 0; j < p_module->p_config[i].i_action; j++)
642 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
644 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
648 /*****************************************************************************
649 * CacheName: Return the cache file name for this platform.
650 *****************************************************************************/
651 static char *CacheName( void )
653 static char psz_cachename[32];
655 /* Code int size, pointer size and endianness in the filename */
656 int32_t x = 0xbe00001e;
657 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
658 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
659 return psz_cachename;
662 /*****************************************************************************
663 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
664 *****************************************************************************/
665 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
670 p_cache->pf_activate = p_module->pf_activate;
671 p_cache->pf_deactivate = p_module->pf_deactivate;
672 p_cache->handle = p_module->handle;
674 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
676 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
677 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
678 p_cchild->pf_activate = p_child->pf_activate;
679 p_cchild->pf_deactivate = p_child->pf_deactivate;
682 p_cache->b_loaded = VLC_TRUE;
683 p_module->b_loaded = VLC_FALSE;
686 /*****************************************************************************
687 * CacheFind: finds the cache entry corresponding to a file
688 *****************************************************************************/
689 module_cache_t *CacheFind( vlc_object_t *p_this, const char *psz_file,
690 int64_t i_time, int64_t i_size )
692 module_cache_t **pp_cache;
694 libvlc_global_data_t *p_libvlc_global = vlc_global();
696 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
697 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
699 for( i = 0; i < i_cache; i++ )
701 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
702 pp_cache[i]->i_time == i_time &&
703 pp_cache[i]->i_size == i_size ) return pp_cache[i];
709 #endif /* HAVE_DYNAMIC_PLUGINS */