]> git.sesse.net Git - vlc/blob - src/modules/cache.c
3f16b25cd9068b93c2431ddf1bc69056ebba97c5
[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 <assert.h>
35 #include <stdlib.h>                                      /* free(), strtol() */
36 #include <stdio.h>                                              /* sprintf() */
37 #include <string.h>                                              /* strdup() */
38 #include <vlc_plugin.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 5
89
90 /* Format string for the cache filename */
91 #define CACHENAME_FORMAT \
92     "plugins-%.2zx%.2zx%.2"PRIx8".dat"
93 /* Magic for the cache filename */
94 #define CACHENAME_VALUES \
95     sizeof(int), sizeof(void *), *(uint8_t *)&(uint16_t){ 0xbe1e }
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     {
233         pp_cache = p_bank->pp_loaded_cache =
234                    malloc( i_cache * sizeof(void *) );
235         assert( pp_cache );
236     }
237
238 #define LOAD_IMMEDIATE(a) \
239     if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
240 #define LOAD_STRING(a) \
241 { \
242     a = NULL; \
243     if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
244      || ( i_size > 16384 ) ) \
245         goto error; \
246     if( i_size ) { \
247         char *psz = malloc( i_size ); \
248         if( fread( psz, i_size, 1, file ) != 1 ) { \
249             free( psz ); \
250             goto error; \
251         } \
252         if( psz[i_size-1] ) { \
253             free( psz ); \
254             goto error; \
255         } \
256         a = psz; \
257     } \
258 }
259
260     for( i = 0; i < i_cache; i++ )
261     {
262         uint16_t i_size;
263         int i_submodules;
264
265         pp_cache[i] = malloc( sizeof(module_cache_t) );
266         assert( pp_cache[i] );
267         p_bank->i_loaded_cache++;
268
269         /* Load common info */
270         LOAD_STRING( pp_cache[i]->psz_file );
271         LOAD_IMMEDIATE( pp_cache[i]->i_time );
272         LOAD_IMMEDIATE( pp_cache[i]->i_size );
273         LOAD_IMMEDIATE( pp_cache[i]->b_junk );
274         pp_cache[i]->b_used = false;
275
276         if( pp_cache[i]->b_junk ) continue;
277
278         pp_cache[i]->p_module = vlc_module_create( p_this );
279
280         /* Load additional infos */
281         free( pp_cache[i]->p_module->psz_object_name );
282         LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
283         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
284         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
285         LOAD_STRING( pp_cache[i]->p_module->psz_help );
286         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
287         {
288             LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
289         }
290         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
291         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
292         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
293         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
294         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
295         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
296
297         /* Config stuff */
298         if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
299             goto error;
300
301         LOAD_STRING( pp_cache[i]->p_module->psz_filename );
302
303         LOAD_IMMEDIATE( i_submodules );
304
305         while( i_submodules-- )
306         {
307             module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
308             free( p_module->psz_object_name );
309             LOAD_STRING( p_module->psz_object_name );
310             LOAD_STRING( p_module->psz_shortname );
311             LOAD_STRING( p_module->psz_longname );
312             LOAD_STRING( p_module->psz_help );
313             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
314             {
315                 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
316             }
317             LOAD_STRING( p_module->psz_capability );
318             LOAD_IMMEDIATE( p_module->i_score );
319             LOAD_IMMEDIATE( p_module->i_cpu );
320             LOAD_IMMEDIATE( p_module->b_unloadable );
321             LOAD_IMMEDIATE( p_module->b_reentrant );
322         }
323     }
324
325     fclose( file );
326     return;
327
328  error:
329
330     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
331
332     /* TODO: cleanup */
333     p_bank->i_loaded_cache = 0;
334
335     fclose( file );
336     return;
337 }
338
339
340 static int CacheLoadConfig( module_t *p_module, FILE *file )
341 {
342     uint32_t i_lines;
343     uint16_t i_size;
344
345     /* Calculate the structure length */
346     LOAD_IMMEDIATE( p_module->i_config_items );
347     LOAD_IMMEDIATE( p_module->i_bool_items );
348
349     LOAD_IMMEDIATE( i_lines );
350
351     /* Allocate memory */
352     if (i_lines)
353     {
354         p_module->p_config =
355             (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
356         if( p_module->p_config == NULL )
357         {
358             p_module->confsize = 0;
359             return VLC_ENOMEM;
360         }
361     }
362     p_module->confsize = i_lines;
363
364     /* Do the duplication job */
365     for (size_t i = 0; i < i_lines; i++ )
366     {
367         LOAD_IMMEDIATE( p_module->p_config[i] );
368
369         LOAD_STRING( p_module->p_config[i].psz_type );
370         LOAD_STRING( p_module->p_config[i].psz_name );
371         LOAD_STRING( p_module->p_config[i].psz_text );
372         LOAD_STRING( p_module->p_config[i].psz_longtext );
373         LOAD_STRING( p_module->p_config[i].psz_oldname );
374         LOAD_IMMEDIATE( p_module->p_config[i].b_removed );
375
376         if (IsConfigStringType (p_module->p_config[i].i_type))
377         {
378             LOAD_STRING (p_module->p_config[i].orig.psz);
379             p_module->p_config[i].value.psz =
380                     (p_module->p_config[i].orig.psz != NULL)
381                         ? strdup (p_module->p_config[i].orig.psz) : NULL;
382             p_module->p_config[i].saved.psz = NULL;
383         }
384         else
385         {
386             memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
387                     sizeof (p_module->p_config[i].value));
388             memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
389                     sizeof (p_module->p_config[i].saved));
390         }
391
392         p_module->p_config[i].b_dirty = false;
393
394         p_module->p_config[i].p_lock = &p_module->lock;
395
396         if( p_module->p_config[i].i_list )
397         {
398             if( p_module->p_config[i].ppsz_list )
399             {
400                 int j;
401                 p_module->p_config[i].ppsz_list =
402                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
403                 if( p_module->p_config[i].ppsz_list )
404                 {
405                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
406                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
407                     p_module->p_config[i].ppsz_list[j] = NULL;
408                 }
409             }
410             if( p_module->p_config[i].ppsz_list_text )
411             {
412                 int j;
413                 p_module->p_config[i].ppsz_list_text =
414                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
415                 if( p_module->p_config[i].ppsz_list_text )
416                 {
417                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
418                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
419                   p_module->p_config[i].ppsz_list_text[j] = NULL;
420                 }
421             }
422             if( p_module->p_config[i].pi_list )
423             {
424                 p_module->p_config[i].pi_list =
425                     malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
426                 if( p_module->p_config[i].pi_list )
427                 {
428                     for (int j = 0; j < p_module->p_config[i].i_list; j++)
429                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
430                 }
431             }
432         }
433
434         if( p_module->p_config[i].i_action )
435         {
436             p_module->p_config[i].ppf_action =
437                 malloc( p_module->p_config[i].i_action * sizeof(void *) );
438             assert( p_module->p_config[i].ppf_action );
439             p_module->p_config[i].ppsz_action_text =
440                 malloc( p_module->p_config[i].i_action * sizeof(char *) );
441             assert( p_module->p_config[i].ppsz_action_text );
442
443             for (int j = 0; j < p_module->p_config[i].i_action; j++)
444             {
445                 p_module->p_config[i].ppf_action[j] = 0;
446                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
447             }
448         }
449
450         LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
451     }
452
453     return VLC_SUCCESS;
454
455  error:
456
457     return VLC_EGENERIC;
458 }
459
460 static int CacheSaveSubmodule( FILE *file, module_t *p_module );
461
462 /*****************************************************************************
463  * SavePluginsCache: saves the plugins cache to a file
464  *****************************************************************************/
465 void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
466 {
467     static char const psz_tag[] =
468         "Signature: 8a477f597d28d172789f06886806bc55\r\n"
469         "# This file is a cache directory tag created by VLC.\r\n"
470         "# For information about cache directory tags, see:\r\n"
471         "#   http://www.brynosaurus.com/cachedir/\r\n";
472
473     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
474     FILE *file;
475     int i, j, i_cache;
476     module_cache_t **pp_cache;
477     uint32_t i_file_size = 0;
478
479     if( !psz_cachedir ) /* XXX: this should never happen */
480     {
481         msg_Err( p_this, "unable to get cache directory" );
482         return;
483     }
484
485     char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
486     config_CreateDir( p_this, psz_cachedir );
487
488     snprintf( psz_filename, sizeof( psz_filename ),
489               "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
490     file = utf8_fopen( psz_filename, "wb" );
491     if (file != NULL)
492     {
493         if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1)
494             clearerr (file); /* what else can we do? */
495         fclose( file );
496     }
497
498     snprintf( psz_filename, sizeof( psz_filename ),
499               "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
500               CACHENAME_VALUES );
501     free( psz_cachedir );
502     msg_Dbg( p_this, "writing plugins cache %s", psz_filename );
503
504     char psz_tmpname[sizeof (psz_filename) + 12];
505     snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
506               (uint32_t)getpid ());
507     file = utf8_fopen( psz_tmpname, "wb" );
508     if (file == NULL)
509         goto error;
510
511     /* Empty space for file size */
512     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
513         goto error;
514
515     /* Contains version number */
516     if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
517         goto error;
518 #ifdef DISTRO_VERSION
519     /* Allow binary maintaner to pass a string to detect new binary version*/
520     if (fputs( DISTRO_VERSION, file ) == EOF)
521         goto error;
522 #endif
523     /* Sub-version number (to avoid breakage in the dev version when cache
524      * structure changes) */
525     i_file_size = CACHE_SUBVERSION_NUM;
526     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
527         goto error;
528
529     /* Header marker */
530     i_file_size = ftell( file );
531     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
532         goto error;
533
534     i_cache = p_bank->i_cache;
535     pp_cache = p_bank->pp_cache;
536
537     if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
538         goto error;
539
540 #define SAVE_IMMEDIATE( a ) \
541     if (fwrite (&a, sizeof(a), 1, file) != 1) \
542         goto error
543 #define SAVE_STRING( a ) \
544     { \
545         uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
546         if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
547          || (a && (fwrite (a, 1, i_size, file) != i_size))) \
548             goto error; \
549     } while(0)
550
551     for( i = 0; i < i_cache; i++ )
552     {
553         uint32_t i_submodule;
554
555         /* Save common info */
556         SAVE_STRING( pp_cache[i]->psz_file );
557         SAVE_IMMEDIATE( pp_cache[i]->i_time );
558         SAVE_IMMEDIATE( pp_cache[i]->i_size );
559         SAVE_IMMEDIATE( pp_cache[i]->b_junk );
560
561         if( pp_cache[i]->b_junk ) continue;
562
563         /* Save additional infos */
564         SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
565         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
566         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
567         SAVE_STRING( pp_cache[i]->p_module->psz_help );
568         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
569         {
570             SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
571         }
572         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
573         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
574         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
575         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
576         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
577         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
578
579         /* Config stuff */
580         if (CacheSaveConfig (pp_cache[i]->p_module, file))
581             goto error;
582
583         SAVE_STRING( pp_cache[i]->p_module->psz_filename );
584
585         i_submodule = pp_cache[i]->p_module->submodule_count;
586         SAVE_IMMEDIATE( i_submodule );
587         if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
588             goto error;
589     }
590
591     /* Fill-up file size */
592     i_file_size = ftell( file );
593     fseek( file, 0, SEEK_SET );
594     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1
595      || fflush (file)) /* flush libc buffers */
596         goto error;
597
598 #ifndef WIN32
599     utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
600     fclose (file);
601 #else
602     utf8_unlink (psz_filename);
603     fclose (file);
604     utf8_rename (psz_tmpname, psz_filename);
605 #endif
606     return; /* success! */
607
608 error:
609     msg_Warn (p_this, "could not write plugins cache %s (%m)",
610               psz_filename);
611     if (file != NULL)
612     {
613         clearerr (file);
614         fclose (file);
615     }
616 }
617
618 static int CacheSaveSubmodule( FILE *file, module_t *p_module )
619 {
620     if( !p_module )
621         return 0;
622     if( CacheSaveSubmodule( file, p_module->next ) )
623         goto error;
624
625     SAVE_STRING( p_module->psz_object_name );
626     SAVE_STRING( p_module->psz_shortname );
627     SAVE_STRING( p_module->psz_longname );
628     SAVE_STRING( p_module->psz_help );
629     for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
630          SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
631
632     SAVE_STRING( p_module->psz_capability );
633     SAVE_IMMEDIATE( p_module->i_score );
634     SAVE_IMMEDIATE( p_module->i_cpu );
635     SAVE_IMMEDIATE( p_module->b_unloadable );
636     SAVE_IMMEDIATE( p_module->b_reentrant );
637     return 0;
638
639 error:
640     return -1;
641 }
642
643
644 static int CacheSaveConfig( module_t *p_module, FILE *file )
645 {
646     uint32_t i_lines = p_module->confsize;
647
648     SAVE_IMMEDIATE( p_module->i_config_items );
649     SAVE_IMMEDIATE( p_module->i_bool_items );
650     SAVE_IMMEDIATE( i_lines );
651
652     for (size_t i = 0; i < i_lines ; i++)
653     {
654         SAVE_IMMEDIATE( p_module->p_config[i] );
655
656         SAVE_STRING( p_module->p_config[i].psz_type );
657         SAVE_STRING( p_module->p_config[i].psz_name );
658         SAVE_STRING( p_module->p_config[i].psz_text );
659         SAVE_STRING( p_module->p_config[i].psz_longtext );
660         SAVE_STRING( p_module->p_config[i].psz_oldname );
661         SAVE_IMMEDIATE( p_module->p_config[i].b_removed );
662
663         if (IsConfigStringType (p_module->p_config[i].i_type))
664             SAVE_STRING( p_module->p_config[i].orig.psz );
665
666         if( p_module->p_config[i].i_list )
667         {
668             if( p_module->p_config[i].ppsz_list )
669             {
670                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
671                     SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
672             }
673
674             if( p_module->p_config[i].ppsz_list_text )
675             {
676                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
677                     SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
678             }
679             if( p_module->p_config[i].pi_list )
680             {
681                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
682                     SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
683             }
684         }
685
686         for (int j = 0; j < p_module->p_config[i].i_action; j++)
687             SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
688
689         SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
690     }
691     return 0;
692
693 error:
694     return -1;
695 }
696
697 /*****************************************************************************
698  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
699  *****************************************************************************/
700 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
701 {
702     (void)p_this;
703
704     p_cache->pf_activate = p_module->pf_activate;
705     p_cache->pf_deactivate = p_module->pf_deactivate;
706     p_cache->handle = p_module->handle;
707
708     /* FIXME: This looks too simplistic an algorithm to me. What if the module
709      * file was altered such that the number of order of submodules was
710      * altered... after VLC started -- Courmisch, 09/2008 */
711     module_t *p_child = p_module->submodule,
712              *p_cchild = p_cache->submodule;
713     while( p_child && p_cchild )
714     {
715         p_cchild->pf_activate = p_child->pf_activate;
716         p_cchild->pf_deactivate = p_child->pf_deactivate;
717         p_child = p_child->next;
718         p_cchild = p_cchild->next;
719     }
720
721     p_cache->b_loaded = true;
722     p_module->b_loaded = false;
723 }
724
725 /*****************************************************************************
726  * CacheFind: finds the cache entry corresponding to a file
727  *****************************************************************************/
728 module_cache_t *CacheFind( module_bank_t *p_bank, const char *psz_file,
729                            int64_t i_time, int64_t i_size )
730 {
731     module_cache_t **pp_cache;
732     int i_cache, i;
733
734     pp_cache = p_bank->pp_loaded_cache;
735     i_cache = p_bank->i_loaded_cache;
736
737     for( i = 0; i < i_cache; i++ )
738     {
739         if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
740             pp_cache[i]->i_time == i_time &&
741             pp_cache[i]->i_size == i_size ) return pp_cache[i];
742     }
743
744     return NULL;
745 }
746
747 #endif /* HAVE_DYNAMIC_PLUGINS */