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 15
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 **pp_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();
204 /* Load additional infos */
205 free (module->psz_object_name);
206 LOAD_STRING(module->psz_object_name);
207 LOAD_STRING(module->psz_shortname);
208 LOAD_STRING(module->psz_longname);
209 LOAD_STRING(module->psz_help);
211 LOAD_IMMEDIATE(module->i_shortcuts);
212 if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
214 else if (module->i_shortcuts == 0)
215 module->pp_shortcuts = NULL;
218 module->pp_shortcuts =
219 xmalloc (sizeof (char **) * module->i_shortcuts);
220 for (unsigned j = 0; j < module->i_shortcuts; j++)
221 LOAD_STRING(module->pp_shortcuts[j]);
224 LOAD_STRING(module->psz_capability);
225 LOAD_IMMEDIATE(module->i_score);
226 LOAD_IMMEDIATE(module->b_unloadable);
229 if (CacheLoadConfig (module, file) != VLC_SUCCESS)
232 LOAD_STRING(module->psz_filename);
233 LOAD_STRING(module->domain);
234 if (module->domain != NULL)
235 vlc_bindtextdomain (module->domain);
237 LOAD_IMMEDIATE( i_submodules );
239 while( i_submodules-- )
241 module_t *submodule = vlc_submodule_create (module);
242 free (submodule->psz_object_name);
243 free (submodule->pp_shortcuts);
244 LOAD_STRING(submodule->psz_object_name);
245 LOAD_STRING(submodule->psz_shortname);
246 LOAD_STRING(submodule->psz_longname);
247 LOAD_STRING(submodule->psz_help);
249 LOAD_IMMEDIATE(submodule->i_shortcuts);
250 if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
252 else if (submodule->i_shortcuts == 0)
253 submodule->pp_shortcuts = NULL;
256 submodule->pp_shortcuts =
257 xmalloc (sizeof (char **) * submodule->i_shortcuts);
258 for (unsigned j = 0; j < submodule->i_shortcuts; j++)
259 LOAD_STRING(submodule->pp_shortcuts[j]);
262 LOAD_STRING(submodule->psz_capability);
263 LOAD_IMMEDIATE(submodule->i_score);
264 LOAD_IMMEDIATE(submodule->b_unloadable);
265 LOAD_STRING(submodule->domain);
271 /* Load common info */
273 LOAD_IMMEDIATE(st.st_mtime);
274 LOAD_IMMEDIATE(st.st_size);
276 CacheAdd (&pp_cache, &count, path, &st, module);
278 /* TODO: deal with errors */
286 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
294 static int CacheLoadConfig( module_t *p_module, FILE *file )
299 /* Calculate the structure length */
300 LOAD_IMMEDIATE( p_module->i_config_items );
301 LOAD_IMMEDIATE( p_module->i_bool_items );
303 LOAD_IMMEDIATE( i_lines );
305 /* Allocate memory */
309 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
310 if( p_module->p_config == NULL )
312 p_module->confsize = 0;
316 p_module->confsize = i_lines;
318 /* Do the duplication job */
319 for (size_t i = 0; i < i_lines; i++ )
321 LOAD_IMMEDIATE( p_module->p_config[i] );
323 LOAD_STRING( p_module->p_config[i].psz_type );
324 LOAD_STRING( p_module->p_config[i].psz_name );
325 LOAD_STRING( p_module->p_config[i].psz_text );
326 LOAD_STRING( p_module->p_config[i].psz_longtext );
327 LOAD_STRING( p_module->p_config[i].psz_oldname );
329 if (IsConfigStringType (p_module->p_config[i].i_type))
331 LOAD_STRING (p_module->p_config[i].orig.psz);
332 p_module->p_config[i].value.psz =
333 (p_module->p_config[i].orig.psz != NULL)
334 ? strdup (p_module->p_config[i].orig.psz) : NULL;
337 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
338 sizeof (p_module->p_config[i].value));
340 p_module->p_config[i].b_dirty = false;
342 if( p_module->p_config[i].i_list )
344 if( p_module->p_config[i].ppsz_list )
347 p_module->p_config[i].ppsz_list =
348 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
349 if( p_module->p_config[i].ppsz_list )
351 for( j = 0; j < p_module->p_config[i].i_list; j++ )
352 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
353 p_module->p_config[i].ppsz_list[j] = NULL;
356 if( p_module->p_config[i].ppsz_list_text )
359 p_module->p_config[i].ppsz_list_text =
360 xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
361 if( p_module->p_config[i].ppsz_list_text )
363 for( j = 0; j < p_module->p_config[i].i_list; j++ )
364 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
365 p_module->p_config[i].ppsz_list_text[j] = NULL;
368 if( p_module->p_config[i].pi_list )
370 p_module->p_config[i].pi_list =
371 xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
372 if( p_module->p_config[i].pi_list )
374 for (int j = 0; j < p_module->p_config[i].i_list; j++)
375 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
380 if( p_module->p_config[i].i_action )
382 p_module->p_config[i].ppf_action =
383 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
384 p_module->p_config[i].ppsz_action_text =
385 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
387 for (int j = 0; j < p_module->p_config[i].i_action; j++)
389 p_module->p_config[i].ppf_action[j] = NULL;
390 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
402 static int CacheSaveBank( FILE *file, module_cache_t *const *, size_t );
405 * Saves a module cache to disk, and release cache data from memory.
407 void CacheSave (vlc_object_t *p_this, const char *dir,
408 module_cache_t **entries, size_t n)
410 char *filename = NULL, *tmpname = NULL;
412 if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
415 if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
417 msg_Dbg (p_this, "saving plugins cache %s", filename);
419 FILE *file = vlc_fopen (tmpname, "wb");
422 if (errno != EACCES && errno != ENOENT)
423 msg_Warn (p_this, "cannot create %s (%m)", tmpname);
427 if (CacheSaveBank (file, entries, n))
429 msg_Warn (p_this, "cannot write %s (%m)", tmpname);
432 vlc_unlink (tmpname);
436 #if !defined( WIN32 ) && !defined( __OS2__ )
437 vlc_rename (tmpname, filename); /* atomically replace old cache */
440 vlc_unlink (filename);
442 vlc_rename (tmpname, filename);
448 for (size_t i = 0; i < n; i++)
450 free (entries[i]->path);
456 static int CacheSaveConfig (FILE *, const module_t *);
457 static int CacheSaveSubmodule (FILE *, const module_t *);
459 static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
462 uint32_t i_file_size = 0;
464 /* Contains version number */
465 if (fputs (CACHE_STRING, file) == EOF)
467 #ifdef DISTRO_VERSION
468 /* Allow binary maintaner to pass a string to detect new binary version*/
469 if (fputs( DISTRO_VERSION, file ) == EOF)
472 /* Sub-version number (to avoid breakage in the dev version when cache
473 * structure changes) */
474 i_file_size = CACHE_SUBVERSION_NUM;
475 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
479 i_file_size = ftell( file );
480 if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
483 if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
486 #define SAVE_IMMEDIATE( a ) \
487 if (fwrite (&a, sizeof(a), 1, file) != 1) \
489 #define SAVE_STRING( a ) \
491 uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
492 if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
493 || (a && (fwrite (a, 1, i_size, file) != i_size))) \
497 for (unsigned i = 0; i < i_cache; i++)
499 uint32_t i_submodule;
501 /* Save additional infos */
502 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
503 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
504 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
505 SAVE_STRING( pp_cache[i]->p_module->psz_help );
506 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
507 for (unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++)
508 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
510 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
511 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
512 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
515 if (CacheSaveConfig (file, pp_cache[i]->p_module))
518 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
519 SAVE_STRING( pp_cache[i]->p_module->domain );
521 i_submodule = pp_cache[i]->p_module->submodule_count;
522 SAVE_IMMEDIATE( i_submodule );
523 if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
526 /* Save common info */
527 SAVE_STRING(pp_cache[i]->path);
528 SAVE_IMMEDIATE(pp_cache[i]->mtime);
529 SAVE_IMMEDIATE(pp_cache[i]->size);
532 if (fflush (file)) /* flush libc buffers */
534 return 0; /* success! */
540 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
544 if( CacheSaveSubmodule( file, p_module->next ) )
547 SAVE_STRING( p_module->psz_object_name );
548 SAVE_STRING( p_module->psz_shortname );
549 SAVE_STRING( p_module->psz_longname );
550 SAVE_STRING( p_module->psz_help );
551 SAVE_IMMEDIATE( p_module->i_shortcuts );
552 for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
553 SAVE_STRING( p_module->pp_shortcuts[j] );
555 SAVE_STRING( p_module->psz_capability );
556 SAVE_IMMEDIATE( p_module->i_score );
557 SAVE_IMMEDIATE( p_module->b_unloadable );
558 SAVE_STRING( p_module->domain );
566 static int CacheSaveConfig (FILE *file, const module_t *p_module)
568 uint32_t i_lines = p_module->confsize;
570 SAVE_IMMEDIATE( p_module->i_config_items );
571 SAVE_IMMEDIATE( p_module->i_bool_items );
572 SAVE_IMMEDIATE( i_lines );
574 for (size_t i = 0; i < i_lines ; i++)
576 SAVE_IMMEDIATE( p_module->p_config[i] );
578 SAVE_STRING( p_module->p_config[i].psz_type );
579 SAVE_STRING( p_module->p_config[i].psz_name );
580 SAVE_STRING( p_module->p_config[i].psz_text );
581 SAVE_STRING( p_module->p_config[i].psz_longtext );
582 SAVE_STRING( p_module->p_config[i].psz_oldname );
584 if (IsConfigStringType (p_module->p_config[i].i_type))
585 SAVE_STRING( p_module->p_config[i].orig.psz );
587 if( p_module->p_config[i].i_list )
589 if( p_module->p_config[i].ppsz_list )
591 for (int j = 0; j < p_module->p_config[i].i_list; j++)
592 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
595 if( p_module->p_config[i].ppsz_list_text )
597 for (int j = 0; j < p_module->p_config[i].i_list; j++)
598 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
600 if( p_module->p_config[i].pi_list )
602 for (int j = 0; j < p_module->p_config[i].i_list; j++)
603 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
607 for (int j = 0; j < p_module->p_config[i].i_action; j++)
608 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
616 /*****************************************************************************
617 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
618 *****************************************************************************/
619 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
623 p_cache->pf_activate = p_module->pf_activate;
624 p_cache->pf_deactivate = p_module->pf_deactivate;
625 p_cache->handle = p_module->handle;
627 /* FIXME: This looks too simplistic an algorithm to me. What if the module
628 * file was altered such that the number of order of submodules was
629 * altered... after VLC started -- Courmisch, 09/2008 */
630 module_t *p_child = p_module->submodule,
631 *p_cchild = p_cache->submodule;
632 while( p_child && p_cchild )
634 p_cchild->pf_activate = p_child->pf_activate;
635 p_cchild->pf_deactivate = p_child->pf_deactivate;
636 p_child = p_child->next;
637 p_cchild = p_cchild->next;
640 p_cache->b_loaded = true;
641 p_module->b_loaded = false;
645 * Looks up a plugin file in a table of cached plugins.
647 module_t *CacheFind (module_cache_t *const *entries, size_t count,
648 const char *path, const struct stat *st)
652 module_cache_t *entry = *(entries++);
654 if (!strcmp (entry->path, path)
655 && entry->mtime == st->st_mtime
656 && entry->size == st->st_size)
658 module_t *module = entry->p_module;
659 entry->p_module = NULL; /* Return NULL next time */
668 /** Adds entry to the cache */
669 int CacheAdd (module_cache_t ***cache, size_t *count,
670 const char *path, const struct stat *st, module_t *module)
672 module_cache_t **entries;
674 entries = realloc (*cache, (*count + 1) * sizeof (*entries));
675 if (unlikely(entries == NULL))
679 entries[*count] = malloc (sizeof (**entries));
680 if (unlikely(entries[*count] == NULL))
682 /* NOTE: strdup() could be avoided, but it would be a bit ugly */
683 entries[*count]->path = strdup (path);
684 entries[*count]->mtime = st->st_mtime;
685 entries[*count]->size = st->st_size;
686 entries[*count]->p_module = module;
691 #endif /* HAVE_DYNAMIC_PLUGINS */