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