]> git.sesse.net Git - vlc/blob - src/modules/cache.c
input: cosmetic
[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 #include <unistd.h>
37 #include <assert.h>
38
39 #include <vlc_common.h>
40 #include "libvlc.h"
41
42 #include <vlc_plugin.h>
43 #include <errno.h>
44
45 #include "config/configuration.h"
46
47 #include <vlc_fs.h>
48
49 #include "modules/modules.h"
50
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 #ifdef HAVE_DYNAMIC_PLUGINS
56 /* Sub-version number
57  * (only used to avoid breakage in dev version when cache structure changes) */
58 #define CACHE_SUBVERSION_NUM 22
59
60 /* Cache filename */
61 #define CACHE_NAME "plugins.dat"
62 /* Magic for the cache filename */
63 #define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
64
65
66 void CacheDelete( vlc_object_t *obj, const char *dir )
67 {
68     char *path;
69
70     assert( dir != NULL );
71
72     if( asprintf( &path, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
73         return;
74     msg_Dbg( obj, "removing plugins cache file %s", path );
75     vlc_unlink( path );
76     free( path );
77 }
78
79 #define LOAD_IMMEDIATE(a) \
80     if (fread (&(a), sizeof (char), sizeof (a), file) != sizeof (a)) \
81         goto error
82 #define LOAD_FLAG(a) \
83     do { \
84         unsigned char b; \
85         LOAD_IMMEDIATE(b); \
86         if (b > 1) \
87             goto error; \
88         (a) = b; \
89     } while (0)
90
91 static int CacheLoadString (char **p, FILE *file)
92 {
93     char *psz = NULL;
94     uint16_t size;
95
96     LOAD_IMMEDIATE (size);
97     if (size > 16384)
98     {
99 error:
100         return -1;
101     }
102
103     if (size > 0)
104     {
105         psz = malloc (size+1);
106         if (unlikely(psz == NULL))
107             goto error;
108         if (fread (psz, 1, size, file) != size)
109         {
110             free (psz);
111             goto error;
112         }
113         psz[size] = '\0';
114     }
115     *p = psz;
116     return 0;
117 }
118
119 #define LOAD_STRING(a) \
120     if (CacheLoadString (&(a), file)) goto error
121
122 static int CacheLoadConfig (module_config_t *cfg, FILE *file)
123 {
124     LOAD_IMMEDIATE (cfg->i_type);
125     LOAD_IMMEDIATE (cfg->i_short);
126     LOAD_FLAG (cfg->b_advanced);
127     LOAD_FLAG (cfg->b_internal);
128     LOAD_FLAG (cfg->b_unsaveable);
129     LOAD_FLAG (cfg->b_safe);
130     LOAD_FLAG (cfg->b_removed);
131     LOAD_STRING (cfg->psz_type);
132     LOAD_STRING (cfg->psz_name);
133     LOAD_STRING (cfg->psz_text);
134     LOAD_STRING (cfg->psz_longtext);
135     LOAD_IMMEDIATE (cfg->list_count);
136
137     if (IsConfigStringType (cfg->i_type))
138     {
139         LOAD_STRING (cfg->orig.psz);
140         if (cfg->orig.psz != NULL)
141             cfg->value.psz = strdup (cfg->orig.psz);
142         else
143             cfg->value.psz = NULL;
144
145         if (cfg->list_count)
146             cfg->list.psz = xmalloc (cfg->list_count * sizeof (char *));
147         else /* TODO: fix config_GetPszChoices() instead of this hack: */
148             LOAD_IMMEDIATE(cfg->list.psz_cb);
149         for (unsigned i = 0; i < cfg->list_count; i++)
150         {
151             LOAD_STRING (cfg->list.psz[i]);
152             if (cfg->list.psz[i] == NULL /* NULL -> empty string */
153              && (cfg->list.psz[i] = calloc (1, 1)) == NULL)
154                 goto error;
155         }
156     }
157     else
158     {
159         LOAD_IMMEDIATE (cfg->orig);
160         LOAD_IMMEDIATE (cfg->min);
161         LOAD_IMMEDIATE (cfg->max);
162         cfg->value = cfg->orig;
163
164         if (cfg->list_count)
165             cfg->list.i = xmalloc (cfg->list_count * sizeof (int));
166         else /* TODO: fix config_GetPszChoices() instead of this hack: */
167             LOAD_IMMEDIATE(cfg->list.i_cb);
168         for (unsigned i = 0; i < cfg->list_count; i++)
169              LOAD_IMMEDIATE (cfg->list.i[i]);
170     }
171     cfg->list_text = xmalloc (cfg->list_count * sizeof (char *));
172     for (unsigned i = 0; i < cfg->list_count; i++)
173     {
174         LOAD_STRING (cfg->list_text[i]);
175         if (cfg->list_text[i] == NULL /* NULL -> empty string */
176          && (cfg->list_text[i] = calloc (1, 1)) == NULL)
177             goto error;
178     }
179
180     return 0;
181 error:
182     return -1; /* FIXME: leaks */
183 }
184
185 static int CacheLoadModuleConfig (module_t *module, FILE *file)
186 {
187     uint16_t lines;
188
189     /* Calculate the structure length */
190     LOAD_IMMEDIATE (module->i_config_items);
191     LOAD_IMMEDIATE (module->i_bool_items);
192     LOAD_IMMEDIATE (lines);
193
194     /* Allocate memory */
195     if (lines)
196     {
197         module->p_config = malloc (lines * sizeof (module_config_t));
198         if (unlikely(module->p_config == NULL))
199         {
200             module->confsize = 0;
201             return -1;
202         }
203     }
204     else
205         module->p_config = NULL;
206     module->confsize = lines;
207
208     /* Do the duplication job */
209     for (size_t i = 0; i < lines; i++)
210         if (CacheLoadConfig (module->p_config + i, file))
211             return -1;
212     return 0;
213 error:
214     return -1; /* FIXME: leaks */
215 }
216
217
218 /**
219  * Loads a plugins cache file.
220  *
221  * This function will load the plugin cache if present and valid. This cache
222  * will in turn be queried by AllocateAllPlugins() to see if it needs to
223  * actually load the dynamically loadable module.
224  * This allows us to only fully load plugins when they are actually used.
225  */
226 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
227 {
228     char *psz_filename;
229     FILE *file;
230     int i_size, i_read;
231     char p_cachestring[sizeof(CACHE_STRING)];
232     size_t i_cache;
233     int32_t i_marker;
234
235     assert( dir != NULL );
236
237     *r = NULL;
238     if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
239         return 0;
240
241     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
242
243     file = vlc_fopen( psz_filename, "rb" );
244     if( !file )
245     {
246         msg_Warn( p_this, "cannot read %s: %s", psz_filename,
247                   vlc_strerror_c(errno) );
248         free( psz_filename );
249         return 0;
250     }
251     free( psz_filename );
252
253     /* Check the file is a plugins cache */
254     i_size = sizeof(CACHE_STRING) - 1;
255     i_read = fread( p_cachestring, 1, i_size, file );
256     if( i_read != i_size ||
257         memcmp( p_cachestring, CACHE_STRING, i_size ) )
258     {
259         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
260         fclose( file );
261         return 0;
262     }
263
264 #ifdef DISTRO_VERSION
265     /* Check for distribution specific version */
266     char p_distrostring[sizeof( DISTRO_VERSION )];
267     i_size = sizeof( DISTRO_VERSION ) - 1;
268     i_read = fread( p_distrostring, 1, i_size, file );
269     if( i_read != i_size ||
270         memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
271     {
272         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
273         fclose( file );
274         return 0;
275     }
276 #endif
277
278     /* Check Sub-version number */
279     i_read = fread( &i_marker, 1, sizeof(i_marker), file );
280     if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
281     {
282         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
283                   "(corrupted header)" );
284         fclose( file );
285         return 0;
286     }
287
288     /* Check header marker */
289     i_read = fread( &i_marker, 1, sizeof(i_marker), file );
290     if( i_read != sizeof(i_marker) ||
291         i_marker != ftell( file ) - (int)sizeof(i_marker) )
292     {
293         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
294                   "(corrupted header)" );
295         fclose( file );
296         return 0;
297     }
298
299     if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
300     {
301         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
302                   "(file too short)" );
303         fclose( file );
304         return 0;
305     }
306
307     module_cache_t *cache = NULL;
308
309     for (size_t count = 0; count < i_cache;)
310     {
311         module_t *module;
312         int i_submodules;
313
314         module = vlc_module_create (NULL);
315
316         /* Load additional infos */
317         LOAD_STRING(module->psz_shortname);
318         LOAD_STRING(module->psz_longname);
319         LOAD_STRING(module->psz_help);
320
321         LOAD_IMMEDIATE(module->i_shortcuts);
322         if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
323             goto error;
324         else
325         {
326             module->pp_shortcuts =
327                               xmalloc (sizeof (*module->pp_shortcuts) * module->i_shortcuts);
328             for (unsigned j = 0; j < module->i_shortcuts; j++)
329                 LOAD_STRING(module->pp_shortcuts[j]);
330         }
331
332         LOAD_STRING(module->psz_capability);
333         LOAD_IMMEDIATE(module->i_score);
334         LOAD_IMMEDIATE(module->b_unloadable);
335
336         /* Config stuff */
337         if (CacheLoadModuleConfig (module, file) != VLC_SUCCESS)
338             goto error;
339
340         LOAD_STRING(module->domain);
341         if (module->domain != NULL)
342             vlc_bindtextdomain (module->domain);
343
344         LOAD_IMMEDIATE( i_submodules );
345
346         while( i_submodules-- )
347         {
348             module_t *submodule = vlc_module_create (module);
349             free (submodule->pp_shortcuts);
350             LOAD_STRING(submodule->psz_shortname);
351             LOAD_STRING(submodule->psz_longname);
352
353             LOAD_IMMEDIATE(submodule->i_shortcuts);
354             if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
355                 goto error;
356             else
357             {
358                 submodule->pp_shortcuts =
359                            xmalloc (sizeof (*submodule->pp_shortcuts) * submodule->i_shortcuts);
360                 for (unsigned j = 0; j < submodule->i_shortcuts; j++)
361                     LOAD_STRING(submodule->pp_shortcuts[j]);
362             }
363
364             LOAD_STRING(submodule->psz_capability);
365             LOAD_IMMEDIATE(submodule->i_score);
366         }
367
368         char *path;
369         struct stat st;
370
371         /* Load common info */
372         LOAD_STRING(path);
373         if (path == NULL)
374             goto error;
375         LOAD_IMMEDIATE(st.st_mtime);
376         LOAD_IMMEDIATE(st.st_size);
377
378         CacheAdd (&cache, &count, path, &st, module);
379         free (path);
380         /* TODO: deal with errors */
381     }
382     fclose( file );
383
384     *r = cache;
385     return i_cache;
386
387 error:
388     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
389
390     /* TODO: cleanup */
391     fclose( file );
392     return 0;
393 }
394
395 #define SAVE_IMMEDIATE( a ) \
396     if (fwrite (&(a), sizeof(a), 1, file) != 1) \
397         goto error
398 #define SAVE_FLAG(a) \
399     do { \
400         char b = (a); \
401         SAVE_IMMEDIATE(b); \
402     } while (0)
403
404 static int CacheSaveString (FILE *file, const char *str)
405 {
406     uint16_t size = (str != NULL) ? strlen (str) : 0;
407
408     SAVE_IMMEDIATE (size);
409     if (size != 0 && fwrite (str, 1, size, file) != size)
410     {
411 error:
412         return -1;
413     }
414     return 0;
415 }
416
417 #define SAVE_STRING( a ) \
418     if (CacheSaveString (file, (a))) \
419         goto error
420
421 static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
422 {
423     SAVE_IMMEDIATE (cfg->i_type);
424     SAVE_IMMEDIATE (cfg->i_short);
425     SAVE_FLAG (cfg->b_advanced);
426     SAVE_FLAG (cfg->b_internal);
427     SAVE_FLAG (cfg->b_unsaveable);
428     SAVE_FLAG (cfg->b_safe);
429     SAVE_FLAG (cfg->b_removed);
430     SAVE_STRING (cfg->psz_type);
431     SAVE_STRING (cfg->psz_name);
432     SAVE_STRING (cfg->psz_text);
433     SAVE_STRING (cfg->psz_longtext);
434     SAVE_IMMEDIATE (cfg->list_count);
435
436     if (IsConfigStringType (cfg->i_type))
437     {
438         SAVE_STRING (cfg->orig.psz);
439         if (cfg->list_count == 0)
440             SAVE_IMMEDIATE (cfg->list.psz_cb); /* XXX: see CacheLoadConfig() */
441         for (unsigned i = 0; i < cfg->list_count; i++)
442             SAVE_STRING (cfg->list.psz[i]);
443     }
444     else
445     {
446         SAVE_IMMEDIATE (cfg->orig);
447         SAVE_IMMEDIATE (cfg->min);
448         SAVE_IMMEDIATE (cfg->max);
449         if (cfg->list_count == 0)
450             SAVE_IMMEDIATE (cfg->list.i_cb); /* XXX: see CacheLoadConfig() */
451         for (unsigned i = 0; i < cfg->list_count; i++)
452              SAVE_IMMEDIATE (cfg->list.i[i]);
453     }
454     for (unsigned i = 0; i < cfg->list_count; i++)
455         SAVE_STRING (cfg->list_text[i]);
456
457     return 0;
458 error:
459     return -1;
460 }
461
462 static int CacheSaveModuleConfig (FILE *file, const module_t *module)
463 {
464     uint16_t lines = module->confsize;
465
466     SAVE_IMMEDIATE (module->i_config_items);
467     SAVE_IMMEDIATE (module->i_bool_items);
468     SAVE_IMMEDIATE (lines);
469
470     for (size_t i = 0; i < lines; i++)
471         if (CacheSaveConfig (file, module->p_config + i))
472            goto error;
473
474     return 0;
475 error:
476     return -1;
477 }
478
479 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
480
481 /**
482  * Saves a module cache to disk, and release cache data from memory.
483  */
484 void CacheSave (vlc_object_t *p_this, const char *dir,
485                module_cache_t *entries, size_t n)
486 {
487     char *filename = NULL, *tmpname = NULL;
488
489     if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
490         goto out;
491
492     if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
493         goto out;
494     msg_Dbg (p_this, "saving plugins cache %s", filename);
495
496     FILE *file = vlc_fopen (tmpname, "wb");
497     if (file == NULL)
498     {
499         if (errno != EACCES && errno != ENOENT)
500             msg_Warn (p_this, "cannot create %s: %s", tmpname,
501                       vlc_strerror_c(errno));
502         goto out;
503     }
504
505     if (CacheSaveBank (file, entries, n))
506     {
507         msg_Warn (p_this, "cannot write %s: %s", tmpname,
508                   vlc_strerror_c(errno));
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 */