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