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