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