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