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 23
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 */
217 static module_t *CacheLoadModule (FILE *file)
219 module_t *module = vlc_module_create (NULL);
221 /* Load additional infos */
222 LOAD_STRING(module->psz_shortname);
223 LOAD_STRING(module->psz_longname);
224 LOAD_STRING(module->psz_help);
226 LOAD_IMMEDIATE(module->i_shortcuts);
227 if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
231 module->pp_shortcuts =
232 xmalloc (sizeof (*module->pp_shortcuts) * module->i_shortcuts);
233 for (unsigned j = 0; j < module->i_shortcuts; j++)
234 LOAD_STRING(module->pp_shortcuts[j]);
237 LOAD_STRING(module->psz_capability);
238 LOAD_IMMEDIATE(module->i_score);
239 LOAD_IMMEDIATE(module->b_unloadable);
242 if (CacheLoadModuleConfig (module, file) != VLC_SUCCESS)
245 LOAD_STRING(module->domain);
246 if (module->domain != NULL)
247 vlc_bindtextdomain (module->domain);
250 LOAD_IMMEDIATE(submodules);
252 for (; submodules > 0; submodules--)
254 module_t *submodule = vlc_module_create (module);
256 free (submodule->pp_shortcuts);
257 LOAD_STRING(submodule->psz_shortname);
258 LOAD_STRING(submodule->psz_longname);
260 LOAD_IMMEDIATE(submodule->i_shortcuts);
261 if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
265 submodule->pp_shortcuts =
266 xmalloc (sizeof (*submodule->pp_shortcuts) * submodule->i_shortcuts);
267 for (unsigned j = 0; j < submodule->i_shortcuts; j++)
268 LOAD_STRING(submodule->pp_shortcuts[j]);
271 LOAD_STRING(submodule->psz_capability);
272 LOAD_IMMEDIATE(submodule->i_score);
277 return NULL; /* FIXME: leaks */
281 * Loads a plugins cache file.
283 * This function will load the plugin cache if present and valid. This cache
284 * will in turn be queried by AllocateAllPlugins() to see if it needs to
285 * actually load the dynamically loadable module.
286 * This allows us to only fully load plugins when they are actually used.
288 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
293 char p_cachestring[sizeof(CACHE_STRING)];
296 assert( dir != NULL );
299 if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
302 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
304 file = vlc_fopen( psz_filename, "rb" );
307 msg_Warn( p_this, "cannot read %s: %s", psz_filename,
308 vlc_strerror_c(errno) );
309 free( psz_filename );
312 free( psz_filename );
314 /* Check the file is a plugins cache */
315 i_size = sizeof(CACHE_STRING) - 1;
316 i_read = fread( p_cachestring, 1, i_size, file );
317 if( i_read != i_size ||
318 memcmp( p_cachestring, CACHE_STRING, i_size ) )
320 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
325 #ifdef DISTRO_VERSION
326 /* Check for distribution specific version */
327 char p_distrostring[sizeof( DISTRO_VERSION )];
328 i_size = sizeof( DISTRO_VERSION ) - 1;
329 i_read = fread( p_distrostring, 1, i_size, file );
330 if( i_read != i_size ||
331 memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
333 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
339 /* Check sub-version number */
340 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
341 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
343 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
344 "(corrupted header)" );
349 /* Check header marker */
350 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
351 if( i_read != sizeof(i_marker) ||
352 i_marker != ftell( file ) - (int)sizeof(i_marker) )
354 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
355 "(corrupted header)" );
360 module_cache_t *cache = NULL;
365 module_t *module = CacheLoadModule (file);
376 /* Load common info */
380 LOAD_IMMEDIATE(st.st_mtime);
381 LOAD_IMMEDIATE(st.st_size);
383 CacheAdd (&cache, &count, path, &st, module);
385 /* TODO: deal with errors */
395 msg_Err(p_this, "plugins cache read error: %s", vlc_strerror_c(errno));
396 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
403 #define SAVE_IMMEDIATE( a ) \
404 if (fwrite (&(a), sizeof(a), 1, file) != 1) \
406 #define SAVE_FLAG(a) \
412 static int CacheSaveString (FILE *file, const char *str)
414 uint16_t size = (str != NULL) ? strlen (str) : 0;
416 SAVE_IMMEDIATE (size);
417 if (size != 0 && fwrite (str, 1, size, file) != size)
425 #define SAVE_STRING( a ) \
426 if (CacheSaveString (file, (a))) \
429 static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
431 SAVE_IMMEDIATE (cfg->i_type);
432 SAVE_IMMEDIATE (cfg->i_short);
433 SAVE_FLAG (cfg->b_advanced);
434 SAVE_FLAG (cfg->b_internal);
435 SAVE_FLAG (cfg->b_unsaveable);
436 SAVE_FLAG (cfg->b_safe);
437 SAVE_FLAG (cfg->b_removed);
438 SAVE_STRING (cfg->psz_type);
439 SAVE_STRING (cfg->psz_name);
440 SAVE_STRING (cfg->psz_text);
441 SAVE_STRING (cfg->psz_longtext);
442 SAVE_IMMEDIATE (cfg->list_count);
444 if (IsConfigStringType (cfg->i_type))
446 SAVE_STRING (cfg->orig.psz);
447 if (cfg->list_count == 0)
448 SAVE_IMMEDIATE (cfg->list.psz_cb); /* XXX: see CacheLoadConfig() */
449 for (unsigned i = 0; i < cfg->list_count; i++)
450 SAVE_STRING (cfg->list.psz[i]);
454 SAVE_IMMEDIATE (cfg->orig);
455 SAVE_IMMEDIATE (cfg->min);
456 SAVE_IMMEDIATE (cfg->max);
457 if (cfg->list_count == 0)
458 SAVE_IMMEDIATE (cfg->list.i_cb); /* XXX: see CacheLoadConfig() */
459 for (unsigned i = 0; i < cfg->list_count; i++)
460 SAVE_IMMEDIATE (cfg->list.i[i]);
462 for (unsigned i = 0; i < cfg->list_count; i++)
463 SAVE_STRING (cfg->list_text[i]);
470 static int CacheSaveModuleConfig (FILE *file, const module_t *module)
472 uint16_t lines = module->confsize;
474 SAVE_IMMEDIATE (module->i_config_items);
475 SAVE_IMMEDIATE (module->i_bool_items);
476 SAVE_IMMEDIATE (lines);
478 for (size_t i = 0; i < lines; i++)
479 if (CacheSaveConfig (file, module->p_config + i))
487 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
490 * Saves a module cache to disk, and release cache data from memory.
492 void CacheSave (vlc_object_t *p_this, const char *dir,
493 module_cache_t *entries, size_t n)
495 char *filename = NULL, *tmpname = NULL;
497 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
500 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
502 msg_Dbg (p_this, "saving plugins cache %s", filename);
504 FILE *file = vlc_fopen (tmpname, "wb");
507 if (errno != EACCES && errno != ENOENT)
508 msg_Warn (p_this, "cannot create %s: %s", tmpname,
509 vlc_strerror_c(errno));
513 if (CacheSaveBank (file, entries, n))
515 msg_Warn (p_this, "cannot write %s: %s", tmpname,
516 vlc_strerror_c(errno));
519 vlc_unlink (tmpname);
523 #if !defined( _WIN32 ) && !defined( __OS2__ )
524 vlc_rename (tmpname, filename); /* atomically replace old cache */
527 vlc_unlink (filename);
529 vlc_rename (tmpname, filename);
535 for (size_t i = 0; i < n; i++)
536 free (entries[i].path);
540 static int CacheSaveSubmodule (FILE *, const module_t *);
542 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
545 uint32_t i_file_size = 0;
547 /* Contains version number */
548 if (fputs (CACHE_STRING, file) == EOF)
550 #ifdef DISTRO_VERSION
551 /* Allow binary maintaner to pass a string to detect new binary version*/
552 if (fputs( DISTRO_VERSION, file ) == EOF)
555 /* Sub-version number (to avoid breakage in the dev version when cache
556 * structure changes) */
557 i_file_size = CACHE_SUBVERSION_NUM;
558 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
562 i_file_size = ftell( file );
563 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
566 for (unsigned i = 0; i < i_cache; i++)
568 module_t *module = cache[i].p_module;
569 uint32_t i_submodule;
571 /* Save additional infos */
572 SAVE_STRING(module->psz_shortname);
573 SAVE_STRING(module->psz_longname);
574 SAVE_STRING(module->psz_help);
575 SAVE_IMMEDIATE(module->i_shortcuts);
576 for (unsigned j = 0; j < module->i_shortcuts; j++)
577 SAVE_STRING(module->pp_shortcuts[j]);
579 SAVE_STRING(module->psz_capability);
580 SAVE_IMMEDIATE(module->i_score);
581 SAVE_IMMEDIATE(module->b_unloadable);
584 if (CacheSaveModuleConfig (file, module))
587 SAVE_STRING(module->domain);
589 i_submodule = module->submodule_count;
590 SAVE_IMMEDIATE( i_submodule );
591 if (CacheSaveSubmodule (file, module->submodule))
594 /* Save common info */
595 SAVE_STRING(cache[i].path);
596 SAVE_IMMEDIATE(cache[i].mtime);
597 SAVE_IMMEDIATE(cache[i].size);
600 if (fflush (file)) /* flush libc buffers */
602 return 0; /* success! */
608 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
612 if( CacheSaveSubmodule( file, p_module->next ) )
615 SAVE_STRING( p_module->psz_shortname );
616 SAVE_STRING( p_module->psz_longname );
617 SAVE_IMMEDIATE( p_module->i_shortcuts );
618 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
619 SAVE_STRING( p_module->pp_shortcuts[j] );
621 SAVE_STRING( p_module->psz_capability );
622 SAVE_IMMEDIATE( p_module->i_score );
629 /*****************************************************************************
630 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
631 *****************************************************************************/
632 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
636 p_cache->pf_activate = p_module->pf_activate;
637 p_cache->pf_deactivate = p_module->pf_deactivate;
638 p_cache->handle = p_module->handle;
640 /* FIXME: This looks too simplistic an algorithm to me. What if the module
641 * file was altered such that the number of order of submodules was
642 * altered... after VLC started -- Courmisch, 09/2008 */
643 module_t *p_child = p_module->submodule,
644 *p_cchild = p_cache->submodule;
645 while( p_child && p_cchild )
647 p_cchild->pf_activate = p_child->pf_activate;
648 p_cchild->pf_deactivate = p_child->pf_deactivate;
649 p_child = p_child->next;
650 p_cchild = p_cchild->next;
653 p_cache->b_loaded = true;
654 p_module->b_loaded = false;
658 * Looks up a plugin file in a table of cached plugins.
660 module_t *CacheFind (module_cache_t *cache, size_t count,
661 const char *path, const struct stat *st)
665 if (cache->path != NULL
666 && !strcmp (cache->path, path)
667 && cache->mtime == st->st_mtime
668 && cache->size == st->st_size)
670 module_t *module = cache->p_module;
671 cache->p_module = NULL;
681 /** Adds entry to the cache */
682 int CacheAdd (module_cache_t **cachep, size_t *countp,
683 const char *path, const struct stat *st, module_t *module)
685 module_cache_t *cache = *cachep;
686 const size_t count = *countp;
688 cache = realloc (cache, (count + 1) * sizeof (*cache));
689 if (unlikely(cache == NULL))
694 /* NOTE: strdup() could be avoided, but it would be a bit ugly */
695 cache->path = strdup (path);
696 cache->mtime = st->st_mtime;
697 cache->size = st->st_size;
698 cache->p_module = module;
703 #endif /* HAVE_DYNAMIC_PLUGINS */