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>
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 * );
85 * (only used to avoid breakage in dev version when cache structure changes) */
86 #define CACHE_SUBVERSION_NUM 7
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 static int CacheLoadConfig( module_t *p_module, FILE *file )
335 /* Calculate the structure length */
336 LOAD_IMMEDIATE( p_module->i_config_items );
337 LOAD_IMMEDIATE( p_module->i_bool_items );
339 LOAD_IMMEDIATE( i_lines );
341 /* Allocate memory */
345 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
346 if( p_module->p_config == NULL )
348 p_module->confsize = 0;
352 p_module->confsize = i_lines;
354 /* Do the duplication job */
355 for (size_t i = 0; i < i_lines; i++ )
357 LOAD_IMMEDIATE( p_module->p_config[i] );
359 LOAD_STRING( p_module->p_config[i].psz_type );
360 LOAD_STRING( p_module->p_config[i].psz_name );
361 LOAD_STRING( p_module->p_config[i].psz_text );
362 LOAD_STRING( p_module->p_config[i].psz_longtext );
363 LOAD_STRING( p_module->p_config[i].psz_oldname );
364 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
366 if (IsConfigStringType (p_module->p_config[i].i_type))
368 LOAD_STRING (p_module->p_config[i].orig.psz);
369 p_module->p_config[i].value.psz =
370 (p_module->p_config[i].orig.psz != NULL)
371 ? strdup (p_module->p_config[i].orig.psz) : NULL;
372 p_module->p_config[i].saved.psz = NULL;
376 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
377 sizeof (p_module->p_config[i].value));
378 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
379 sizeof (p_module->p_config[i].saved));
382 p_module->p_config[i].b_dirty = false;
384 p_module->p_config[i].p_lock = &p_module->lock;
386 if( p_module->p_config[i].i_list )
388 if( p_module->p_config[i].ppsz_list )
391 p_module->p_config[i].ppsz_list =
392 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
393 if( p_module->p_config[i].ppsz_list )
395 for( j = 0; j < p_module->p_config[i].i_list; j++ )
396 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
397 p_module->p_config[i].ppsz_list[j] = NULL;
400 if( p_module->p_config[i].ppsz_list_text )
403 p_module->p_config[i].ppsz_list_text =
404 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
405 if( p_module->p_config[i].ppsz_list_text )
407 for( j = 0; j < p_module->p_config[i].i_list; j++ )
408 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
409 p_module->p_config[i].ppsz_list_text[j] = NULL;
412 if( p_module->p_config[i].pi_list )
414 p_module->p_config[i].pi_list =
415 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
416 if( p_module->p_config[i].pi_list )
418 for (int j = 0; j < p_module->p_config[i].i_list; j++)
419 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
424 if( p_module->p_config[i].i_action )
426 p_module->p_config[i].ppf_action =
427 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
428 p_module->p_config[i].ppsz_action_text =
429 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
431 for (int j = 0; j < p_module->p_config[i].i_action; j++)
433 p_module->p_config[i].ppf_action[j] = 0;
434 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
438 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
448 static int CacheSaveSubmodule( FILE *file, module_t *p_module );
450 /*****************************************************************************
451 * SavePluginsCache: saves the plugins cache to a file
452 *****************************************************************************/
453 void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
455 static char const psz_tag[] =
456 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
457 "# This file is a cache directory tag created by VLC.\r\n"
458 "# For information about cache directory tags, see:\r\n"
459 "# http://www.brynosaurus.com/cachedir/\r\n";
461 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
464 module_cache_t **pp_cache;
465 uint32_t i_file_size = 0;
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"CACHEDIR.TAG", psz_cachedir );
478 file = utf8_fopen( psz_filename, "wb" );
481 if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
482 clearerr (file); /* what else can we do? */
486 snprintf( psz_filename, sizeof( psz_filename ),
487 "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
489 free( psz_cachedir );
490 msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
492 char psz_tmpname[sizeof (psz_filename) + 12];
493 snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
494 (uint32_t)getpid ());
495 file = utf8_fopen( psz_tmpname, "wb" );
499 /* Empty space for file size */
500 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
503 /* Contains version number */
504 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
506 #ifdef DISTRO_VERSION
507 /* Allow binary maintaner to pass a string to detect new binary version*/
508 if (fputs( DISTRO_VERSION, file ) == EOF)
511 /* Sub-version number (to avoid breakage in the dev version when cache
512 * structure changes) */
513 i_file_size = CACHE_SUBVERSION_NUM;
514 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
518 i_file_size = ftell( file );
519 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
522 i_cache = p_bank->i_cache;
523 pp_cache = p_bank->pp_cache;
525 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
528 #define SAVE_IMMEDIATE( a ) \
529 if (fwrite (&a, sizeof(a), 1, file) != 1) \
531 #define SAVE_STRING( a ) \
533 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
534 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
535 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
539 for( i = 0; i < i_cache; i++ )
541 uint32_t i_submodule;
543 /* Save common info */
544 SAVE_STRING( pp_cache[i]->psz_file );
545 SAVE_IMMEDIATE( pp_cache[i]->i_time );
546 SAVE_IMMEDIATE( pp_cache[i]->i_size );
547 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
549 if( pp_cache[i]->b_junk ) continue;
551 /* Save additional infos */
552 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
553 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
554 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
555 SAVE_STRING( pp_cache[i]->p_module->psz_help );
556 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
558 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
560 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
561 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
562 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
563 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
566 if (CacheSaveConfig (pp_cache[i]->p_module, file))
569 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
571 i_submodule = pp_cache[i]->p_module->submodule_count;
572 SAVE_IMMEDIATE( i_submodule );
573 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
577 /* Fill-up file size */
578 i_file_size = ftell( file );
579 fseek( file, 0, SEEK_SET );
580 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
581 || fflush (file)) /* flush libc buffers */
585 utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
588 utf8_unlink (psz_filename);
590 utf8_rename (psz_tmpname, psz_filename);
592 return; /* success! */
595 msg_Warn (p_this, "could not write plugins cache %s (%m)",
604 static int CacheSaveSubmodule( FILE *file, module_t *p_module )
608 if( CacheSaveSubmodule( file, p_module->next ) )
611 SAVE_STRING( p_module->psz_object_name );
612 SAVE_STRING( p_module->psz_shortname );
613 SAVE_STRING( p_module->psz_longname );
614 SAVE_STRING( p_module->psz_help );
615 for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
616 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
618 SAVE_STRING( p_module->psz_capability );
619 SAVE_IMMEDIATE( p_module->i_score );
620 SAVE_IMMEDIATE( p_module->b_unloadable );
628 static int CacheSaveConfig( module_t *p_module, FILE *file )
630 uint32_t i_lines = p_module->confsize;
632 SAVE_IMMEDIATE( p_module->i_config_items );
633 SAVE_IMMEDIATE( p_module->i_bool_items );
634 SAVE_IMMEDIATE( i_lines );
636 for (size_t i = 0; i < i_lines ; i++)
638 SAVE_IMMEDIATE( p_module->p_config[i] );
640 SAVE_STRING( p_module->p_config[i].psz_type );
641 SAVE_STRING( p_module->p_config[i].psz_name );
642 SAVE_STRING( p_module->p_config[i].psz_text );
643 SAVE_STRING( p_module->p_config[i].psz_longtext );
644 SAVE_STRING( p_module->p_config[i].psz_oldname );
645 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
647 if (IsConfigStringType (p_module->p_config[i].i_type))
648 SAVE_STRING( p_module->p_config[i].orig.psz );
650 if( p_module->p_config[i].i_list )
652 if( p_module->p_config[i].ppsz_list )
654 for (int j = 0; j < p_module->p_config[i].i_list; j++)
655 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
658 if( p_module->p_config[i].ppsz_list_text )
660 for (int j = 0; j < p_module->p_config[i].i_list; j++)
661 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
663 if( p_module->p_config[i].pi_list )
665 for (int j = 0; j < p_module->p_config[i].i_list; j++)
666 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
670 for (int j = 0; j < p_module->p_config[i].i_action; j++)
671 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
673 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
681 /*****************************************************************************
682 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
683 *****************************************************************************/
684 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
688 p_cache->pf_activate = p_module->pf_activate;
689 p_cache->pf_deactivate = p_module->pf_deactivate;
690 p_cache->handle = p_module->handle;
692 /* FIXME: This looks too simplistic an algorithm to me. What if the module
693 * file was altered such that the number of order of submodules was
694 * altered... after VLC started -- Courmisch, 09/2008 */
695 module_t *p_child = p_module->submodule,
696 *p_cchild = p_cache->submodule;
697 while( p_child && p_cchild )
699 p_cchild->pf_activate = p_child->pf_activate;
700 p_cchild->pf_deactivate = p_child->pf_deactivate;
701 p_child = p_child->next;
702 p_cchild = p_cchild->next;
705 p_cache->b_loaded = true;
706 p_module->b_loaded = false;
709 /*****************************************************************************
710 * CacheFind: finds the cache entry corresponding to a file
711 *****************************************************************************/
712 module_cache_t *CacheFind( module_bank_t *p_bank, const char *psz_file,
713 int64_t i_time, int64_t i_size )
715 module_cache_t **pp_cache;
718 pp_cache = p_bank->pp_loaded_cache;
719 i_cache = p_bank->i_loaded_cache;
721 for( i = 0; i < i_cache; i++ )
723 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
724 pp_cache[i]->i_time == i_time &&
725 pp_cache[i]->i_size == i_size ) return pp_cache[i];
731 #endif /* HAVE_DYNAMIC_PLUGINS */