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