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 * );
86 * (only used to avoid breakage in dev version when cache structure changes) */
87 #define CACHE_SUBVERSION_NUM 5
89 /* Format string for the cache filename */
90 #define CACHENAME_FORMAT \
91 "plugins-%.2zx%.2zx%.2"PRIx8".dat"
92 /* Magic for the cache filename */
93 #define CACHENAME_VALUES \
94 sizeof(int), sizeof(void *), *(uint8_t *)&(uint16_t){ 0xbe1e }
97 /*****************************************************************************
98 * LoadPluginsCache: loads the plugins cache file
99 *****************************************************************************
100 * This function will load the plugin cache if present and valid. This cache
101 * will in turn be queried by AllocateAllPlugins() to see if it needs to
102 * actually load the dynamically loadable module.
103 * This allows us to only fully load plugins when they are actually used.
104 *****************************************************************************/
105 void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, bool b_delete )
107 char *psz_filename, *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
109 int i, j, i_size, i_read;
110 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
112 module_cache_t **pp_cache = 0;
113 int32_t i_file_size, i_marker;
115 if( !psz_cachedir ) /* XXX: this should never happen */
117 msg_Err( p_this, "Unable to get cache directory" );
121 if( asprintf( &psz_filename, "%s"DIR_SEP CACHENAME_FORMAT,
122 psz_cachedir, CACHENAME_VALUES ) == -1 )
124 free( psz_cachedir );
127 free( psz_cachedir );
131 #if !defined( UNDER_CE )
132 unlink( psz_filename );
134 wchar_t psz_wf[MAX_PATH];
135 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
136 DeleteFile( psz_wf );
138 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
139 free( psz_filename );
143 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
145 file = utf8_fopen( psz_filename, "rb" );
148 msg_Warn( p_this, "could not open plugins cache file %s for reading",
150 free( psz_filename );
153 free( psz_filename );
155 /* Check the file size */
156 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
157 if( i_read != sizeof(i_file_size) )
159 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
165 fseek( file, 0, SEEK_END );
166 if( ftell( file ) != i_file_size )
168 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
169 "(corrupted size)" );
173 fseek( file, sizeof(i_file_size), SEEK_SET );
175 /* Check the file is a plugins cache */
176 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
177 i_read = fread( p_cachestring, 1, i_size, file );
178 if( i_read != i_size ||
179 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
181 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
186 #ifdef DISTRO_VERSION
187 /* Check for distribution specific version */
188 char p_distrostring[sizeof( DISTRO_VERSION )];
189 i_size = sizeof( DISTRO_VERSION ) - 1;
190 i_read = fread( p_distrostring, 1, i_size, file );
191 if( i_read != i_size ||
192 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
194 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
200 /* Check Sub-version number */
201 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
202 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
204 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
205 "(corrupted header)" );
210 /* Check header marker */
211 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
212 if( i_read != sizeof(i_marker) ||
213 i_marker != ftell( file ) - (int)sizeof(i_marker) )
215 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
216 "(corrupted header)" );
221 p_bank->i_loaded_cache = 0;
222 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
224 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
225 "(file too short)" );
231 pp_cache = p_bank->pp_loaded_cache =
232 malloc( i_cache * sizeof(void *) );
234 #define LOAD_IMMEDIATE(a) \
235 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
236 #define LOAD_STRING(a) \
239 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
240 || ( i_size > 16384 ) ) \
243 char *psz = malloc( i_size ); \
244 if( fread( psz, i_size, 1, file ) != 1 ) { \
248 if( psz[i_size-1] ) { \
256 for( i = 0; i < i_cache; i++ )
261 pp_cache[i] = malloc( sizeof(module_cache_t) );
262 p_bank->i_loaded_cache++;
264 /* Load common info */
265 LOAD_STRING( pp_cache[i]->psz_file );
266 LOAD_IMMEDIATE( pp_cache[i]->i_time );
267 LOAD_IMMEDIATE( pp_cache[i]->i_size );
268 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
269 pp_cache[i]->b_used = false;
271 if( pp_cache[i]->b_junk ) continue;
273 pp_cache[i]->p_module = vlc_module_create( p_this );
275 /* Load additional infos */
276 free( pp_cache[i]->p_module->psz_object_name );
277 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
278 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
279 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
280 LOAD_STRING( pp_cache[i]->p_module->psz_help );
281 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
283 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
285 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
286 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
287 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
288 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
289 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
290 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
293 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
296 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
298 LOAD_IMMEDIATE( i_submodules );
300 while( i_submodules-- )
302 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
303 free( p_module->psz_object_name );
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_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;
357 p_module->confsize = i_lines;
359 /* Do the duplication job */
360 for (size_t i = 0; i < i_lines; i++ )
362 LOAD_IMMEDIATE( p_module->p_config[i] );
364 LOAD_STRING( p_module->p_config[i].psz_type );
365 LOAD_STRING( p_module->p_config[i].psz_name );
366 LOAD_STRING( p_module->p_config[i].psz_text );
367 LOAD_STRING( p_module->p_config[i].psz_longtext );
368 LOAD_STRING( p_module->p_config[i].psz_oldname );
369 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
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 = false;
389 p_module->p_config[i].p_lock = &p_module->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 static int CacheSaveSubmodule( FILE *file, module_t *p_module );
455 /*****************************************************************************
456 * SavePluginsCache: saves the plugins cache to a file
457 *****************************************************************************/
458 void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
460 static char const psz_tag[] =
461 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
462 "# This file is a cache directory tag created by VLC.\r\n"
463 "# For information about cache directory tags, see:\r\n"
464 "# http://www.brynosaurus.com/cachedir/\r\n";
466 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
469 module_cache_t **pp_cache;
470 uint32_t i_file_size = 0;
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 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
487 clearerr (file); /* what else can we do? */
491 snprintf( psz_filename, sizeof( psz_filename ),
492 "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
494 free( psz_cachedir );
495 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
497 char psz_tmpname[sizeof (psz_filename) + 12];
498 snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
499 (uint32_t)getpid ());
500 file = utf8_fopen( psz_tmpname, "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 i_file_size = ftell( file );
524 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
527 i_cache = p_bank->i_cache;
528 pp_cache = p_bank->pp_cache;
530 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
533 #define SAVE_IMMEDIATE( a ) \
534 if (fwrite (&a, sizeof(a), 1, file) != 1) \
536 #define SAVE_STRING( a ) \
538 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
539 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
540 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
544 for( i = 0; i < i_cache; i++ )
546 uint32_t i_submodule;
548 /* Save common info */
549 SAVE_STRING( pp_cache[i]->psz_file );
550 SAVE_IMMEDIATE( pp_cache[i]->i_time );
551 SAVE_IMMEDIATE( pp_cache[i]->i_size );
552 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
554 if( pp_cache[i]->b_junk ) continue;
556 /* Save additional infos */
557 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
558 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
559 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
560 SAVE_STRING( pp_cache[i]->p_module->psz_help );
561 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
563 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
565 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
566 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
567 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
568 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
569 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
570 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
573 if (CacheSaveConfig (pp_cache[i]->p_module, file))
576 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
578 i_submodule = pp_cache[i]->p_module->submodule_count;
579 SAVE_IMMEDIATE( i_submodule );
580 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
584 /* Fill-up file size */
585 i_file_size = ftell( file );
586 fseek( file, 0, SEEK_SET );
587 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
588 || fflush (file)) /* flush libc buffers */
592 utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
595 utf8_unlink (psz_filename);
597 utf8_rename (psz_tmpname, psz_filename);
599 return; /* success! */
602 msg_Warn (p_this, "could not write plugins cache %s (%m)",
611 static int CacheSaveSubmodule( FILE *file, module_t *p_module )
615 if( CacheSaveSubmodule( file, p_module->next ) )
618 SAVE_STRING( p_module->psz_object_name );
619 SAVE_STRING( p_module->psz_shortname );
620 SAVE_STRING( p_module->psz_longname );
621 SAVE_STRING( p_module->psz_help );
622 for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
623 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
625 SAVE_STRING( p_module->psz_capability );
626 SAVE_IMMEDIATE( p_module->i_score );
627 SAVE_IMMEDIATE( p_module->i_cpu );
628 SAVE_IMMEDIATE( p_module->b_unloadable );
629 SAVE_IMMEDIATE( p_module->b_reentrant );
637 static int CacheSaveConfig( module_t *p_module, FILE *file )
639 uint32_t i_lines = p_module->confsize;
641 SAVE_IMMEDIATE( p_module->i_config_items );
642 SAVE_IMMEDIATE( p_module->i_bool_items );
643 SAVE_IMMEDIATE( i_lines );
645 for (size_t i = 0; i < i_lines ; i++)
647 SAVE_IMMEDIATE( p_module->p_config[i] );
649 SAVE_STRING( p_module->p_config[i].psz_type );
650 SAVE_STRING( p_module->p_config[i].psz_name );
651 SAVE_STRING( p_module->p_config[i].psz_text );
652 SAVE_STRING( p_module->p_config[i].psz_longtext );
653 SAVE_STRING( p_module->p_config[i].psz_oldname );
654 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
656 if (IsConfigStringType (p_module->p_config[i].i_type))
657 SAVE_STRING( p_module->p_config[i].orig.psz );
659 if( p_module->p_config[i].i_list )
661 if( p_module->p_config[i].ppsz_list )
663 for (int j = 0; j < p_module->p_config[i].i_list; j++)
664 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
667 if( p_module->p_config[i].ppsz_list_text )
669 for (int j = 0; j < p_module->p_config[i].i_list; j++)
670 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
672 if( p_module->p_config[i].pi_list )
674 for (int j = 0; j < p_module->p_config[i].i_list; j++)
675 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
679 for (int j = 0; j < p_module->p_config[i].i_action; j++)
680 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
682 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
690 /*****************************************************************************
691 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
692 *****************************************************************************/
693 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
697 p_cache->pf_activate = p_module->pf_activate;
698 p_cache->pf_deactivate = p_module->pf_deactivate;
699 p_cache->handle = p_module->handle;
701 /* FIXME: This looks too simplistic an algorithm to me. What if the module
702 * file was altered such that the number of order of submodules was
703 * altered... after VLC started -- Courmisch, 09/2008 */
704 module_t *p_child = p_module->submodule,
705 *p_cchild = p_cache->submodule;
706 while( p_child && p_cchild )
708 p_cchild->pf_activate = p_child->pf_activate;
709 p_cchild->pf_deactivate = p_child->pf_deactivate;
710 p_child = p_child->next;
711 p_cchild = p_cchild->next;
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( module_bank_t *p_bank, const char *psz_file,
722 int64_t i_time, int64_t i_size )
724 module_cache_t **pp_cache;
727 pp_cache = p_bank->pp_loaded_cache;
728 i_cache = p_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 */