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 #include "config/configuration.h"
48 #include "vlc_charset.h"
50 #include "modules/modules.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 #ifdef HAVE_DYNAMIC_PLUGINS
57 static int CacheLoadConfig ( module_t *, FILE * );
60 * (only used to avoid breakage in dev version when cache structure changes) */
61 #define CACHE_SUBVERSION_NUM 9
63 /* Format string for the cache filename */
64 #define CACHENAME_FORMAT \
65 "plugins-%.2zx%.2zx%.2"PRIx8"-%x.dat"
66 /* Magic for the cache filename */
67 #define CACHENAME_VALUES \
68 sizeof(int), sizeof(void *), *(uint8_t *)&(uint16_t){ 0xbe1e }, vlc_CPU()
71 /*****************************************************************************
72 * LoadPluginsCache: loads the plugins cache file
73 *****************************************************************************
74 * This function will load the plugin cache if present and valid. This cache
75 * will in turn be queried by AllocateAllPlugins() to see if it needs to
76 * actually load the dynamically loadable module.
77 * This allows us to only fully load plugins when they are actually used.
78 *****************************************************************************/
79 void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, bool b_delete )
81 char *psz_filename, *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
83 int i, j, i_size, i_read;
84 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
86 module_cache_t **pp_cache = 0;
87 int32_t i_file_size, i_marker;
89 if( !psz_cachedir ) /* XXX: this should never happen */
91 msg_Err( p_this, "Unable to get cache directory" );
95 if( asprintf( &psz_filename, "%s"DIR_SEP CACHENAME_FORMAT,
96 psz_cachedir, CACHENAME_VALUES ) == -1 )
101 free( psz_cachedir );
105 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
106 utf8_unlink( psz_filename );
107 free( psz_filename );
111 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
113 file = utf8_fopen( psz_filename, "rb" );
116 msg_Warn( p_this, "could not open plugins cache file %s for reading",
118 free( psz_filename );
121 free( psz_filename );
123 /* Check the file size */
124 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
125 if( i_read != sizeof(i_file_size) )
127 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
133 fseek( file, 0, SEEK_END );
134 if( ftell( file ) != i_file_size )
136 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
137 "(corrupted size)" );
141 fseek( file, sizeof(i_file_size), SEEK_SET );
143 /* Check the file is a plugins cache */
144 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
145 i_read = fread( p_cachestring, 1, i_size, file );
146 if( i_read != i_size ||
147 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
149 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
154 #ifdef DISTRO_VERSION
155 /* Check for distribution specific version */
156 char p_distrostring[sizeof( DISTRO_VERSION )];
157 i_size = sizeof( DISTRO_VERSION ) - 1;
158 i_read = fread( p_distrostring, 1, i_size, file );
159 if( i_read != i_size ||
160 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
162 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
168 /* Check Sub-version number */
169 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
170 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
172 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
173 "(corrupted header)" );
178 /* Check header marker */
179 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
180 if( i_read != sizeof(i_marker) ||
181 i_marker != ftell( file ) - (int)sizeof(i_marker) )
183 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
184 "(corrupted header)" );
189 p_bank->i_loaded_cache = 0;
190 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
192 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
193 "(file too short)" );
199 pp_cache = p_bank->pp_loaded_cache =
200 xmalloc( i_cache * sizeof(void *) );
202 #define LOAD_IMMEDIATE(a) \
203 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
204 #define LOAD_STRING(a) \
207 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
208 || ( i_size > 16384 ) ) \
211 char *psz = xmalloc( i_size ); \
212 if( fread( psz, i_size, 1, file ) != 1 ) { \
216 if( psz[i_size-1] ) { \
224 for( i = 0; i < i_cache; i++ )
229 pp_cache[i] = xmalloc( sizeof(module_cache_t) );
230 p_bank->i_loaded_cache++;
232 /* Load common info */
233 LOAD_STRING( pp_cache[i]->psz_file );
234 LOAD_IMMEDIATE( pp_cache[i]->i_time );
235 LOAD_IMMEDIATE( pp_cache[i]->i_size );
236 pp_cache[i]->b_used = false;
238 pp_cache[i]->p_module = vlc_module_create( p_this );
240 /* Load additional infos */
241 free( pp_cache[i]->p_module->psz_object_name );
242 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
243 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
244 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
245 LOAD_STRING( pp_cache[i]->p_module->psz_help );
246 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
248 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
250 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
251 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
252 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
253 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
256 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
259 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
261 LOAD_IMMEDIATE( i_submodules );
263 while( i_submodules-- )
265 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
266 free( p_module->psz_object_name );
267 LOAD_STRING( p_module->psz_object_name );
268 LOAD_STRING( p_module->psz_shortname );
269 LOAD_STRING( p_module->psz_longname );
270 LOAD_STRING( p_module->psz_help );
271 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
273 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
275 LOAD_STRING( p_module->psz_capability );
276 LOAD_IMMEDIATE( p_module->i_score );
277 LOAD_IMMEDIATE( p_module->b_unloadable );
286 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
289 p_bank->i_loaded_cache = 0;
296 /* This function should never be called.
297 * It is only used as a non-NULL vlc_callback_t value for comparison. */
298 static int dummy_callback (vlc_object_t *obj, const char *name,
299 vlc_value_t oldval, vlc_value_t newval, void *data)
301 (void) obj; (void)name; (void)oldval; (void)newval; (void)data;
306 static int CacheLoadConfig( module_t *p_module, FILE *file )
311 /* Calculate the structure length */
312 LOAD_IMMEDIATE( p_module->i_config_items );
313 LOAD_IMMEDIATE( p_module->i_bool_items );
315 LOAD_IMMEDIATE( i_lines );
317 /* Allocate memory */
321 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
322 if( p_module->p_config == NULL )
324 p_module->confsize = 0;
328 p_module->confsize = i_lines;
330 /* Do the duplication job */
331 for (size_t i = 0; i < i_lines; i++ )
333 LOAD_IMMEDIATE( p_module->p_config[i] );
335 LOAD_STRING( p_module->p_config[i].psz_type );
336 LOAD_STRING( p_module->p_config[i].psz_name );
337 LOAD_STRING( p_module->p_config[i].psz_text );
338 LOAD_STRING( p_module->p_config[i].psz_longtext );
339 LOAD_STRING( p_module->p_config[i].psz_oldname );
340 LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
342 if (IsConfigStringType (p_module->p_config[i].i_type))
344 LOAD_STRING (p_module->p_config[i].orig.psz);
345 p_module->p_config[i].value.psz =
346 (p_module->p_config[i].orig.psz != NULL)
347 ? strdup (p_module->p_config[i].orig.psz) : NULL;
348 p_module->p_config[i].saved.psz = NULL;
352 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
353 sizeof (p_module->p_config[i].value));
354 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
355 sizeof (p_module->p_config[i].saved));
358 p_module->p_config[i].b_dirty = false;
360 if( p_module->p_config[i].i_list )
362 if( p_module->p_config[i].ppsz_list )
365 p_module->p_config[i].ppsz_list =
366 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
367 if( p_module->p_config[i].ppsz_list )
369 for( j = 0; j < p_module->p_config[i].i_list; j++ )
370 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
371 p_module->p_config[i].ppsz_list[j] = NULL;
374 if( p_module->p_config[i].ppsz_list_text )
377 p_module->p_config[i].ppsz_list_text =
378 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
379 if( p_module->p_config[i].ppsz_list_text )
381 for( j = 0; j < p_module->p_config[i].i_list; j++ )
382 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
383 p_module->p_config[i].ppsz_list_text[j] = NULL;
386 if( p_module->p_config[i].pi_list )
388 p_module->p_config[i].pi_list =
389 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
390 if( p_module->p_config[i].pi_list )
392 for (int j = 0; j < p_module->p_config[i].i_list; j++)
393 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
398 if( p_module->p_config[i].i_action )
400 p_module->p_config[i].ppf_action =
401 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
402 p_module->p_config[i].ppsz_action_text =
403 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
405 for (int j = 0; j < p_module->p_config[i].i_action; j++)
407 p_module->p_config[i].ppf_action[j] = NULL;
408 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
413 LOAD_IMMEDIATE( has_callback );
415 p_module->p_config[i].pf_callback = dummy_callback;
425 static int CacheSaveBank( FILE *file, module_bank_t *p_bank );
427 /*****************************************************************************
428 * SavePluginsCache: saves the plugins cache to a file
429 *****************************************************************************/
430 void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
432 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
433 if( !psz_cachedir ) /* XXX: this should never happen */
435 msg_Err( p_this, "unable to get cache directory" );
439 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
440 config_CreateDir( p_this, psz_cachedir );
442 snprintf( psz_filename, sizeof( psz_filename ),
443 "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
445 free( psz_cachedir );
447 char psz_tmpname[sizeof (psz_filename) + 12];
448 snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
449 (uint32_t)getpid ());
450 FILE *file = utf8_fopen( psz_tmpname, "wb" );
454 msg_Dbg (p_this, "saving plugins cache %s", psz_filename);
455 if (CacheSaveBank (file, p_bank))
459 utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
462 utf8_unlink (psz_filename);
464 utf8_rename (psz_tmpname, psz_filename);
466 return; /* success! */
469 msg_Warn (p_this, "could not write plugins cache %s (%m)",
478 static int CacheSaveConfig (FILE *, module_t *);
479 static int CacheSaveSubmodule (FILE *, module_t *);
481 static int CacheSaveBank (FILE *file, module_bank_t *p_bank)
483 uint32_t i_file_size = 0;
485 /* Empty space for file size */
486 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
489 /* Contains version number */
490 if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
492 #ifdef DISTRO_VERSION
493 /* Allow binary maintaner to pass a string to detect new binary version*/
494 if (fputs( DISTRO_VERSION, file ) == EOF)
497 /* Sub-version number (to avoid breakage in the dev version when cache
498 * structure changes) */
499 i_file_size = CACHE_SUBVERSION_NUM;
500 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
504 i_file_size = ftell( file );
505 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
508 module_cache_t **pp_cache = p_bank->pp_cache;
509 uint32_t i_cache = p_bank->i_cache;
511 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
514 #define SAVE_IMMEDIATE( a ) \
515 if (fwrite (&a, sizeof(a), 1, file) != 1) \
517 #define SAVE_STRING( a ) \
519 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
520 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
521 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
525 for (unsigned i = 0; i < i_cache; i++)
527 uint32_t i_submodule;
529 /* Save common info */
530 SAVE_STRING( pp_cache[i]->psz_file );
531 SAVE_IMMEDIATE( pp_cache[i]->i_time );
532 SAVE_IMMEDIATE( pp_cache[i]->i_size );
534 /* Save additional infos */
535 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
536 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
537 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
538 SAVE_STRING( pp_cache[i]->p_module->psz_help );
539 for (unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++)
541 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
543 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
544 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
545 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
546 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
549 if (CacheSaveConfig (file, pp_cache[i]->p_module))
552 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
554 i_submodule = pp_cache[i]->p_module->submodule_count;
555 SAVE_IMMEDIATE( i_submodule );
556 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
560 /* Fill-up file size */
561 i_file_size = ftell( file );
562 fseek( file, 0, SEEK_SET );
563 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
564 || fflush (file)) /* flush libc buffers */
566 return 0; /* success! */
572 static int CacheSaveSubmodule( FILE *file, module_t *p_module )
576 if( CacheSaveSubmodule( file, p_module->next ) )
579 SAVE_STRING( p_module->psz_object_name );
580 SAVE_STRING( p_module->psz_shortname );
581 SAVE_STRING( p_module->psz_longname );
582 SAVE_STRING( p_module->psz_help );
583 for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
584 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
586 SAVE_STRING( p_module->psz_capability );
587 SAVE_IMMEDIATE( p_module->i_score );
588 SAVE_IMMEDIATE( p_module->b_unloadable );
596 static int CacheSaveConfig (FILE *file, module_t *p_module)
598 uint32_t i_lines = p_module->confsize;
600 SAVE_IMMEDIATE( p_module->i_config_items );
601 SAVE_IMMEDIATE( p_module->i_bool_items );
602 SAVE_IMMEDIATE( i_lines );
604 for (size_t i = 0; i < i_lines ; i++)
606 SAVE_IMMEDIATE( p_module->p_config[i] );
608 SAVE_STRING( p_module->p_config[i].psz_type );
609 SAVE_STRING( p_module->p_config[i].psz_name );
610 SAVE_STRING( p_module->p_config[i].psz_text );
611 SAVE_STRING( p_module->p_config[i].psz_longtext );
612 SAVE_STRING( p_module->p_config[i].psz_oldname );
613 SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
615 if (IsConfigStringType (p_module->p_config[i].i_type))
616 SAVE_STRING( p_module->p_config[i].orig.psz );
618 if( p_module->p_config[i].i_list )
620 if( p_module->p_config[i].ppsz_list )
622 for (int j = 0; j < p_module->p_config[i].i_list; j++)
623 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
626 if( p_module->p_config[i].ppsz_list_text )
628 for (int j = 0; j < p_module->p_config[i].i_list; j++)
629 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
631 if( p_module->p_config[i].pi_list )
633 for (int j = 0; j < p_module->p_config[i].i_list; j++)
634 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
638 for (int j = 0; j < p_module->p_config[i].i_action; j++)
639 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
641 bool has_callback = p_module->p_config[i].pf_callback != NULL;
642 SAVE_IMMEDIATE( has_callback );
650 /*****************************************************************************
651 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
652 *****************************************************************************/
653 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
657 p_cache->pf_activate = p_module->pf_activate;
658 p_cache->pf_deactivate = p_module->pf_deactivate;
659 p_cache->handle = p_module->handle;
661 /* FIXME: This looks too simplistic an algorithm to me. What if the module
662 * file was altered such that the number of order of submodules was
663 * altered... after VLC started -- Courmisch, 09/2008 */
664 module_t *p_child = p_module->submodule,
665 *p_cchild = p_cache->submodule;
666 while( p_child && p_cchild )
668 p_cchild->pf_activate = p_child->pf_activate;
669 p_cchild->pf_deactivate = p_child->pf_deactivate;
670 p_child = p_child->next;
671 p_cchild = p_cchild->next;
674 p_cache->b_loaded = true;
675 p_module->b_loaded = false;
678 /*****************************************************************************
679 * CacheFind: finds the cache entry corresponding to a file
680 *****************************************************************************/
681 module_cache_t *CacheFind( module_bank_t *p_bank, const char *psz_file,
682 int64_t i_time, int64_t i_size )
684 module_cache_t **pp_cache;
687 pp_cache = p_bank->pp_loaded_cache;
688 i_cache = p_bank->i_loaded_cache;
690 for( i = 0; i < i_cache; i++ )
692 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
693 pp_cache[i]->i_time == i_time &&
694 pp_cache[i]->i_size == i_size ) return pp_cache[i];
700 #endif /* HAVE_DYNAMIC_PLUGINS */