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