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