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 *****************************************************************************/
34 #include <sys/types.h>
39 #include <vlc_common.h>
42 #include <vlc_plugin.h>
45 #include "config/configuration.h"
49 #include "modules/modules.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 #ifdef HAVE_DYNAMIC_PLUGINS
57 * (only used to avoid breakage in dev version when cache structure changes) */
58 #define CACHE_SUBVERSION_NUM 22
61 #define CACHE_NAME "plugins.dat"
62 /* Magic for the cache filename */
63 #define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
66 void CacheDelete( vlc_object_t *obj, const char *dir )
70 assert( dir != NULL );
72 if( asprintf( &path, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
74 msg_Dbg( obj, "removing plugins cache file %s", path );
79 #define LOAD_IMMEDIATE(a) \
80 if (fread (&(a), sizeof (char), sizeof (a), file) != sizeof (a)) \
82 #define LOAD_FLAG(a) \
91 static int CacheLoadString (char **p, FILE *file)
96 LOAD_IMMEDIATE (size);
105 psz = malloc (size+1);
106 if (unlikely(psz == NULL))
108 if (fread (psz, 1, size, file) != size)
119 #define LOAD_STRING(a) \
120 if (CacheLoadString (&(a), file)) goto error
122 static int CacheLoadConfig (module_config_t *cfg, FILE *file)
124 LOAD_IMMEDIATE (cfg->i_type);
125 LOAD_IMMEDIATE (cfg->i_short);
126 LOAD_FLAG (cfg->b_advanced);
127 LOAD_FLAG (cfg->b_internal);
128 LOAD_FLAG (cfg->b_unsaveable);
129 LOAD_FLAG (cfg->b_safe);
130 LOAD_FLAG (cfg->b_removed);
131 LOAD_STRING (cfg->psz_type);
132 LOAD_STRING (cfg->psz_name);
133 LOAD_STRING (cfg->psz_text);
134 LOAD_STRING (cfg->psz_longtext);
135 LOAD_IMMEDIATE (cfg->list_count);
137 if (IsConfigStringType (cfg->i_type))
139 LOAD_STRING (cfg->orig.psz);
140 if (cfg->orig.psz != NULL)
141 cfg->value.psz = strdup (cfg->orig.psz);
143 cfg->value.psz = NULL;
146 cfg->list.psz = xmalloc (cfg->list_count * sizeof (char *));
147 else /* TODO: fix config_GetPszChoices() instead of this hack: */
148 LOAD_IMMEDIATE(cfg->list.psz_cb);
149 for (unsigned i = 0; i < cfg->list_count; i++)
151 LOAD_STRING (cfg->list.psz[i]);
152 if (cfg->list.psz[i] == NULL /* NULL -> empty string */
153 && (cfg->list.psz[i] = calloc (1, 1)) == NULL)
159 LOAD_IMMEDIATE (cfg->orig);
160 LOAD_IMMEDIATE (cfg->min);
161 LOAD_IMMEDIATE (cfg->max);
162 cfg->value = cfg->orig;
165 cfg->list.i = xmalloc (cfg->list_count * sizeof (int));
166 else /* TODO: fix config_GetPszChoices() instead of this hack: */
167 LOAD_IMMEDIATE(cfg->list.i_cb);
168 for (unsigned i = 0; i < cfg->list_count; i++)
169 LOAD_IMMEDIATE (cfg->list.i[i]);
171 cfg->list_text = xmalloc (cfg->list_count * sizeof (char *));
172 for (unsigned i = 0; i < cfg->list_count; i++)
174 LOAD_STRING (cfg->list_text[i]);
175 if (cfg->list_text[i] == NULL /* NULL -> empty string */
176 && (cfg->list_text[i] = calloc (1, 1)) == NULL)
182 return -1; /* FIXME: leaks */
185 static int CacheLoadModuleConfig (module_t *module, FILE *file)
189 /* Calculate the structure length */
190 LOAD_IMMEDIATE (module->i_config_items);
191 LOAD_IMMEDIATE (module->i_bool_items);
192 LOAD_IMMEDIATE (lines);
194 /* Allocate memory */
197 module->p_config = malloc (lines * sizeof (module_config_t));
198 if (unlikely(module->p_config == NULL))
200 module->confsize = 0;
205 module->p_config = NULL;
206 module->confsize = lines;
208 /* Do the duplication job */
209 for (size_t i = 0; i < lines; i++)
210 if (CacheLoadConfig (module->p_config + i, file))
214 return -1; /* FIXME: leaks */
219 * Loads a plugins cache file.
221 * This function will load the plugin cache if present and valid. This cache
222 * will in turn be queried by AllocateAllPlugins() to see if it needs to
223 * actually load the dynamically loadable module.
224 * This allows us to only fully load plugins when they are actually used.
226 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
231 char p_cachestring[sizeof(CACHE_STRING)];
235 assert( dir != NULL );
238 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
241 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
243 file = vlc_fopen( psz_filename, "rb" );
246 msg_Warn( p_this, "cannot read %s: %s", psz_filename,
247 vlc_strerror_c(errno) );
248 free( psz_filename );
251 free( psz_filename );
253 /* Check the file is a plugins cache */
254 i_size = sizeof(CACHE_STRING) - 1;
255 i_read = fread( p_cachestring, 1, i_size, file );
256 if( i_read != i_size ||
257 memcmp( p_cachestring, CACHE_STRING, i_size ) )
259 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
264 #ifdef DISTRO_VERSION
265 /* Check for distribution specific version */
266 char p_distrostring[sizeof( DISTRO_VERSION )];
267 i_size = sizeof( DISTRO_VERSION ) - 1;
268 i_read = fread( p_distrostring, 1, i_size, file );
269 if( i_read != i_size ||
270 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
272 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
278 /* Check Sub-version number */
279 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
280 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
282 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
283 "(corrupted header)" );
288 /* Check header marker */
289 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
290 if( i_read != sizeof(i_marker) ||
291 i_marker != ftell( file ) - (int)sizeof(i_marker) )
293 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
294 "(corrupted header)" );
299 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
301 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
302 "(file too short)" );
307 module_cache_t *cache = NULL;
309 for (size_t count = 0; count < i_cache;)
314 module = vlc_module_create (NULL);
316 /* Load additional infos */
317 LOAD_STRING(module->psz_shortname);
318 LOAD_STRING(module->psz_longname);
319 LOAD_STRING(module->psz_help);
321 LOAD_IMMEDIATE(module->i_shortcuts);
322 if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
326 module->pp_shortcuts =
327 xmalloc (sizeof (*module->pp_shortcuts) * module->i_shortcuts);
328 for (unsigned j = 0; j < module->i_shortcuts; j++)
329 LOAD_STRING(module->pp_shortcuts[j]);
332 LOAD_STRING(module->psz_capability);
333 LOAD_IMMEDIATE(module->i_score);
334 LOAD_IMMEDIATE(module->b_unloadable);
337 if (CacheLoadModuleConfig (module, file) != VLC_SUCCESS)
340 LOAD_STRING(module->domain);
341 if (module->domain != NULL)
342 vlc_bindtextdomain (module->domain);
344 LOAD_IMMEDIATE( i_submodules );
346 while( i_submodules-- )
348 module_t *submodule = vlc_module_create (module);
349 free (submodule->pp_shortcuts);
350 LOAD_STRING(submodule->psz_shortname);
351 LOAD_STRING(submodule->psz_longname);
353 LOAD_IMMEDIATE(submodule->i_shortcuts);
354 if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
358 submodule->pp_shortcuts =
359 xmalloc (sizeof (*submodule->pp_shortcuts) * submodule->i_shortcuts);
360 for (unsigned j = 0; j < submodule->i_shortcuts; j++)
361 LOAD_STRING(submodule->pp_shortcuts[j]);
364 LOAD_STRING(submodule->psz_capability);
365 LOAD_IMMEDIATE(submodule->i_score);
371 /* Load common info */
375 LOAD_IMMEDIATE(st.st_mtime);
376 LOAD_IMMEDIATE(st.st_size);
378 CacheAdd (&cache, &count, path, &st, module);
380 /* TODO: deal with errors */
388 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
395 #define SAVE_IMMEDIATE( a ) \
396 if (fwrite (&(a), sizeof(a), 1, file) != 1) \
398 #define SAVE_FLAG(a) \
404 static int CacheSaveString (FILE *file, const char *str)
406 uint16_t size = (str != NULL) ? strlen (str) : 0;
408 SAVE_IMMEDIATE (size);
409 if (size != 0 && fwrite (str, 1, size, file) != size)
417 #define SAVE_STRING( a ) \
418 if (CacheSaveString (file, (a))) \
421 static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
423 SAVE_IMMEDIATE (cfg->i_type);
424 SAVE_IMMEDIATE (cfg->i_short);
425 SAVE_FLAG (cfg->b_advanced);
426 SAVE_FLAG (cfg->b_internal);
427 SAVE_FLAG (cfg->b_unsaveable);
428 SAVE_FLAG (cfg->b_safe);
429 SAVE_FLAG (cfg->b_removed);
430 SAVE_STRING (cfg->psz_type);
431 SAVE_STRING (cfg->psz_name);
432 SAVE_STRING (cfg->psz_text);
433 SAVE_STRING (cfg->psz_longtext);
434 SAVE_IMMEDIATE (cfg->list_count);
436 if (IsConfigStringType (cfg->i_type))
438 SAVE_STRING (cfg->orig.psz);
439 if (cfg->list_count == 0)
440 SAVE_IMMEDIATE (cfg->list.psz_cb); /* XXX: see CacheLoadConfig() */
441 for (unsigned i = 0; i < cfg->list_count; i++)
442 SAVE_STRING (cfg->list.psz[i]);
446 SAVE_IMMEDIATE (cfg->orig);
447 SAVE_IMMEDIATE (cfg->min);
448 SAVE_IMMEDIATE (cfg->max);
449 if (cfg->list_count == 0)
450 SAVE_IMMEDIATE (cfg->list.i_cb); /* XXX: see CacheLoadConfig() */
451 for (unsigned i = 0; i < cfg->list_count; i++)
452 SAVE_IMMEDIATE (cfg->list.i[i]);
454 for (unsigned i = 0; i < cfg->list_count; i++)
455 SAVE_STRING (cfg->list_text[i]);
462 static int CacheSaveModuleConfig (FILE *file, const module_t *module)
464 uint16_t lines = module->confsize;
466 SAVE_IMMEDIATE (module->i_config_items);
467 SAVE_IMMEDIATE (module->i_bool_items);
468 SAVE_IMMEDIATE (lines);
470 for (size_t i = 0; i < lines; i++)
471 if (CacheSaveConfig (file, module->p_config + i))
479 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
482 * Saves a module cache to disk, and release cache data from memory.
484 void CacheSave (vlc_object_t *p_this, const char *dir,
485 module_cache_t *entries, size_t n)
487 char *filename = NULL, *tmpname = NULL;
489 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
492 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
494 msg_Dbg (p_this, "saving plugins cache %s", filename);
496 FILE *file = vlc_fopen (tmpname, "wb");
499 if (errno != EACCES && errno != ENOENT)
500 msg_Warn (p_this, "cannot create %s: %s", tmpname,
501 vlc_strerror_c(errno));
505 if (CacheSaveBank (file, entries, n))
507 msg_Warn (p_this, "cannot write %s: %s", tmpname,
508 vlc_strerror_c(errno));
511 vlc_unlink (tmpname);
515 #if !defined( _WIN32 ) && !defined( __OS2__ )
516 vlc_rename (tmpname, filename); /* atomically replace old cache */
519 vlc_unlink (filename);
521 vlc_rename (tmpname, filename);
527 for (size_t i = 0; i < n; i++)
528 free (entries[i].path);
532 static int CacheSaveSubmodule (FILE *, const module_t *);
534 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
537 uint32_t i_file_size = 0;
539 /* Contains version number */
540 if (fputs (CACHE_STRING, file) == EOF)
542 #ifdef DISTRO_VERSION
543 /* Allow binary maintaner to pass a string to detect new binary version*/
544 if (fputs( DISTRO_VERSION, file ) == EOF)
547 /* Sub-version number (to avoid breakage in the dev version when cache
548 * structure changes) */
549 i_file_size = CACHE_SUBVERSION_NUM;
550 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
554 i_file_size = ftell( file );
555 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
558 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
561 for (unsigned i = 0; i < i_cache; i++)
563 module_t *module = cache[i].p_module;
564 uint32_t i_submodule;
566 /* Save additional infos */
567 SAVE_STRING(module->psz_shortname);
568 SAVE_STRING(module->psz_longname);
569 SAVE_STRING(module->psz_help);
570 SAVE_IMMEDIATE(module->i_shortcuts);
571 for (unsigned j = 0; j < module->i_shortcuts; j++)
572 SAVE_STRING(module->pp_shortcuts[j]);
574 SAVE_STRING(module->psz_capability);
575 SAVE_IMMEDIATE(module->i_score);
576 SAVE_IMMEDIATE(module->b_unloadable);
579 if (CacheSaveModuleConfig (file, module))
582 SAVE_STRING(module->domain);
584 i_submodule = module->submodule_count;
585 SAVE_IMMEDIATE( i_submodule );
586 if (CacheSaveSubmodule (file, module->submodule))
589 /* Save common info */
590 SAVE_STRING(cache[i].path);
591 SAVE_IMMEDIATE(cache[i].mtime);
592 SAVE_IMMEDIATE(cache[i].size);
595 if (fflush (file)) /* flush libc buffers */
597 return 0; /* success! */
603 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
607 if( CacheSaveSubmodule( file, p_module->next ) )
610 SAVE_STRING( p_module->psz_shortname );
611 SAVE_STRING( p_module->psz_longname );
612 SAVE_IMMEDIATE( p_module->i_shortcuts );
613 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
614 SAVE_STRING( p_module->pp_shortcuts[j] );
616 SAVE_STRING( p_module->psz_capability );
617 SAVE_IMMEDIATE( p_module->i_score );
624 /*****************************************************************************
625 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
626 *****************************************************************************/
627 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
631 p_cache->pf_activate = p_module->pf_activate;
632 p_cache->pf_deactivate = p_module->pf_deactivate;
633 p_cache->handle = p_module->handle;
635 /* FIXME: This looks too simplistic an algorithm to me. What if the module
636 * file was altered such that the number of order of submodules was
637 * altered... after VLC started -- Courmisch, 09/2008 */
638 module_t *p_child = p_module->submodule,
639 *p_cchild = p_cache->submodule;
640 while( p_child && p_cchild )
642 p_cchild->pf_activate = p_child->pf_activate;
643 p_cchild->pf_deactivate = p_child->pf_deactivate;
644 p_child = p_child->next;
645 p_cchild = p_cchild->next;
648 p_cache->b_loaded = true;
649 p_module->b_loaded = false;
653 * Looks up a plugin file in a table of cached plugins.
655 module_t *CacheFind (module_cache_t *cache, size_t count,
656 const char *path, const struct stat *st)
660 if (cache->path != NULL
661 && !strcmp (cache->path, path)
662 && cache->mtime == st->st_mtime
663 && cache->size == st->st_size)
665 module_t *module = cache->p_module;
666 cache->p_module = NULL;
676 /** Adds entry to the cache */
677 int CacheAdd (module_cache_t **cachep, size_t *countp,
678 const char *path, const struct stat *st, module_t *module)
680 module_cache_t *cache = *cachep;
681 const size_t count = *countp;
683 cache = realloc (cache, (count + 1) * sizeof (*cache));
684 if (unlikely(cache == NULL))
689 /* NOTE: strdup() could be avoided, but it would be a bit ugly */
690 cache->path = strdup (path);
691 cache->mtime = st->st_mtime;
692 cache->size = st->st_size;
693 cache->p_module = module;
698 #endif /* HAVE_DYNAMIC_PLUGINS */