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 *****************************************************************************/
31 #include <vlc_common.h>
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;
109 if( !psz_cachedir ) /* XXX: this should never happen */
111 msg_Err( p_this, "Unable to get cache directory" );
115 if( asprintf( &psz_filename, "%s"DIR_SEP"%s",
116 psz_cachedir, CacheName() ) == -1 )
118 free( psz_cachedir );
121 free( psz_cachedir );
123 if( p_module_bank->b_cache_delete )
125 #if !defined( UNDER_CE )
126 unlink( psz_filename );
128 wchar_t psz_wf[MAX_PATH];
129 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
130 DeleteFile( psz_wf );
132 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
133 free( psz_filename );
137 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
139 file = utf8_fopen( psz_filename, "rb" );
142 msg_Warn( p_this, "could not open plugins cache file %s for reading",
144 free( psz_filename );
147 free( psz_filename );
149 /* Check the file size */
150 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
151 if( i_read != sizeof(i_file_size) )
153 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
159 fseek( file, 0, SEEK_END );
160 if( ftell( file ) != i_file_size )
162 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
163 "(corrupted size)" );
167 fseek( file, sizeof(i_file_size), SEEK_SET );
169 /* Check the file is a plugins cache */
170 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
171 i_read = fread( p_cachestring, 1, i_size, file );
172 if( i_read != i_size ||
173 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
175 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
180 #ifdef DISTRO_VERSION
181 /* Check for distribution specific version */
182 char p_distrostring[sizeof( DISTRO_VERSION )];
183 i_size = sizeof( DISTRO_VERSION ) - 1;
184 i_read = fread( p_distrostring, 1, i_size, file );
185 if( i_read != i_size ||
186 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
188 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
194 /* Check Sub-version number */
195 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
196 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
198 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
199 "(corrupted header)" );
204 /* Check the language hasn't changed */
205 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
206 i_read = fread( p_cachelang, 1, i_size, file );
207 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
209 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
210 "(language changed)" );
215 /* Check header marker */
216 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
217 if( i_read != sizeof(i_marker) ||
218 i_marker != ftell( file ) - (int)sizeof(i_marker) )
220 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
221 "(corrupted header)" );
226 p_module_bank->i_loaded_cache = 0;
227 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
229 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
230 "(file too short)" );
236 pp_cache = p_module_bank->pp_loaded_cache =
237 malloc( i_cache * sizeof(void *) );
239 #define LOAD_IMMEDIATE(a) \
240 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
241 #define LOAD_STRING(a) \
244 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
245 || ( i_size > 16384 ) ) \
248 char *psz = malloc( i_size ); \
249 if( fread( psz, i_size, 1, file ) != 1 ) { \
253 if( psz[i_size-1] ) { \
261 for( i = 0; i < i_cache; i++ )
266 pp_cache[i] = malloc( sizeof(module_cache_t) );
267 p_module_bank->i_loaded_cache++;
269 /* Load common info */
270 LOAD_STRING( pp_cache[i]->psz_file );
271 LOAD_IMMEDIATE( pp_cache[i]->i_time );
272 LOAD_IMMEDIATE( pp_cache[i]->i_size );
273 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
274 pp_cache[i]->b_used = false;
276 if( pp_cache[i]->b_junk ) continue;
278 pp_cache[i]->p_module = vlc_module_create( p_this );
280 /* Load additional infos */
281 free( pp_cache[i]->p_module->psz_object_name );
282 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
283 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
284 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
285 LOAD_STRING( pp_cache[i]->p_module->psz_help );
286 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
288 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
290 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
291 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
292 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
293 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
294 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
295 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
298 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
301 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
303 LOAD_IMMEDIATE( i_submodules );
305 while( i_submodules-- )
307 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
308 free( p_module->psz_object_name );
309 LOAD_STRING( p_module->psz_object_name );
310 LOAD_STRING( p_module->psz_shortname );
311 LOAD_STRING( p_module->psz_longname );
312 LOAD_STRING( p_module->psz_help );
313 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
315 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
317 LOAD_STRING( p_module->psz_capability );
318 LOAD_IMMEDIATE( p_module->i_score );
319 LOAD_IMMEDIATE( p_module->i_cpu );
320 LOAD_IMMEDIATE( p_module->b_unloadable );
321 LOAD_IMMEDIATE( p_module->b_reentrant );
330 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
333 p_module_bank->i_loaded_cache = 0;
340 static int CacheLoadConfig( module_t *p_module, FILE *file )
345 /* Calculate the structure length */
346 LOAD_IMMEDIATE( p_module->i_config_items );
347 LOAD_IMMEDIATE( p_module->i_bool_items );
349 LOAD_IMMEDIATE( i_lines );
351 /* Allocate memory */
355 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
356 if( p_module->p_config == NULL )
358 p_module->confsize = 0;
359 msg_Err( p_module, "config error: can't duplicate p_config" );
363 p_module->confsize = i_lines;
365 /* Do the duplication job */
366 for (size_t i = 0; i < i_lines; i++ )
368 LOAD_IMMEDIATE( p_module->p_config[i] );
370 LOAD_STRING( p_module->p_config[i].psz_type );
371 LOAD_STRING( p_module->p_config[i].psz_name );
372 LOAD_STRING( p_module->p_config[i].psz_text );
373 LOAD_STRING( p_module->p_config[i].psz_longtext );
374 LOAD_STRING( p_module->p_config[i].psz_oldname );
375 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
377 if (IsConfigStringType (p_module->p_config[i].i_type))
379 LOAD_STRING (p_module->p_config[i].orig.psz);
380 p_module->p_config[i].value.psz =
381 (p_module->p_config[i].orig.psz != NULL)
382 ? strdup (p_module->p_config[i].orig.psz) : NULL;
383 p_module->p_config[i].saved.psz = NULL;
387 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
388 sizeof (p_module->p_config[i].value));
389 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
390 sizeof (p_module->p_config[i].saved));
393 p_module->p_config[i].b_dirty = false;
395 p_module->p_config[i].p_lock = &(vlc_internals(p_module)->lock);
397 if( p_module->p_config[i].i_list )
399 if( p_module->p_config[i].ppsz_list )
402 p_module->p_config[i].ppsz_list =
403 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
404 if( p_module->p_config[i].ppsz_list )
406 for( j = 0; j < p_module->p_config[i].i_list; j++ )
407 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
408 p_module->p_config[i].ppsz_list[j] = NULL;
411 if( p_module->p_config[i].ppsz_list_text )
414 p_module->p_config[i].ppsz_list_text =
415 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
416 if( p_module->p_config[i].ppsz_list_text )
418 for( j = 0; j < p_module->p_config[i].i_list; j++ )
419 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
420 p_module->p_config[i].ppsz_list_text[j] = NULL;
423 if( p_module->p_config[i].pi_list )
425 p_module->p_config[i].pi_list =
426 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
427 if( p_module->p_config[i].pi_list )
429 for (int j = 0; j < p_module->p_config[i].i_list; j++)
430 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
435 if( p_module->p_config[i].i_action )
437 p_module->p_config[i].ppf_action =
438 malloc( p_module->p_config[i].i_action * sizeof(void *) );
439 p_module->p_config[i].ppsz_action_text =
440 malloc( p_module->p_config[i].i_action * sizeof(char *) );
442 for (int j = 0; j < p_module->p_config[i].i_action; j++)
444 p_module->p_config[i].ppf_action[j] = 0;
445 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
449 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
459 /*****************************************************************************
460 * SavePluginsCache: saves the plugins cache to a file
461 *****************************************************************************/
462 void CacheSave( vlc_object_t *p_this )
464 static char const psz_tag[] =
465 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
466 "# This file is a cache directory tag created by VLC.\r\n"
467 "# For information about cache directory tags, see:\r\n"
468 "# http://www.brynosaurus.com/cachedir/\r\n";
470 char *psz_cachedir = config_GetCacheDir();
473 module_cache_t **pp_cache;
474 uint32_t i_file_size = 0;
476 if( !psz_cachedir ) /* XXX: this should never happen */
478 msg_Err( p_this, "unable to get cache directory" );
482 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
483 config_CreateDir( p_this, psz_cachedir );
485 snprintf( psz_filename, sizeof( psz_filename ),
486 "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
487 file = utf8_fopen( psz_filename, "wb" );
490 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
491 clearerr (file); /* what else can we do? */
495 snprintf( psz_filename, sizeof( psz_filename ),
496 "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
497 free( psz_cachedir );
498 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
500 file = utf8_fopen( psz_filename, "wb" );
504 /* Empty space for file size */
505 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
508 /* Contains version number */
509 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
511 #ifdef DISTRO_VERSION
512 /* Allow binary maintaner to pass a string to detect new binary version*/
513 if (fputs( DISTRO_VERSION, file ) == EOF)
516 /* Sub-version number (to avoid breakage in the dev version when cache
517 * structure changes) */
518 i_file_size = CACHE_SUBVERSION_NUM;
519 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
523 if (fprintf (file, "%5.5s", _("C")) == EOF)
527 i_file_size = ftell( file );
528 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
531 i_cache = p_module_bank->i_cache;
532 pp_cache = p_module_bank->pp_cache;
534 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
537 #define SAVE_IMMEDIATE( a ) \
538 if (fwrite (&a, sizeof(a), 1, file) != 1) \
540 #define SAVE_STRING( a ) \
542 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
543 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
544 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
548 for( i = 0; i < i_cache; i++ )
550 uint32_t i_submodule;
552 /* Save common info */
553 SAVE_STRING( pp_cache[i]->psz_file );
554 SAVE_IMMEDIATE( pp_cache[i]->i_time );
555 SAVE_IMMEDIATE( pp_cache[i]->i_size );
556 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
558 if( pp_cache[i]->b_junk ) continue;
560 /* Save additional infos */
561 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
562 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
563 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
564 SAVE_STRING( pp_cache[i]->p_module->psz_help );
565 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
567 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
569 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
570 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
571 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
572 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
573 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
574 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
577 if (CacheSaveConfig (pp_cache[i]->p_module, file))
580 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
582 i_submodule = vlc_internals( pp_cache[i]->p_module )->i_children;
583 SAVE_IMMEDIATE( i_submodule );
584 for( i_submodule = 0;
585 i_submodule < (unsigned)vlc_internals( pp_cache[i]->p_module)->i_children;
589 (module_t *)vlc_internals( pp_cache[i]->p_module )->pp_children[i_submodule];
591 SAVE_STRING( p_module->psz_object_name );
592 SAVE_STRING( p_module->psz_shortname );
593 SAVE_STRING( p_module->psz_longname );
594 SAVE_STRING( p_module->psz_help );
595 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
597 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
599 SAVE_STRING( p_module->psz_capability );
600 SAVE_IMMEDIATE( p_module->i_score );
601 SAVE_IMMEDIATE( p_module->i_cpu );
602 SAVE_IMMEDIATE( p_module->b_unloadable );
603 SAVE_IMMEDIATE( p_module->b_reentrant );
607 /* Fill-up file size */
608 i_file_size = ftell( file );
609 fseek( file, 0, SEEK_SET );
610 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
613 if (fclose (file) == 0)
614 return; /* success! */
618 msg_Warn (p_this, "could not write plugins cache %s (%m)",
627 static int CacheSaveConfig( module_t *p_module, FILE *file )
629 uint32_t i_lines = p_module->confsize;
631 SAVE_IMMEDIATE( p_module->i_config_items );
632 SAVE_IMMEDIATE( p_module->i_bool_items );
633 SAVE_IMMEDIATE( i_lines );
635 for (size_t i = 0; i < i_lines ; i++)
637 SAVE_IMMEDIATE( p_module->p_config[i] );
639 SAVE_STRING( p_module->p_config[i].psz_type );
640 SAVE_STRING( p_module->p_config[i].psz_name );
641 SAVE_STRING( p_module->p_config[i].psz_text );
642 SAVE_STRING( p_module->p_config[i].psz_longtext );
643 SAVE_STRING( p_module->p_config[i].psz_oldname );
644 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
646 if (IsConfigStringType (p_module->p_config[i].i_type))
647 SAVE_STRING( p_module->p_config[i].orig.psz );
649 if( p_module->p_config[i].i_list )
651 if( p_module->p_config[i].ppsz_list )
653 for (int j = 0; j < p_module->p_config[i].i_list; j++)
654 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
657 if( p_module->p_config[i].ppsz_list_text )
659 for (int j = 0; j < p_module->p_config[i].i_list; j++)
660 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
662 if( p_module->p_config[i].pi_list )
664 for (int j = 0; j < p_module->p_config[i].i_list; j++)
665 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
669 for (int j = 0; j < p_module->p_config[i].i_action; j++)
670 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
672 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
680 /*****************************************************************************
681 * CacheName: Return the cache file name for this platform.
682 *****************************************************************************/
683 static char *CacheName( void )
685 static char psz_cachename[32];
687 /* Code int size, pointer size and endianness in the filename */
688 int32_t x = 0xbe00001e;
689 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
690 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
691 return psz_cachename;
694 /*****************************************************************************
695 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
696 *****************************************************************************/
697 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
702 p_cache->pf_activate = p_module->pf_activate;
703 p_cache->pf_deactivate = p_module->pf_deactivate;
704 p_cache->handle = p_module->handle;
706 for( i_submodule = 0; i_submodule < vlc_internals( p_module )->i_children; i_submodule++ )
708 module_t *p_child = (module_t*)vlc_internals( p_module )->pp_children[i_submodule];
709 module_t *p_cchild = (module_t*)vlc_internals( p_cache )->pp_children[i_submodule];
710 p_cchild->pf_activate = p_child->pf_activate;
711 p_cchild->pf_deactivate = p_child->pf_deactivate;
714 p_cache->b_loaded = true;
715 p_module->b_loaded = false;
718 /*****************************************************************************
719 * CacheFind: finds the cache entry corresponding to a file
720 *****************************************************************************/
721 module_cache_t *CacheFind( const char *psz_file,
722 int64_t i_time, int64_t i_size )
724 module_cache_t **pp_cache;
727 pp_cache = p_module_bank->pp_loaded_cache;
728 i_cache = p_module_bank->i_loaded_cache;
730 for( i = 0; i < i_cache; i++ )
732 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
733 pp_cache[i]->i_time == i_time &&
734 pp_cache[i]->i_size == i_size ) return pp_cache[i];
740 #endif /* HAVE_DYNAMIC_PLUGINS */