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