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>
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 * );
85 * (only used to avoid breakage in dev version when cache structure changes) */
86 #define CACHE_SUBVERSION_NUM 8
88 /* Format string for the cache filename */
89 #define CACHENAME_FORMAT \
90 "plugins-%.2zx%.2zx%.2"PRIx8"-%x.dat"
91 /* Magic for the cache filename */
92 #define CACHENAME_VALUES \
93 sizeof(int), sizeof(void *), *(uint8_t *)&(uint16_t){ 0xbe1e }, vlc_CPU()
96 /*****************************************************************************
97 * LoadPluginsCache: loads the plugins cache file
98 *****************************************************************************
99 * This function will load the plugin cache if present and valid. This cache
100 * will in turn be queried by AllocateAllPlugins() to see if it needs to
101 * actually load the dynamically loadable module.
102 * This allows us to only fully load plugins when they are actually used.
103 *****************************************************************************/
104 void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, bool b_delete )
106 char *psz_filename, *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
108 int i, j, i_size, i_read;
109 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
111 module_cache_t **pp_cache = 0;
112 int32_t i_file_size, i_marker;
114 if( !psz_cachedir ) /* XXX: this should never happen */
116 msg_Err( p_this, "Unable to get cache directory" );
120 if( asprintf( &psz_filename, "%s"DIR_SEP CACHENAME_FORMAT,
121 psz_cachedir, CACHENAME_VALUES ) == -1 )
123 free( psz_cachedir );
126 free( psz_cachedir );
130 #if !defined( UNDER_CE )
131 unlink( psz_filename );
133 wchar_t psz_wf[MAX_PATH];
134 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
135 DeleteFile( psz_wf );
137 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
138 free( psz_filename );
142 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
144 file = utf8_fopen( psz_filename, "rb" );
147 msg_Warn( p_this, "could not open plugins cache file %s for reading",
149 free( psz_filename );
152 free( psz_filename );
154 /* Check the file size */
155 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
156 if( i_read != sizeof(i_file_size) )
158 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
164 fseek( file, 0, SEEK_END );
165 if( ftell( file ) != i_file_size )
167 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
168 "(corrupted size)" );
172 fseek( file, sizeof(i_file_size), SEEK_SET );
174 /* Check the file is a plugins cache */
175 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
176 i_read = fread( p_cachestring, 1, i_size, file );
177 if( i_read != i_size ||
178 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
180 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
185 #ifdef DISTRO_VERSION
186 /* Check for distribution specific version */
187 char p_distrostring[sizeof( DISTRO_VERSION )];
188 i_size = sizeof( DISTRO_VERSION ) - 1;
189 i_read = fread( p_distrostring, 1, i_size, file );
190 if( i_read != i_size ||
191 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
193 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
199 /* Check Sub-version number */
200 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
201 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
203 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
204 "(corrupted header)" );
209 /* Check header marker */
210 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
211 if( i_read != sizeof(i_marker) ||
212 i_marker != ftell( file ) - (int)sizeof(i_marker) )
214 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
215 "(corrupted header)" );
220 p_bank->i_loaded_cache = 0;
221 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
223 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
224 "(file too short)" );
230 pp_cache = p_bank->pp_loaded_cache =
231 xmalloc( i_cache * sizeof(void *) );
233 #define LOAD_IMMEDIATE(a) \
234 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
235 #define LOAD_STRING(a) \
238 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
239 || ( i_size > 16384 ) ) \
242 char *psz = xmalloc( i_size ); \
243 if( fread( psz, i_size, 1, file ) != 1 ) { \
247 if( psz[i_size-1] ) { \
255 for( i = 0; i < i_cache; i++ )
260 pp_cache[i] = xmalloc( sizeof(module_cache_t) );
261 p_bank->i_loaded_cache++;
263 /* Load common info */
264 LOAD_STRING( pp_cache[i]->psz_file );
265 LOAD_IMMEDIATE( pp_cache[i]->i_time );
266 LOAD_IMMEDIATE( pp_cache[i]->i_size );
267 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
268 pp_cache[i]->b_used = false;
270 if( pp_cache[i]->b_junk ) continue;
272 pp_cache[i]->p_module = vlc_module_create( p_this );
274 /* Load additional infos */
275 free( pp_cache[i]->p_module->psz_object_name );
276 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
277 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
278 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
279 LOAD_STRING( pp_cache[i]->p_module->psz_help );
280 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
282 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
284 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
285 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
286 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
287 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
290 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
293 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
295 LOAD_IMMEDIATE( i_submodules );
297 while( i_submodules-- )
299 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
300 free( p_module->psz_object_name );
301 LOAD_STRING( p_module->psz_object_name );
302 LOAD_STRING( p_module->psz_shortname );
303 LOAD_STRING( p_module->psz_longname );
304 LOAD_STRING( p_module->psz_help );
305 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
307 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
309 LOAD_STRING( p_module->psz_capability );
310 LOAD_IMMEDIATE( p_module->i_score );
311 LOAD_IMMEDIATE( p_module->b_unloadable );
320 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
323 p_bank->i_loaded_cache = 0;
330 /* This function should never be called.
331 * It is only used as a non-NULL vlc_callback_t value for comparison. */
332 static int dummy_callback (vlc_object_t *obj, const char *name,
333 vlc_value_t oldval, vlc_value_t newval, void *data)
335 (void) obj; (void)name; (void)oldval; (void)newval; (void)data;
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;
362 p_module->confsize = i_lines;
364 /* Do the duplication job */
365 for (size_t i = 0; i < i_lines; i++ )
367 LOAD_IMMEDIATE( p_module->p_config[i] );
369 LOAD_STRING( p_module->p_config[i].psz_type );
370 LOAD_STRING( p_module->p_config[i].psz_name );
371 LOAD_STRING( p_module->p_config[i].psz_text );
372 LOAD_STRING( p_module->p_config[i].psz_longtext );
373 LOAD_STRING( p_module->p_config[i].psz_oldname );
374 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
376 if (IsConfigStringType (p_module->p_config[i].i_type))
378 LOAD_STRING (p_module->p_config[i].orig.psz);
379 p_module->p_config[i].value.psz =
380 (p_module->p_config[i].orig.psz != NULL)
381 ? strdup (p_module->p_config[i].orig.psz) : NULL;
382 p_module->p_config[i].saved.psz = NULL;
386 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
387 sizeof (p_module->p_config[i].value));
388 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
389 sizeof (p_module->p_config[i].saved));
392 p_module->p_config[i].b_dirty = false;
394 if( p_module->p_config[i].i_list )
396 if( p_module->p_config[i].ppsz_list )
399 p_module->p_config[i].ppsz_list =
400 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
401 if( p_module->p_config[i].ppsz_list )
403 for( j = 0; j < p_module->p_config[i].i_list; j++ )
404 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
405 p_module->p_config[i].ppsz_list[j] = NULL;
408 if( p_module->p_config[i].ppsz_list_text )
411 p_module->p_config[i].ppsz_list_text =
412 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
413 if( p_module->p_config[i].ppsz_list_text )
415 for( j = 0; j < p_module->p_config[i].i_list; j++ )
416 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
417 p_module->p_config[i].ppsz_list_text[j] = NULL;
420 if( p_module->p_config[i].pi_list )
422 p_module->p_config[i].pi_list =
423 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
424 if( p_module->p_config[i].pi_list )
426 for (int j = 0; j < p_module->p_config[i].i_list; j++)
427 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
432 if( p_module->p_config[i].i_action )
434 p_module->p_config[i].ppf_action =
435 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
436 p_module->p_config[i].ppsz_action_text =
437 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
439 for (int j = 0; j < p_module->p_config[i].i_action; j++)
441 p_module->p_config[i].ppf_action[j] = NULL;
442 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
447 LOAD_IMMEDIATE( has_callback );
449 p_module->p_config[i].pf_callback = dummy_callback;
459 static int CacheSaveBank( FILE *file, module_bank_t *p_bank );
461 /*****************************************************************************
462 * SavePluginsCache: saves the plugins cache to a file
463 *****************************************************************************/
464 void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
466 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
467 if( !psz_cachedir ) /* XXX: this should never happen */
469 msg_Err( p_this, "unable to get cache directory" );
473 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
474 config_CreateDir( p_this, psz_cachedir );
476 snprintf( psz_filename, sizeof( psz_filename ),
477 "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
479 free( psz_cachedir );
481 char psz_tmpname[sizeof (psz_filename) + 12];
482 snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
483 (uint32_t)getpid ());
484 FILE *file = utf8_fopen( psz_tmpname, "wb" );
488 msg_Dbg (p_this, "writing plugins cache %s", psz_filename);
489 if (CacheSaveBank (file, p_bank))
493 utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
496 utf8_unlink (psz_filename);
498 utf8_rename (psz_tmpname, psz_filename);
500 return; /* success! */
503 msg_Warn (p_this, "could not write plugins cache %s (%m)",
512 static int CacheSaveConfig (FILE *, module_t *);
513 static int CacheSaveSubmodule (FILE *, module_t *);
515 static int CacheSaveBank (FILE *file, module_bank_t *p_bank)
517 uint32_t i_file_size = 0;
519 /* Empty space for file size */
520 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
523 /* Contains version number */
524 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
526 #ifdef DISTRO_VERSION
527 /* Allow binary maintaner to pass a string to detect new binary version*/
528 if (fputs( DISTRO_VERSION, file ) == EOF)
531 /* Sub-version number (to avoid breakage in the dev version when cache
532 * structure changes) */
533 i_file_size = CACHE_SUBVERSION_NUM;
534 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
538 i_file_size = ftell( file );
539 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
542 module_cache_t **pp_cache = p_bank->pp_cache;
543 uint32_t i_cache = p_bank->i_cache;
545 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
548 #define SAVE_IMMEDIATE( a ) \
549 if (fwrite (&a, sizeof(a), 1, file) != 1) \
551 #define SAVE_STRING( a ) \
553 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
554 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
555 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
559 for (unsigned i = 0; i < i_cache; i++)
561 uint32_t i_submodule;
563 /* Save common info */
564 SAVE_STRING( pp_cache[i]->psz_file );
565 SAVE_IMMEDIATE( pp_cache[i]->i_time );
566 SAVE_IMMEDIATE( pp_cache[i]->i_size );
567 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
569 if( pp_cache[i]->b_junk ) continue;
571 /* Save additional infos */
572 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
573 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
574 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
575 SAVE_STRING( pp_cache[i]->p_module->psz_help );
576 for (unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++)
578 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
580 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
581 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
582 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
583 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
586 if (CacheSaveConfig (file, pp_cache[i]->p_module))
589 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
591 i_submodule = pp_cache[i]->p_module->submodule_count;
592 SAVE_IMMEDIATE( i_submodule );
593 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
597 /* Fill-up file size */
598 i_file_size = ftell( file );
599 fseek( file, 0, SEEK_SET );
600 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
601 || fflush (file)) /* flush libc buffers */
603 return 0; /* success! */
609 static int CacheSaveSubmodule( FILE *file, module_t *p_module )
613 if( CacheSaveSubmodule( file, p_module->next ) )
616 SAVE_STRING( p_module->psz_object_name );
617 SAVE_STRING( p_module->psz_shortname );
618 SAVE_STRING( p_module->psz_longname );
619 SAVE_STRING( p_module->psz_help );
620 for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
621 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
623 SAVE_STRING( p_module->psz_capability );
624 SAVE_IMMEDIATE( p_module->i_score );
625 SAVE_IMMEDIATE( p_module->b_unloadable );
633 static int CacheSaveConfig (FILE *file, module_t *p_module)
635 uint32_t i_lines = p_module->confsize;
637 SAVE_IMMEDIATE( p_module->i_config_items );
638 SAVE_IMMEDIATE( p_module->i_bool_items );
639 SAVE_IMMEDIATE( i_lines );
641 for (size_t i = 0; i < i_lines ; i++)
643 SAVE_IMMEDIATE( p_module->p_config[i] );
645 SAVE_STRING( p_module->p_config[i].psz_type );
646 SAVE_STRING( p_module->p_config[i].psz_name );
647 SAVE_STRING( p_module->p_config[i].psz_text );
648 SAVE_STRING( p_module->p_config[i].psz_longtext );
649 SAVE_STRING( p_module->p_config[i].psz_oldname );
650 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
652 if (IsConfigStringType (p_module->p_config[i].i_type))
653 SAVE_STRING( p_module->p_config[i].orig.psz );
655 if( p_module->p_config[i].i_list )
657 if( p_module->p_config[i].ppsz_list )
659 for (int j = 0; j < p_module->p_config[i].i_list; j++)
660 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
663 if( p_module->p_config[i].ppsz_list_text )
665 for (int j = 0; j < p_module->p_config[i].i_list; j++)
666 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
668 if( p_module->p_config[i].pi_list )
670 for (int j = 0; j < p_module->p_config[i].i_list; j++)
671 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
675 for (int j = 0; j < p_module->p_config[i].i_action; j++)
676 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
678 bool has_callback = p_module->p_config[i].pf_callback != NULL;
679 SAVE_IMMEDIATE( has_callback );
687 /*****************************************************************************
688 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
689 *****************************************************************************/
690 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
694 p_cache->pf_activate = p_module->pf_activate;
695 p_cache->pf_deactivate = p_module->pf_deactivate;
696 p_cache->handle = p_module->handle;
698 /* FIXME: This looks too simplistic an algorithm to me. What if the module
699 * file was altered such that the number of order of submodules was
700 * altered... after VLC started -- Courmisch, 09/2008 */
701 module_t *p_child = p_module->submodule,
702 *p_cchild = p_cache->submodule;
703 while( p_child && p_cchild )
705 p_cchild->pf_activate = p_child->pf_activate;
706 p_cchild->pf_deactivate = p_child->pf_deactivate;
707 p_child = p_child->next;
708 p_cchild = p_cchild->next;
711 p_cache->b_loaded = true;
712 p_module->b_loaded = false;
715 /*****************************************************************************
716 * CacheFind: finds the cache entry corresponding to a file
717 *****************************************************************************/
718 module_cache_t *CacheFind( module_bank_t *p_bank, const char *psz_file,
719 int64_t i_time, int64_t i_size )
721 module_cache_t **pp_cache;
724 pp_cache = p_bank->pp_loaded_cache;
725 i_cache = p_bank->i_loaded_cache;
727 for( i = 0; i < i_cache; i++ )
729 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
730 pp_cache[i]->i_time == i_time &&
731 pp_cache[i]->i_size == i_size ) return pp_cache[i];
737 #endif /* HAVE_DYNAMIC_PLUGINS */