1 /*****************************************************************************
2 * cache.c: Plugins cache
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
47 #include "config/configuration.h"
51 #include "modules/modules.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 #ifdef HAVE_DYNAMIC_PLUGINS
58 static int CacheLoadConfig ( module_t *, FILE * );
61 * (only used to avoid breakage in dev version when cache structure changes) */
62 #define CACHE_SUBVERSION_NUM 18
65 #define CACHE_NAME "plugins.dat"
66 /* Magic for the cache filename */
67 #define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
70 void CacheDelete( vlc_object_t *obj, const char *dir )
74 assert( dir != NULL );
76 if( asprintf( &path, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
78 msg_Dbg( obj, "removing plugins cache file %s", path );
84 * Loads a plugins cache file.
86 * This function will load the plugin cache if present and valid. This cache
87 * will in turn be queried by AllocateAllPlugins() to see if it needs to
88 * actually load the dynamically loadable module.
89 * This allows us to only fully load plugins when they are actually used.
91 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
96 char p_cachestring[sizeof(CACHE_STRING)];
100 assert( dir != NULL );
103 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
106 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
108 file = vlc_fopen( psz_filename, "rb" );
111 msg_Warn( p_this, "cannot read %s (%m)",
113 free( psz_filename );
116 free( psz_filename );
118 /* Check the file is a plugins cache */
119 i_size = sizeof(CACHE_STRING) - 1;
120 i_read = fread( p_cachestring, 1, i_size, file );
121 if( i_read != i_size ||
122 memcmp( p_cachestring, CACHE_STRING, i_size ) )
124 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
129 #ifdef DISTRO_VERSION
130 /* Check for distribution specific version */
131 char p_distrostring[sizeof( DISTRO_VERSION )];
132 i_size = sizeof( DISTRO_VERSION ) - 1;
133 i_read = fread( p_distrostring, 1, i_size, file );
134 if( i_read != i_size ||
135 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
137 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
143 /* Check Sub-version number */
144 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
145 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
147 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
148 "(corrupted header)" );
153 /* Check header marker */
154 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
155 if( i_read != sizeof(i_marker) ||
156 i_marker != ftell( file ) - (int)sizeof(i_marker) )
158 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
159 "(corrupted header)" );
164 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
166 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
167 "(file too short)" );
172 module_cache_t *cache = NULL;
174 #define LOAD_IMMEDIATE(a) \
175 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
176 #define LOAD_STRING(a) \
179 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
180 || ( i_size > 16384 ) ) \
183 char *psz = xmalloc( i_size ); \
184 if( fread( psz, i_size, 1, file ) != 1 ) { \
188 if( psz[i_size-1] ) { \
196 for (size_t count = 0; count < i_cache;)
202 module = vlc_module_create (NULL);
204 /* Load additional infos */
205 LOAD_STRING(module->psz_shortname);
206 LOAD_STRING(module->psz_longname);
207 LOAD_STRING(module->psz_help);
209 LOAD_IMMEDIATE(module->i_shortcuts);
210 if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
214 module->pp_shortcuts =
215 xmalloc (sizeof (char **) * module->i_shortcuts);
216 for (unsigned j = 0; j < module->i_shortcuts; j++)
217 LOAD_STRING(module->pp_shortcuts[j]);
220 LOAD_STRING(module->psz_capability);
221 LOAD_IMMEDIATE(module->i_score);
222 LOAD_IMMEDIATE(module->b_unloadable);
225 if (CacheLoadConfig (module, file) != VLC_SUCCESS)
228 LOAD_STRING(module->domain);
229 if (module->domain != NULL)
230 vlc_bindtextdomain (module->domain);
232 LOAD_IMMEDIATE( i_submodules );
234 while( i_submodules-- )
236 module_t *submodule = vlc_module_create (module);
237 free (submodule->pp_shortcuts);
238 LOAD_STRING(submodule->psz_shortname);
239 LOAD_STRING(submodule->psz_longname);
241 LOAD_IMMEDIATE(submodule->i_shortcuts);
242 if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
246 submodule->pp_shortcuts =
247 xmalloc (sizeof (char **) * submodule->i_shortcuts);
248 for (unsigned j = 0; j < submodule->i_shortcuts; j++)
249 LOAD_STRING(submodule->pp_shortcuts[j]);
252 LOAD_STRING(submodule->psz_capability);
253 LOAD_IMMEDIATE(submodule->i_score);
259 /* Load common info */
263 LOAD_IMMEDIATE(st.st_mtime);
264 LOAD_IMMEDIATE(st.st_size);
266 CacheAdd (&cache, &count, path, &st, module);
268 /* TODO: deal with errors */
276 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
284 static int CacheLoadConfig( module_t *p_module, FILE *file )
289 /* Calculate the structure length */
290 LOAD_IMMEDIATE( p_module->i_config_items );
291 LOAD_IMMEDIATE( p_module->i_bool_items );
293 LOAD_IMMEDIATE( i_lines );
295 /* Allocate memory */
299 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
300 if( p_module->p_config == NULL )
302 p_module->confsize = 0;
306 p_module->confsize = i_lines;
308 /* Do the duplication job */
309 for (size_t i = 0; i < i_lines; i++ )
311 LOAD_IMMEDIATE( p_module->p_config[i] );
313 LOAD_STRING( p_module->p_config[i].psz_type );
314 LOAD_STRING( p_module->p_config[i].psz_name );
315 LOAD_STRING( p_module->p_config[i].psz_text );
316 LOAD_STRING( p_module->p_config[i].psz_longtext );
318 if (IsConfigStringType (p_module->p_config[i].i_type))
320 LOAD_STRING (p_module->p_config[i].orig.psz);
321 p_module->p_config[i].value.psz =
322 (p_module->p_config[i].orig.psz != NULL)
323 ? strdup (p_module->p_config[i].orig.psz) : NULL;
326 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
327 sizeof (p_module->p_config[i].value));
329 p_module->p_config[i].b_dirty = false;
331 if( p_module->p_config[i].i_list )
333 if( p_module->p_config[i].ppsz_list )
336 p_module->p_config[i].ppsz_list =
337 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
338 if( p_module->p_config[i].ppsz_list )
340 for( j = 0; j < p_module->p_config[i].i_list; j++ )
341 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
342 p_module->p_config[i].ppsz_list[j] = NULL;
345 if( p_module->p_config[i].ppsz_list_text )
348 p_module->p_config[i].ppsz_list_text =
349 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
350 if( p_module->p_config[i].ppsz_list_text )
352 for( j = 0; j < p_module->p_config[i].i_list; j++ )
353 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
354 p_module->p_config[i].ppsz_list_text[j] = NULL;
357 if( p_module->p_config[i].pi_list )
359 p_module->p_config[i].pi_list =
360 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
361 if( p_module->p_config[i].pi_list )
363 for (int j = 0; j < p_module->p_config[i].i_list; j++)
364 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
369 if( p_module->p_config[i].i_action )
371 p_module->p_config[i].ppf_action =
372 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
373 p_module->p_config[i].ppsz_action_text =
374 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
376 for (int j = 0; j < p_module->p_config[i].i_action; j++)
378 p_module->p_config[i].ppf_action[j] = NULL;
379 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
391 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
394 * Saves a module cache to disk, and release cache data from memory.
396 void CacheSave (vlc_object_t *p_this, const char *dir,
397 module_cache_t *entries, size_t n)
399 char *filename = NULL, *tmpname = NULL;
401 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
404 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
406 msg_Dbg (p_this, "saving plugins cache %s", filename);
408 FILE *file = vlc_fopen (tmpname, "wb");
411 if (errno != EACCES && errno != ENOENT)
412 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
416 if (CacheSaveBank (file, entries, n))
418 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
421 vlc_unlink (tmpname);
425 #if !defined( WIN32 ) && !defined( __OS2__ )
426 vlc_rename (tmpname, filename); /* atomically replace old cache */
429 vlc_unlink (filename);
431 vlc_rename (tmpname, filename);
437 for (size_t i = 0; i < n; i++)
438 free (entries[i].path);
442 static int CacheSaveConfig (FILE *, const module_t *);
443 static int CacheSaveSubmodule (FILE *, const module_t *);
445 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
448 uint32_t i_file_size = 0;
450 /* Contains version number */
451 if (fputs (CACHE_STRING, file) == EOF)
453 #ifdef DISTRO_VERSION
454 /* Allow binary maintaner to pass a string to detect new binary version*/
455 if (fputs( DISTRO_VERSION, file ) == EOF)
458 /* Sub-version number (to avoid breakage in the dev version when cache
459 * structure changes) */
460 i_file_size = CACHE_SUBVERSION_NUM;
461 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
465 i_file_size = ftell( file );
466 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
469 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
472 #define SAVE_IMMEDIATE( a ) \
473 if (fwrite (&a, sizeof(a), 1, file) != 1) \
475 #define SAVE_STRING( a ) \
477 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
478 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
479 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
483 for (unsigned i = 0; i < i_cache; i++)
485 module_t *module = cache[i].p_module;
486 uint32_t i_submodule;
488 /* Save additional infos */
489 SAVE_STRING(module->psz_shortname);
490 SAVE_STRING(module->psz_longname);
491 SAVE_STRING(module->psz_help);
492 SAVE_IMMEDIATE(module->i_shortcuts);
493 for (unsigned j = 0; j < module->i_shortcuts; j++)
494 SAVE_STRING(module->pp_shortcuts[j]);
496 SAVE_STRING(module->psz_capability);
497 SAVE_IMMEDIATE(module->i_score);
498 SAVE_IMMEDIATE(module->b_unloadable);
501 if (CacheSaveConfig (file, module))
504 SAVE_STRING(module->domain);
506 i_submodule = module->submodule_count;
507 SAVE_IMMEDIATE( i_submodule );
508 if (CacheSaveSubmodule (file, module->submodule))
511 /* Save common info */
512 SAVE_STRING(cache[i].path);
513 SAVE_IMMEDIATE(cache[i].mtime);
514 SAVE_IMMEDIATE(cache[i].size);
517 if (fflush (file)) /* flush libc buffers */
519 return 0; /* success! */
525 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
529 if( CacheSaveSubmodule( file, p_module->next ) )
532 SAVE_STRING( p_module->psz_shortname );
533 SAVE_STRING( p_module->psz_longname );
534 SAVE_IMMEDIATE( p_module->i_shortcuts );
535 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
536 SAVE_STRING( p_module->pp_shortcuts[j] );
538 SAVE_STRING( p_module->psz_capability );
539 SAVE_IMMEDIATE( p_module->i_score );
547 static int CacheSaveConfig (FILE *file, const module_t *p_module)
549 uint32_t i_lines = p_module->confsize;
551 SAVE_IMMEDIATE( p_module->i_config_items );
552 SAVE_IMMEDIATE( p_module->i_bool_items );
553 SAVE_IMMEDIATE( i_lines );
555 for (size_t i = 0; i < i_lines ; i++)
557 SAVE_IMMEDIATE( p_module->p_config[i] );
559 SAVE_STRING( p_module->p_config[i].psz_type );
560 SAVE_STRING( p_module->p_config[i].psz_name );
561 SAVE_STRING( p_module->p_config[i].psz_text );
562 SAVE_STRING( p_module->p_config[i].psz_longtext );
564 if (IsConfigStringType (p_module->p_config[i].i_type))
565 SAVE_STRING( p_module->p_config[i].orig.psz );
567 if( p_module->p_config[i].i_list )
569 if( p_module->p_config[i].ppsz_list )
571 for (int j = 0; j < p_module->p_config[i].i_list; j++)
572 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
575 if( p_module->p_config[i].ppsz_list_text )
577 for (int j = 0; j < p_module->p_config[i].i_list; j++)
578 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
580 if( p_module->p_config[i].pi_list )
582 for (int j = 0; j < p_module->p_config[i].i_list; j++)
583 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
587 for (int j = 0; j < p_module->p_config[i].i_action; j++)
588 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
596 /*****************************************************************************
597 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
598 *****************************************************************************/
599 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
603 p_cache->pf_activate = p_module->pf_activate;
604 p_cache->pf_deactivate = p_module->pf_deactivate;
605 p_cache->handle = p_module->handle;
607 /* FIXME: This looks too simplistic an algorithm to me. What if the module
608 * file was altered such that the number of order of submodules was
609 * altered... after VLC started -- Courmisch, 09/2008 */
610 module_t *p_child = p_module->submodule,
611 *p_cchild = p_cache->submodule;
612 while( p_child && p_cchild )
614 p_cchild->pf_activate = p_child->pf_activate;
615 p_cchild->pf_deactivate = p_child->pf_deactivate;
616 p_child = p_child->next;
617 p_cchild = p_cchild->next;
620 p_cache->b_loaded = true;
621 p_module->b_loaded = false;
625 * Looks up a plugin file in a table of cached plugins.
627 module_t *CacheFind (module_cache_t *cache, size_t count,
628 const char *path, const struct stat *st)
632 if (cache->path != NULL
633 && !strcmp (cache->path, path)
634 && cache->mtime == st->st_mtime
635 && cache->size == st->st_size)
637 module_t *module = cache->p_module;
638 cache->p_module = NULL;
648 /** Adds entry to the cache */
649 int CacheAdd (module_cache_t **cachep, size_t *countp,
650 const char *path, const struct stat *st, module_t *module)
652 module_cache_t *cache = *cachep;
653 const size_t count = *countp;
655 cache = realloc (cache, (count + 1) * sizeof (*cache));
656 if (unlikely(cache == NULL))
661 /* NOTE: strdup() could be avoided, but it would be a bit ugly */
662 cache->path = strdup (path);
663 cache->mtime = st->st_mtime;
664 cache->size = st->st_size;
665 cache->p_module = module;
670 #endif /* HAVE_DYNAMIC_PLUGINS */