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