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>
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 17
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 );
317 LOAD_STRING( p_module->p_config[i].psz_oldname );
319 if (IsConfigStringType (p_module->p_config[i].i_type))
321 LOAD_STRING (p_module->p_config[i].orig.psz);
322 p_module->p_config[i].value.psz =
323 (p_module->p_config[i].orig.psz != NULL)
324 ? strdup (p_module->p_config[i].orig.psz) : NULL;
327 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
328 sizeof (p_module->p_config[i].value));
330 p_module->p_config[i].b_dirty = false;
332 if( p_module->p_config[i].i_list )
334 if( p_module->p_config[i].ppsz_list )
337 p_module->p_config[i].ppsz_list =
338 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
339 if( p_module->p_config[i].ppsz_list )
341 for( j = 0; j < p_module->p_config[i].i_list; j++ )
342 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
343 p_module->p_config[i].ppsz_list[j] = NULL;
346 if( p_module->p_config[i].ppsz_list_text )
349 p_module->p_config[i].ppsz_list_text =
350 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
351 if( p_module->p_config[i].ppsz_list_text )
353 for( j = 0; j < p_module->p_config[i].i_list; j++ )
354 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
355 p_module->p_config[i].ppsz_list_text[j] = NULL;
358 if( p_module->p_config[i].pi_list )
360 p_module->p_config[i].pi_list =
361 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
362 if( p_module->p_config[i].pi_list )
364 for (int j = 0; j < p_module->p_config[i].i_list; j++)
365 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
370 if( p_module->p_config[i].i_action )
372 p_module->p_config[i].ppf_action =
373 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
374 p_module->p_config[i].ppsz_action_text =
375 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
377 for (int j = 0; j < p_module->p_config[i].i_action; j++)
379 p_module->p_config[i].ppf_action[j] = NULL;
380 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
392 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
395 * Saves a module cache to disk, and release cache data from memory.
397 void CacheSave (vlc_object_t *p_this, const char *dir,
398 module_cache_t *entries, size_t n)
400 char *filename = NULL, *tmpname = NULL;
402 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
405 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
407 msg_Dbg (p_this, "saving plugins cache %s", filename);
409 FILE *file = vlc_fopen (tmpname, "wb");
412 if (errno != EACCES && errno != ENOENT)
413 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
417 if (CacheSaveBank (file, entries, n))
419 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
422 vlc_unlink (tmpname);
426 #if !defined( WIN32 ) && !defined( __OS2__ )
427 vlc_rename (tmpname, filename); /* atomically replace old cache */
430 vlc_unlink (filename);
432 vlc_rename (tmpname, filename);
438 for (size_t i = 0; i < n; i++)
439 free (entries[i].path);
443 static int CacheSaveConfig (FILE *, const module_t *);
444 static int CacheSaveSubmodule (FILE *, const module_t *);
446 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
449 uint32_t i_file_size = 0;
451 /* Contains version number */
452 if (fputs (CACHE_STRING, file) == EOF)
454 #ifdef DISTRO_VERSION
455 /* Allow binary maintaner to pass a string to detect new binary version*/
456 if (fputs( DISTRO_VERSION, file ) == EOF)
459 /* Sub-version number (to avoid breakage in the dev version when cache
460 * structure changes) */
461 i_file_size = CACHE_SUBVERSION_NUM;
462 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
466 i_file_size = ftell( file );
467 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
470 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
473 #define SAVE_IMMEDIATE( a ) \
474 if (fwrite (&a, sizeof(a), 1, file) != 1) \
476 #define SAVE_STRING( a ) \
478 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
479 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
480 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
484 for (unsigned i = 0; i < i_cache; i++)
486 module_t *module = cache[i].p_module;
487 uint32_t i_submodule;
489 /* Save additional infos */
490 SAVE_STRING(module->psz_shortname);
491 SAVE_STRING(module->psz_longname);
492 SAVE_STRING(module->psz_help);
493 SAVE_IMMEDIATE(module->i_shortcuts);
494 for (unsigned j = 0; j < module->i_shortcuts; j++)
495 SAVE_STRING(module->pp_shortcuts[j]);
497 SAVE_STRING(module->psz_capability);
498 SAVE_IMMEDIATE(module->i_score);
499 SAVE_IMMEDIATE(module->b_unloadable);
502 if (CacheSaveConfig (file, module))
505 SAVE_STRING(module->domain);
507 i_submodule = module->submodule_count;
508 SAVE_IMMEDIATE( i_submodule );
509 if (CacheSaveSubmodule (file, module->submodule))
512 /* Save common info */
513 SAVE_STRING(cache[i].path);
514 SAVE_IMMEDIATE(cache[i].mtime);
515 SAVE_IMMEDIATE(cache[i].size);
518 if (fflush (file)) /* flush libc buffers */
520 return 0; /* success! */
526 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
530 if( CacheSaveSubmodule( file, p_module->next ) )
533 SAVE_STRING( p_module->psz_shortname );
534 SAVE_STRING( p_module->psz_longname );
535 SAVE_IMMEDIATE( p_module->i_shortcuts );
536 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
537 SAVE_STRING( p_module->pp_shortcuts[j] );
539 SAVE_STRING( p_module->psz_capability );
540 SAVE_IMMEDIATE( p_module->i_score );
548 static int CacheSaveConfig (FILE *file, const module_t *p_module)
550 uint32_t i_lines = p_module->confsize;
552 SAVE_IMMEDIATE( p_module->i_config_items );
553 SAVE_IMMEDIATE( p_module->i_bool_items );
554 SAVE_IMMEDIATE( i_lines );
556 for (size_t i = 0; i < i_lines ; i++)
558 SAVE_IMMEDIATE( p_module->p_config[i] );
560 SAVE_STRING( p_module->p_config[i].psz_type );
561 SAVE_STRING( p_module->p_config[i].psz_name );
562 SAVE_STRING( p_module->p_config[i].psz_text );
563 SAVE_STRING( p_module->p_config[i].psz_longtext );
564 SAVE_STRING( p_module->p_config[i].psz_oldname );
566 if (IsConfigStringType (p_module->p_config[i].i_type))
567 SAVE_STRING( p_module->p_config[i].orig.psz );
569 if( p_module->p_config[i].i_list )
571 if( p_module->p_config[i].ppsz_list )
573 for (int j = 0; j < p_module->p_config[i].i_list; j++)
574 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
577 if( p_module->p_config[i].ppsz_list_text )
579 for (int j = 0; j < p_module->p_config[i].i_list; j++)
580 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
582 if( p_module->p_config[i].pi_list )
584 for (int j = 0; j < p_module->p_config[i].i_list; j++)
585 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
589 for (int j = 0; j < p_module->p_config[i].i_action; j++)
590 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
598 /*****************************************************************************
599 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
600 *****************************************************************************/
601 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
605 p_cache->pf_activate = p_module->pf_activate;
606 p_cache->pf_deactivate = p_module->pf_deactivate;
607 p_cache->handle = p_module->handle;
609 /* FIXME: This looks too simplistic an algorithm to me. What if the module
610 * file was altered such that the number of order of submodules was
611 * altered... after VLC started -- Courmisch, 09/2008 */
612 module_t *p_child = p_module->submodule,
613 *p_cchild = p_cache->submodule;
614 while( p_child && p_cchild )
616 p_cchild->pf_activate = p_child->pf_activate;
617 p_cchild->pf_deactivate = p_child->pf_deactivate;
618 p_child = p_child->next;
619 p_cchild = p_cchild->next;
622 p_cache->b_loaded = true;
623 p_module->b_loaded = false;
627 * Looks up a plugin file in a table of cached plugins.
629 module_t *CacheFind (module_cache_t *cache, size_t count,
630 const char *path, const struct stat *st)
634 if (cache->path != NULL
635 && !strcmp (cache->path, path)
636 && cache->mtime == st->st_mtime
637 && cache->size == st->st_size)
639 module_t *module = cache->p_module;
640 cache->p_module = NULL;
650 /** Adds entry to the cache */
651 int CacheAdd (module_cache_t **cachep, size_t *countp,
652 const char *path, const struct stat *st, module_t *module)
654 module_cache_t *cache = *cachep;
655 const size_t count = *countp;
657 cache = realloc (cache, (count + 1) * sizeof (*cache));
658 if (unlikely(cache == NULL))
663 /* NOTE: strdup() could be avoided, but it would be a bit ugly */
664 cache->path = strdup (path);
665 cache->mtime = st->st_mtime;
666 cache->size = st->st_size;
667 cache->p_module = module;
672 #endif /* HAVE_DYNAMIC_PLUGINS */