]> git.sesse.net Git - vlc/blob - src/modules/cache.c
882cfc1d25a0441d3554d70cf9ea72bdceb01ae0
[vlc] / src / modules / cache.c
1 /*****************************************************************************
2  * cache.c: Plugins cache
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include "libvlc.h"
33
34 #include <stdlib.h>                                      /* free(), strtol() */
35 #include <stdio.h>                                              /* sprintf() */
36 #include <string.h>                                              /* strdup() */
37 #include <vlc_plugin.h>
38 #include <vlc_cpu.h>
39
40 #include <sys/types.h>
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>
43 #endif
44 #include <assert.h>
45
46 #if !defined(HAVE_DYNAMIC_PLUGINS)
47     /* no support for plugins */
48 #elif defined(HAVE_DL_DYLD)
49 #   if defined(HAVE_MACH_O_DYLD_H)
50 #       include <mach-o/dyld.h>
51 #   endif
52 #elif defined(HAVE_DL_BEOS)
53 #   if defined(HAVE_IMAGE_H)
54 #       include <image.h>
55 #   endif
56 #elif defined(HAVE_DL_WINDOWS)
57 #   include <windows.h>
58 #elif defined(HAVE_DL_DLOPEN)
59 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
60 #       include <dlfcn.h>
61 #   endif
62 #   if defined(HAVE_SYS_DL_H)
63 #       include <sys/dl.h>
64 #   endif
65 #elif defined(HAVE_DL_SHL_LOAD)
66 #   if defined(HAVE_DL_H)
67 #       include <dl.h>
68 #   endif
69 #endif
70
71 #include "config/configuration.h"
72
73 #include "vlc_charset.h"
74
75 #include "modules/modules.h"
76
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 #ifdef HAVE_DYNAMIC_PLUGINS
82 static int    CacheLoadConfig  ( module_t *, FILE * );
83
84 /* Sub-version number
85  * (only used to avoid breakage in dev version when cache structure changes) */
86 #define CACHE_SUBVERSION_NUM 8
87
88 /* Format string for the cache filename */
89 #define CACHENAME_FORMAT \
90     "plugins-%.2zx%.2zx%.2"PRIx8"-%x.dat"
91 /* Magic for the cache filename */
92 #define CACHENAME_VALUES \
93     sizeof(int), sizeof(void *), *(uint8_t *)&(uint16_t){ 0xbe1e }, vlc_CPU()
94
95
96 /*****************************************************************************
97  * LoadPluginsCache: loads the plugins cache file
98  *****************************************************************************
99  * This function will load the plugin cache if present and valid. This cache
100  * will in turn be queried by AllocateAllPlugins() to see if it needs to
101  * actually load the dynamically loadable module.
102  * This allows us to only fully load plugins when they are actually used.
103  *****************************************************************************/
104 void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, bool b_delete )
105 {
106     char *psz_filename, *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
107     FILE *file;
108     int i, j, i_size, i_read;
109     char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
110     int i_cache;
111     module_cache_t **pp_cache = 0;
112     int32_t i_file_size, i_marker;
113
114     if( !psz_cachedir ) /* XXX: this should never happen */
115     {
116         msg_Err( p_this, "Unable to get cache directory" );
117         return;
118     }
119
120     if( asprintf( &psz_filename, "%s"DIR_SEP CACHENAME_FORMAT,
121                   psz_cachedir, CACHENAME_VALUES ) == -1 )
122     {
123         free( psz_cachedir );
124         return;
125     }
126     free( psz_cachedir );
127
128     if( b_delete )
129     {
130 #if !defined( UNDER_CE )
131         unlink( psz_filename );
132 #else
133         wchar_t psz_wf[MAX_PATH];
134         MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
135         DeleteFile( psz_wf );
136 #endif
137         msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
138         free( psz_filename );
139         return;
140     }
141
142     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
143
144     file = utf8_fopen( psz_filename, "rb" );
145     if( !file )
146     {
147         msg_Warn( p_this, "could not open plugins cache file %s for reading",
148                   psz_filename );
149         free( psz_filename );
150         return;
151     }
152     free( psz_filename );
153
154     /* Check the file size */
155     i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
156     if( i_read != sizeof(i_file_size) )
157     {
158         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
159                   "(too short)" );
160         fclose( file );
161         return;
162     }
163
164     fseek( file, 0, SEEK_END );
165     if( ftell( file ) != i_file_size )
166     {
167         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
168                   "(corrupted size)" );
169         fclose( file );
170         return;
171     }
172     fseek( file, sizeof(i_file_size), SEEK_SET );
173
174     /* Check the file is a plugins cache */
175     i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
176     i_read = fread( p_cachestring, 1, i_size, file );
177     if( i_read != i_size ||
178         memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
179     {
180         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
181         fclose( file );
182         return;
183     }
184
185 #ifdef DISTRO_VERSION
186     /* Check for distribution specific version */
187     char p_distrostring[sizeof( DISTRO_VERSION )];
188     i_size = sizeof( DISTRO_VERSION ) - 1;
189     i_read = fread( p_distrostring, 1, i_size, file );
190     if( i_read != i_size ||
191         memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
192     {
193         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
194         fclose( file );
195         return;
196     }
197 #endif
198
199     /* Check Sub-version number */
200     i_read = fread( &i_marker, 1, sizeof(i_marker), file );
201     if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
202     {
203         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
204                   "(corrupted header)" );
205         fclose( file );
206         return;
207     }
208
209     /* Check header marker */
210     i_read = fread( &i_marker, 1, sizeof(i_marker), file );
211     if( i_read != sizeof(i_marker) ||
212         i_marker != ftell( file ) - (int)sizeof(i_marker) )
213     {
214         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
215                   "(corrupted header)" );
216         fclose( file );
217         return;
218     }
219
220     p_bank->i_loaded_cache = 0;
221     if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
222     {
223         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
224                   "(file too short)" );
225         fclose( file );
226         return;
227     }
228
229     if( i_cache )
230         pp_cache = p_bank->pp_loaded_cache =
231                    xmalloc( i_cache * sizeof(void *) );
232
233 #define LOAD_IMMEDIATE(a) \
234     if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
235 #define LOAD_STRING(a) \
236 { \
237     a = NULL; \
238     if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
239      || ( i_size > 16384 ) ) \
240         goto error; \
241     if( i_size ) { \
242         char *psz = xmalloc( i_size ); \
243         if( fread( psz, i_size, 1, file ) != 1 ) { \
244             free( psz ); \
245             goto error; \
246         } \
247         if( psz[i_size-1] ) { \
248             free( psz ); \
249             goto error; \
250         } \
251         a = psz; \
252     } \
253 }
254
255     for( i = 0; i < i_cache; i++ )
256     {
257         uint16_t i_size;
258         int i_submodules;
259
260         pp_cache[i] = xmalloc( sizeof(module_cache_t) );
261         p_bank->i_loaded_cache++;
262
263         /* Load common info */
264         LOAD_STRING( pp_cache[i]->psz_file );
265         LOAD_IMMEDIATE( pp_cache[i]->i_time );
266         LOAD_IMMEDIATE( pp_cache[i]->i_size );
267         LOAD_IMMEDIATE( pp_cache[i]->b_junk );
268         pp_cache[i]->b_used = false;
269
270         if( pp_cache[i]->b_junk ) continue;
271
272         pp_cache[i]->p_module = vlc_module_create( p_this );
273
274         /* Load additional infos */
275         free( pp_cache[i]->p_module->psz_object_name );
276         LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
277         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
278         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
279         LOAD_STRING( pp_cache[i]->p_module->psz_help );
280         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
281         {
282             LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
283         }
284         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
285         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
286         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
287         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
288
289         /* Config stuff */
290         if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
291             goto error;
292
293         LOAD_STRING( pp_cache[i]->p_module->psz_filename );
294
295         LOAD_IMMEDIATE( i_submodules );
296
297         while( i_submodules-- )
298         {
299             module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
300             free( p_module->psz_object_name );
301             LOAD_STRING( p_module->psz_object_name );
302             LOAD_STRING( p_module->psz_shortname );
303             LOAD_STRING( p_module->psz_longname );
304             LOAD_STRING( p_module->psz_help );
305             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
306             {
307                 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
308             }
309             LOAD_STRING( p_module->psz_capability );
310             LOAD_IMMEDIATE( p_module->i_score );
311             LOAD_IMMEDIATE( p_module->b_unloadable );
312         }
313     }
314
315     fclose( file );
316     return;
317
318  error:
319
320     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
321
322     /* TODO: cleanup */
323     p_bank->i_loaded_cache = 0;
324
325     fclose( file );
326     return;
327 }
328
329
330 /* This function should never be called.
331  * It is only used as a non-NULL vlc_callback_t value for comparison. */
332 static int dummy_callback (vlc_object_t *obj, const char *name,
333                            vlc_value_t oldval, vlc_value_t newval, void *data)
334 {
335     (void) obj; (void)name; (void)oldval; (void)newval; (void)data;
336     assert (0);
337 }
338
339
340 static int CacheLoadConfig( module_t *p_module, FILE *file )
341 {
342     uint32_t i_lines;
343     uint16_t i_size;
344
345     /* Calculate the structure length */
346     LOAD_IMMEDIATE( p_module->i_config_items );
347     LOAD_IMMEDIATE( p_module->i_bool_items );
348
349     LOAD_IMMEDIATE( i_lines );
350
351     /* Allocate memory */
352     if (i_lines)
353     {
354         p_module->p_config =
355             (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
356         if( p_module->p_config == NULL )
357         {
358             p_module->confsize = 0;
359             return VLC_ENOMEM;
360         }
361     }
362     p_module->confsize = i_lines;
363
364     /* Do the duplication job */
365     for (size_t i = 0; i < i_lines; i++ )
366     {
367         LOAD_IMMEDIATE( p_module->p_config[i] );
368
369         LOAD_STRING( p_module->p_config[i].psz_type );
370         LOAD_STRING( p_module->p_config[i].psz_name );
371         LOAD_STRING( p_module->p_config[i].psz_text );
372         LOAD_STRING( p_module->p_config[i].psz_longtext );
373         LOAD_STRING( p_module->p_config[i].psz_oldname );
374         LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
375
376         if (IsConfigStringType (p_module->p_config[i].i_type))
377         {
378             LOAD_STRING (p_module->p_config[i].orig.psz);
379             p_module->p_config[i].value.psz =
380                     (p_module->p_config[i].orig.psz != NULL)
381                         ? strdup (p_module->p_config[i].orig.psz) : NULL;
382             p_module->p_config[i].saved.psz = NULL;
383         }
384         else
385         {
386             memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
387                     sizeof (p_module->p_config[i].value));
388             memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
389                     sizeof (p_module->p_config[i].saved));
390         }
391
392         p_module->p_config[i].b_dirty = false;
393
394         if( p_module->p_config[i].i_list )
395         {
396             if( p_module->p_config[i].ppsz_list )
397             {
398                 int j;
399                 p_module->p_config[i].ppsz_list =
400                     xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
401                 if( p_module->p_config[i].ppsz_list )
402                 {
403                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
404                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
405                     p_module->p_config[i].ppsz_list[j] = NULL;
406                 }
407             }
408             if( p_module->p_config[i].ppsz_list_text )
409             {
410                 int j;
411                 p_module->p_config[i].ppsz_list_text =
412                     xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
413                 if( p_module->p_config[i].ppsz_list_text )
414                 {
415                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
416                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
417                   p_module->p_config[i].ppsz_list_text[j] = NULL;
418                 }
419             }
420             if( p_module->p_config[i].pi_list )
421             {
422                 p_module->p_config[i].pi_list =
423                     xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
424                 if( p_module->p_config[i].pi_list )
425                 {
426                     for (int j = 0; j < p_module->p_config[i].i_list; j++)
427                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
428                 }
429             }
430         }
431
432         if( p_module->p_config[i].i_action )
433         {
434             p_module->p_config[i].ppf_action =
435                 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
436             p_module->p_config[i].ppsz_action_text =
437                 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
438
439             for (int j = 0; j < p_module->p_config[i].i_action; j++)
440             {
441                 p_module->p_config[i].ppf_action[j] = NULL;
442                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
443             }
444         }
445
446         bool has_callback;
447         LOAD_IMMEDIATE( has_callback );
448         if (has_callback)
449             p_module->p_config[i].pf_callback = dummy_callback;
450     }
451
452     return VLC_SUCCESS;
453
454  error:
455
456     return VLC_EGENERIC;
457 }
458
459 static int CacheSaveBank( FILE *file, module_bank_t *p_bank );
460
461 /*****************************************************************************
462  * SavePluginsCache: saves the plugins cache to a file
463  *****************************************************************************/
464 void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
465 {
466     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
467     if( !psz_cachedir ) /* XXX: this should never happen */
468     {
469         msg_Err( p_this, "unable to get cache directory" );
470         return;
471     }
472
473     char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
474     config_CreateDir( p_this, psz_cachedir );
475
476     snprintf( psz_filename, sizeof( psz_filename ),
477               "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
478               CACHENAME_VALUES );
479     free( psz_cachedir );
480
481     char psz_tmpname[sizeof (psz_filename) + 12];
482     snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
483               (uint32_t)getpid ());
484     FILE *file = utf8_fopen( psz_tmpname, "wb" );
485     if (file == NULL)
486         goto error;
487
488     msg_Dbg (p_this, "writing plugins cache %s", psz_filename);
489     if (CacheSaveBank (file, p_bank))
490         goto error;
491
492 #ifndef WIN32
493     utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
494     fclose (file);
495 #else
496     utf8_unlink (psz_filename);
497     fclose (file);
498     utf8_rename (psz_tmpname, psz_filename);
499 #endif
500     return; /* success! */
501
502 error:
503     msg_Warn (p_this, "could not write plugins cache %s (%m)",
504               psz_filename);
505     if (file != NULL)
506     {
507         clearerr (file);
508         fclose (file);
509     }
510 }
511
512 static int CacheSaveConfig (FILE *, module_t *);
513 static int CacheSaveSubmodule (FILE *, module_t *);
514
515 static int CacheSaveBank (FILE *file, module_bank_t *p_bank)
516 {
517     uint32_t i_file_size = 0;
518
519     /* Empty space for file size */
520     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
521         goto error;
522
523     /* Contains version number */
524     if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
525         goto error;
526 #ifdef DISTRO_VERSION
527     /* Allow binary maintaner to pass a string to detect new binary version*/
528     if (fputs( DISTRO_VERSION, file ) == EOF)
529         goto error;
530 #endif
531     /* Sub-version number (to avoid breakage in the dev version when cache
532      * structure changes) */
533     i_file_size = CACHE_SUBVERSION_NUM;
534     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
535         goto error;
536
537     /* Header marker */
538     i_file_size = ftell( file );
539     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
540         goto error;
541
542     module_cache_t **pp_cache = p_bank->pp_cache;
543     uint32_t i_cache = p_bank->i_cache;
544
545     if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
546         goto error;
547
548 #define SAVE_IMMEDIATE( a ) \
549     if (fwrite (&a, sizeof(a), 1, file) != 1) \
550         goto error
551 #define SAVE_STRING( a ) \
552     { \
553         uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
554         if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
555          || (a && (fwrite (a, 1, i_size, file) != i_size))) \
556             goto error; \
557     } while(0)
558
559     for (unsigned i = 0; i < i_cache; i++)
560     {
561         uint32_t i_submodule;
562
563         /* Save common info */
564         SAVE_STRING( pp_cache[i]->psz_file );
565         SAVE_IMMEDIATE( pp_cache[i]->i_time );
566         SAVE_IMMEDIATE( pp_cache[i]->i_size );
567         SAVE_IMMEDIATE( pp_cache[i]->b_junk );
568
569         if( pp_cache[i]->b_junk ) continue;
570
571         /* Save additional infos */
572         SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
573         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
574         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
575         SAVE_STRING( pp_cache[i]->p_module->psz_help );
576         for (unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++)
577         {
578             SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
579         }
580         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
581         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
582         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
583         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
584
585         /* Config stuff */
586         if (CacheSaveConfig (file, pp_cache[i]->p_module))
587             goto error;
588
589         SAVE_STRING( pp_cache[i]->p_module->psz_filename );
590
591         i_submodule = pp_cache[i]->p_module->submodule_count;
592         SAVE_IMMEDIATE( i_submodule );
593         if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
594             goto error;
595     }
596
597     /* Fill-up file size */
598     i_file_size = ftell( file );
599     fseek( file, 0, SEEK_SET );
600     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
601      || fflush (file)) /* flush libc buffers */
602         goto error;
603     return 0; /* success! */
604
605 error:
606     return -1;
607 }
608
609 static int CacheSaveSubmodule( FILE *file, module_t *p_module )
610 {
611     if( !p_module )
612         return 0;
613     if( CacheSaveSubmodule( file, p_module->next ) )
614         goto error;
615
616     SAVE_STRING( p_module->psz_object_name );
617     SAVE_STRING( p_module->psz_shortname );
618     SAVE_STRING( p_module->psz_longname );
619     SAVE_STRING( p_module->psz_help );
620     for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
621          SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
622
623     SAVE_STRING( p_module->psz_capability );
624     SAVE_IMMEDIATE( p_module->i_score );
625     SAVE_IMMEDIATE( p_module->b_unloadable );
626     return 0;
627
628 error:
629     return -1;
630 }
631
632
633 static int CacheSaveConfig (FILE *file, module_t *p_module)
634 {
635     uint32_t i_lines = p_module->confsize;
636
637     SAVE_IMMEDIATE( p_module->i_config_items );
638     SAVE_IMMEDIATE( p_module->i_bool_items );
639     SAVE_IMMEDIATE( i_lines );
640
641     for (size_t i = 0; i < i_lines ; i++)
642     {
643         SAVE_IMMEDIATE( p_module->p_config[i] );
644
645         SAVE_STRING( p_module->p_config[i].psz_type );
646         SAVE_STRING( p_module->p_config[i].psz_name );
647         SAVE_STRING( p_module->p_config[i].psz_text );
648         SAVE_STRING( p_module->p_config[i].psz_longtext );
649         SAVE_STRING( p_module->p_config[i].psz_oldname );
650         SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
651
652         if (IsConfigStringType (p_module->p_config[i].i_type))
653             SAVE_STRING( p_module->p_config[i].orig.psz );
654
655         if( p_module->p_config[i].i_list )
656         {
657             if( p_module->p_config[i].ppsz_list )
658             {
659                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
660                     SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
661             }
662
663             if( p_module->p_config[i].ppsz_list_text )
664             {
665                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
666                     SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
667             }
668             if( p_module->p_config[i].pi_list )
669             {
670                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
671                     SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
672             }
673         }
674
675         for (int j = 0; j < p_module->p_config[i].i_action; j++)
676             SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
677
678         bool has_callback = p_module->p_config[i].pf_callback != NULL;
679         SAVE_IMMEDIATE( has_callback );
680     }
681     return 0;
682
683 error:
684     return -1;
685 }
686
687 /*****************************************************************************
688  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
689  *****************************************************************************/
690 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
691 {
692     (void)p_this;
693
694     p_cache->pf_activate = p_module->pf_activate;
695     p_cache->pf_deactivate = p_module->pf_deactivate;
696     p_cache->handle = p_module->handle;
697
698     /* FIXME: This looks too simplistic an algorithm to me. What if the module
699      * file was altered such that the number of order of submodules was
700      * altered... after VLC started -- Courmisch, 09/2008 */
701     module_t *p_child = p_module->submodule,
702              *p_cchild = p_cache->submodule;
703     while( p_child && p_cchild )
704     {
705         p_cchild->pf_activate = p_child->pf_activate;
706         p_cchild->pf_deactivate = p_child->pf_deactivate;
707         p_child = p_child->next;
708         p_cchild = p_cchild->next;
709     }
710
711     p_cache->b_loaded = true;
712     p_module->b_loaded = false;
713 }
714
715 /*****************************************************************************
716  * CacheFind: finds the cache entry corresponding to a file
717  *****************************************************************************/
718 module_cache_t *CacheFind( module_bank_t *p_bank, const char *psz_file,
719                            int64_t i_time, int64_t i_size )
720 {
721     module_cache_t **pp_cache;
722     int i_cache, i;
723
724     pp_cache = p_bank->pp_loaded_cache;
725     i_cache = p_bank->i_loaded_cache;
726
727     for( i = 0; i < i_cache; i++ )
728     {
729         if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
730             pp_cache[i]->i_time == i_time &&
731             pp_cache[i]->i_size == i_size ) return pp_cache[i];
732     }
733
734     return NULL;
735 }
736
737 #endif /* HAVE_DYNAMIC_PLUGINS */