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