]> git.sesse.net Git - vlc/blob - src/modules/cache.c
LGPL
[vlc] / src / modules / cache.c
1 /*****************************************************************************
2  * cache.c: Plugins cache
3  *****************************************************************************
4  * Copyright (C) 2001-2007 VLC authors and VideoLAN
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 it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * 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 18
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
318         if (IsConfigStringType (p_module->p_config[i].i_type))
319         {
320             LOAD_STRING (p_module->p_config[i].orig.psz);
321             p_module->p_config[i].value.psz =
322                     (p_module->p_config[i].orig.psz != NULL)
323                         ? strdup (p_module->p_config[i].orig.psz) : NULL;
324         }
325         else
326             memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
327                     sizeof (p_module->p_config[i].value));
328
329         p_module->p_config[i].b_dirty = false;
330
331         if( p_module->p_config[i].i_list )
332         {
333             if( p_module->p_config[i].ppsz_list )
334             {
335                 int j;
336                 p_module->p_config[i].ppsz_list =
337                     xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
338                 if( p_module->p_config[i].ppsz_list )
339                 {
340                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
341                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
342                     p_module->p_config[i].ppsz_list[j] = NULL;
343                 }
344             }
345             if( p_module->p_config[i].ppsz_list_text )
346             {
347                 int j;
348                 p_module->p_config[i].ppsz_list_text =
349                     xmalloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
350                 if( p_module->p_config[i].ppsz_list_text )
351                 {
352                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
353                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
354                   p_module->p_config[i].ppsz_list_text[j] = NULL;
355                 }
356             }
357             if( p_module->p_config[i].pi_list )
358             {
359                 p_module->p_config[i].pi_list =
360                     xmalloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
361                 if( p_module->p_config[i].pi_list )
362                 {
363                     for (int j = 0; j < p_module->p_config[i].i_list; j++)
364                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
365                 }
366             }
367         }
368
369         if( p_module->p_config[i].i_action )
370         {
371             p_module->p_config[i].ppf_action =
372                 xmalloc( p_module->p_config[i].i_action * sizeof(void *) );
373             p_module->p_config[i].ppsz_action_text =
374                 xmalloc( p_module->p_config[i].i_action * sizeof(char *) );
375
376             for (int j = 0; j < p_module->p_config[i].i_action; j++)
377             {
378                 p_module->p_config[i].ppf_action[j] = NULL;
379                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
380             }
381         }
382     }
383
384     return VLC_SUCCESS;
385
386  error:
387
388     return VLC_EGENERIC;
389 }
390
391 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
392
393 /**
394  * Saves a module cache to disk, and release cache data from memory.
395  */
396 void CacheSave (vlc_object_t *p_this, const char *dir,
397                module_cache_t *entries, size_t n)
398 {
399     char *filename = NULL, *tmpname = NULL;
400
401     if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
402         goto out;
403
404     if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
405         goto out;
406     msg_Dbg (p_this, "saving plugins cache %s", filename);
407
408     FILE *file = vlc_fopen (tmpname, "wb");
409     if (file == NULL)
410     {
411         if (errno != EACCES && errno != ENOENT)
412             msg_Warn (p_this, "cannot create %s (%m)", tmpname);
413         goto out;
414     }
415
416     if (CacheSaveBank (file, entries, n))
417     {
418         msg_Warn (p_this, "cannot write %s (%m)", tmpname);
419         clearerr (file);
420         fclose (file);
421         vlc_unlink (tmpname);
422         goto out;
423     }
424
425 #if !defined( WIN32 ) && !defined( __OS2__ )
426     vlc_rename (tmpname, filename); /* atomically replace old cache */
427     fclose (file);
428 #else
429     vlc_unlink (filename);
430     fclose (file);
431     vlc_rename (tmpname, filename);
432 #endif
433 out:
434     free (filename);
435     free (tmpname);
436
437     for (size_t i = 0; i < n; i++)
438         free (entries[i].path);
439     free (entries);
440 }
441
442 static int CacheSaveConfig (FILE *, const module_t *);
443 static int CacheSaveSubmodule (FILE *, const module_t *);
444
445 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
446                           size_t i_cache)
447 {
448     uint32_t i_file_size = 0;
449
450     /* Contains version number */
451     if (fputs (CACHE_STRING, file) == EOF)
452         goto error;
453 #ifdef DISTRO_VERSION
454     /* Allow binary maintaner to pass a string to detect new binary version*/
455     if (fputs( DISTRO_VERSION, file ) == EOF)
456         goto error;
457 #endif
458     /* Sub-version number (to avoid breakage in the dev version when cache
459      * structure changes) */
460     i_file_size = CACHE_SUBVERSION_NUM;
461     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
462         goto error;
463
464     /* Header marker */
465     i_file_size = ftell( file );
466     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
467         goto error;
468
469     if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
470         goto error;
471
472 #define SAVE_IMMEDIATE( a ) \
473     if (fwrite (&a, sizeof(a), 1, file) != 1) \
474         goto error
475 #define SAVE_STRING( a ) \
476     { \
477         uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \
478         if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \
479          || (a && (fwrite (a, 1, i_size, file) != i_size))) \
480             goto error; \
481     } while(0)
482
483     for (unsigned i = 0; i < i_cache; i++)
484     {
485         module_t *module = cache[i].p_module;
486         uint32_t i_submodule;
487
488         /* Save additional infos */
489         SAVE_STRING(module->psz_shortname);
490         SAVE_STRING(module->psz_longname);
491         SAVE_STRING(module->psz_help);
492         SAVE_IMMEDIATE(module->i_shortcuts);
493         for (unsigned j = 0; j < module->i_shortcuts; j++)
494             SAVE_STRING(module->pp_shortcuts[j]);
495
496         SAVE_STRING(module->psz_capability);
497         SAVE_IMMEDIATE(module->i_score);
498         SAVE_IMMEDIATE(module->b_unloadable);
499
500         /* Config stuff */
501         if (CacheSaveConfig (file, module))
502             goto error;
503
504         SAVE_STRING(module->domain);
505
506         i_submodule = module->submodule_count;
507         SAVE_IMMEDIATE( i_submodule );
508         if (CacheSaveSubmodule (file, module->submodule))
509             goto error;
510
511         /* Save common info */
512         SAVE_STRING(cache[i].path);
513         SAVE_IMMEDIATE(cache[i].mtime);
514         SAVE_IMMEDIATE(cache[i].size);
515     }
516
517     if (fflush (file)) /* flush libc buffers */
518         goto error;
519     return 0; /* success! */
520
521 error:
522     return -1;
523 }
524
525 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
526 {
527     if( !p_module )
528         return 0;
529     if( CacheSaveSubmodule( file, p_module->next ) )
530         goto error;
531
532     SAVE_STRING( p_module->psz_shortname );
533     SAVE_STRING( p_module->psz_longname );
534     SAVE_IMMEDIATE( p_module->i_shortcuts );
535     for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
536          SAVE_STRING( p_module->pp_shortcuts[j] );
537
538     SAVE_STRING( p_module->psz_capability );
539     SAVE_IMMEDIATE( p_module->i_score );
540     return 0;
541
542 error:
543     return -1;
544 }
545
546
547 static int CacheSaveConfig (FILE *file, const module_t *p_module)
548 {
549     uint32_t i_lines = p_module->confsize;
550
551     SAVE_IMMEDIATE( p_module->i_config_items );
552     SAVE_IMMEDIATE( p_module->i_bool_items );
553     SAVE_IMMEDIATE( i_lines );
554
555     for (size_t i = 0; i < i_lines ; i++)
556     {
557         SAVE_IMMEDIATE( p_module->p_config[i] );
558
559         SAVE_STRING( p_module->p_config[i].psz_type );
560         SAVE_STRING( p_module->p_config[i].psz_name );
561         SAVE_STRING( p_module->p_config[i].psz_text );
562         SAVE_STRING( p_module->p_config[i].psz_longtext );
563
564         if (IsConfigStringType (p_module->p_config[i].i_type))
565             SAVE_STRING( p_module->p_config[i].orig.psz );
566
567         if( p_module->p_config[i].i_list )
568         {
569             if( p_module->p_config[i].ppsz_list )
570             {
571                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
572                     SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
573             }
574
575             if( p_module->p_config[i].ppsz_list_text )
576             {
577                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
578                     SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
579             }
580             if( p_module->p_config[i].pi_list )
581             {
582                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
583                     SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
584             }
585         }
586
587         for (int j = 0; j < p_module->p_config[i].i_action; j++)
588             SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
589     }
590     return 0;
591
592 error:
593     return -1;
594 }
595
596 /*****************************************************************************
597  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
598  *****************************************************************************/
599 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
600 {
601     (void)p_this;
602
603     p_cache->pf_activate = p_module->pf_activate;
604     p_cache->pf_deactivate = p_module->pf_deactivate;
605     p_cache->handle = p_module->handle;
606
607     /* FIXME: This looks too simplistic an algorithm to me. What if the module
608      * file was altered such that the number of order of submodules was
609      * altered... after VLC started -- Courmisch, 09/2008 */
610     module_t *p_child = p_module->submodule,
611              *p_cchild = p_cache->submodule;
612     while( p_child && p_cchild )
613     {
614         p_cchild->pf_activate = p_child->pf_activate;
615         p_cchild->pf_deactivate = p_child->pf_deactivate;
616         p_child = p_child->next;
617         p_cchild = p_cchild->next;
618     }
619
620     p_cache->b_loaded = true;
621     p_module->b_loaded = false;
622 }
623
624 /**
625  * Looks up a plugin file in a table of cached plugins.
626  */
627 module_t *CacheFind (module_cache_t *cache, size_t count,
628                      const char *path, const struct stat *st)
629 {
630     while (count > 0)
631     {
632         if (cache->path != NULL
633          && !strcmp (cache->path, path)
634          && cache->mtime == st->st_mtime
635          && cache->size == st->st_size)
636        {
637             module_t *module = cache->p_module;
638             cache->p_module = NULL;
639             return module;
640        }
641        cache++;
642        count--;
643     }
644
645     return NULL;
646 }
647
648 /** Adds entry to the cache */
649 int CacheAdd (module_cache_t **cachep, size_t *countp,
650               const char *path, const struct stat *st, module_t *module)
651 {
652     module_cache_t *cache = *cachep;
653     const size_t count = *countp;
654
655     cache = realloc (cache, (count + 1) * sizeof (*cache));
656     if (unlikely(cache == NULL))
657         return -1;
658     *cachep = cache;
659
660     cache += count;
661     /* NOTE: strdup() could be avoided, but it would be a bit ugly */
662     cache->path = strdup (path);
663     cache->mtime = st->st_mtime;
664     cache->size = st->st_size;
665     cache->p_module = module;
666     *countp = count + 1;
667     return 0;
668 }
669
670 #endif /* HAVE_DYNAMIC_PLUGINS */