]> git.sesse.net Git - vlc/blob - src/modules/cache.c
Revert "deinterlace: add basic support for YUY2 and NV12 (fixes #2206)"
[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 23
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 static module_t *CacheLoadModule (FILE *file)
218 {
219     module_t *module = vlc_module_create (NULL);
220
221     /* Load additional infos */
222     LOAD_STRING(module->psz_shortname);
223     LOAD_STRING(module->psz_longname);
224     LOAD_STRING(module->psz_help);
225
226     LOAD_IMMEDIATE(module->i_shortcuts);
227     if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
228         goto error;
229     else
230     {
231         module->pp_shortcuts =
232             xmalloc (sizeof (*module->pp_shortcuts) * module->i_shortcuts);
233         for (unsigned j = 0; j < module->i_shortcuts; j++)
234             LOAD_STRING(module->pp_shortcuts[j]);
235     }
236
237     LOAD_STRING(module->psz_capability);
238     LOAD_IMMEDIATE(module->i_score);
239     LOAD_IMMEDIATE(module->b_unloadable);
240
241     /* Config stuff */
242     if (CacheLoadModuleConfig (module, file) != VLC_SUCCESS)
243         goto error;
244
245     LOAD_STRING(module->domain);
246     if (module->domain != NULL)
247         vlc_bindtextdomain (module->domain);
248
249     uint32_t submodules;
250     LOAD_IMMEDIATE(submodules);
251
252     for (; submodules > 0; submodules--)
253     {
254         module_t *submodule = vlc_module_create (module);
255
256         free (submodule->pp_shortcuts);
257         LOAD_STRING(submodule->psz_shortname);
258         LOAD_STRING(submodule->psz_longname);
259
260         LOAD_IMMEDIATE(submodule->i_shortcuts);
261         if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
262             goto error;
263         else
264         {
265             submodule->pp_shortcuts =
266                 xmalloc (sizeof (*submodule->pp_shortcuts) * submodule->i_shortcuts);
267             for (unsigned j = 0; j < submodule->i_shortcuts; j++)
268                 LOAD_STRING(submodule->pp_shortcuts[j]);
269         }
270
271         LOAD_STRING(submodule->psz_capability);
272         LOAD_IMMEDIATE(submodule->i_score);
273     }
274
275     return module;
276 error:
277     return NULL; /* FIXME: leaks */
278 }
279
280 /**
281  * Loads a plugins cache file.
282  *
283  * This function will load the plugin cache if present and valid. This cache
284  * will in turn be queried by AllocateAllPlugins() to see if it needs to
285  * actually load the dynamically loadable module.
286  * This allows us to only fully load plugins when they are actually used.
287  */
288 size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
289 {
290     char *psz_filename;
291     FILE *file;
292     int i_size, i_read;
293     char p_cachestring[sizeof(CACHE_STRING)];
294     int32_t i_marker;
295
296     assert( dir != NULL );
297
298     *r = NULL;
299     if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
300         return 0;
301
302     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
303
304     file = vlc_fopen( psz_filename, "rb" );
305     if( !file )
306     {
307         msg_Warn( p_this, "cannot read %s: %s", psz_filename,
308                   vlc_strerror_c(errno) );
309         free( psz_filename );
310         return 0;
311     }
312     free( psz_filename );
313
314     /* Check the file is a plugins cache */
315     i_size = sizeof(CACHE_STRING) - 1;
316     i_read = fread( p_cachestring, 1, i_size, file );
317     if( i_read != i_size ||
318         memcmp( p_cachestring, CACHE_STRING, i_size ) )
319     {
320         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
321         fclose( file );
322         return 0;
323     }
324
325 #ifdef DISTRO_VERSION
326     /* Check for distribution specific version */
327     char p_distrostring[sizeof( DISTRO_VERSION )];
328     i_size = sizeof( DISTRO_VERSION ) - 1;
329     i_read = fread( p_distrostring, 1, i_size, file );
330     if( i_read != i_size ||
331         memcmp( p_distrostring, DISTRO_VERSION, i_size ) )
332     {
333         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
334         fclose( file );
335         return 0;
336     }
337 #endif
338
339     /* Check sub-version number */
340     i_read = fread( &i_marker, 1, sizeof(i_marker), file );
341     if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
342     {
343         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
344                   "(corrupted header)" );
345         fclose( file );
346         return 0;
347     }
348
349     /* Check header marker */
350     i_read = fread( &i_marker, 1, sizeof(i_marker), file );
351     if( i_read != sizeof(i_marker) ||
352         i_marker != ftell( file ) - (int)sizeof(i_marker) )
353     {
354         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
355                   "(corrupted header)" );
356         fclose( file );
357         return 0;
358     }
359
360     module_cache_t *cache = NULL;
361     size_t count = 0;
362
363     for (;;)
364     {
365         module_t *module = CacheLoadModule (file);
366         if (module == NULL)
367         {
368             if (feof (file))
369                 break;
370             goto error;
371         }
372
373         char *path;
374         struct stat st;
375
376         /* Load common info */
377         LOAD_STRING(path);
378         if (path == NULL)
379             goto error;
380         LOAD_IMMEDIATE(st.st_mtime);
381         LOAD_IMMEDIATE(st.st_size);
382
383         CacheAdd (&cache, &count, path, &st, module);
384         free (path);
385         /* TODO: deal with errors */
386     }
387
388     fclose( file );
389
390     *r = cache;
391     return count;
392
393 error:
394     if (ferror (file))
395         msg_Err(p_this, "plugins cache read error: %s", vlc_strerror_c(errno));
396     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
397
398     /* TODO: cleanup */
399     fclose( file );
400     return 0;
401 }
402
403 #define SAVE_IMMEDIATE( a ) \
404     if (fwrite (&(a), sizeof(a), 1, file) != 1) \
405         goto error
406 #define SAVE_FLAG(a) \
407     do { \
408         char b = (a); \
409         SAVE_IMMEDIATE(b); \
410     } while (0)
411
412 static int CacheSaveString (FILE *file, const char *str)
413 {
414     uint16_t size = (str != NULL) ? strlen (str) : 0;
415
416     SAVE_IMMEDIATE (size);
417     if (size != 0 && fwrite (str, 1, size, file) != size)
418     {
419 error:
420         return -1;
421     }
422     return 0;
423 }
424
425 #define SAVE_STRING( a ) \
426     if (CacheSaveString (file, (a))) \
427         goto error
428
429 static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
430 {
431     SAVE_IMMEDIATE (cfg->i_type);
432     SAVE_IMMEDIATE (cfg->i_short);
433     SAVE_FLAG (cfg->b_advanced);
434     SAVE_FLAG (cfg->b_internal);
435     SAVE_FLAG (cfg->b_unsaveable);
436     SAVE_FLAG (cfg->b_safe);
437     SAVE_FLAG (cfg->b_removed);
438     SAVE_STRING (cfg->psz_type);
439     SAVE_STRING (cfg->psz_name);
440     SAVE_STRING (cfg->psz_text);
441     SAVE_STRING (cfg->psz_longtext);
442     SAVE_IMMEDIATE (cfg->list_count);
443
444     if (IsConfigStringType (cfg->i_type))
445     {
446         SAVE_STRING (cfg->orig.psz);
447         if (cfg->list_count == 0)
448             SAVE_IMMEDIATE (cfg->list.psz_cb); /* XXX: see CacheLoadConfig() */
449         for (unsigned i = 0; i < cfg->list_count; i++)
450             SAVE_STRING (cfg->list.psz[i]);
451     }
452     else
453     {
454         SAVE_IMMEDIATE (cfg->orig);
455         SAVE_IMMEDIATE (cfg->min);
456         SAVE_IMMEDIATE (cfg->max);
457         if (cfg->list_count == 0)
458             SAVE_IMMEDIATE (cfg->list.i_cb); /* XXX: see CacheLoadConfig() */
459         for (unsigned i = 0; i < cfg->list_count; i++)
460              SAVE_IMMEDIATE (cfg->list.i[i]);
461     }
462     for (unsigned i = 0; i < cfg->list_count; i++)
463         SAVE_STRING (cfg->list_text[i]);
464
465     return 0;
466 error:
467     return -1;
468 }
469
470 static int CacheSaveModuleConfig (FILE *file, const module_t *module)
471 {
472     uint16_t lines = module->confsize;
473
474     SAVE_IMMEDIATE (module->i_config_items);
475     SAVE_IMMEDIATE (module->i_bool_items);
476     SAVE_IMMEDIATE (lines);
477
478     for (size_t i = 0; i < lines; i++)
479         if (CacheSaveConfig (file, module->p_config + i))
480            goto error;
481
482     return 0;
483 error:
484     return -1;
485 }
486
487 static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
488
489 /**
490  * Saves a module cache to disk, and release cache data from memory.
491  */
492 void CacheSave (vlc_object_t *p_this, const char *dir,
493                module_cache_t *entries, size_t n)
494 {
495     char *filename = NULL, *tmpname = NULL;
496
497     if (asprintf (&filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1)
498         goto out;
499
500     if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
501         goto out;
502     msg_Dbg (p_this, "saving plugins cache %s", filename);
503
504     FILE *file = vlc_fopen (tmpname, "wb");
505     if (file == NULL)
506     {
507         if (errno != EACCES && errno != ENOENT)
508             msg_Warn (p_this, "cannot create %s: %s", tmpname,
509                       vlc_strerror_c(errno));
510         goto out;
511     }
512
513     if (CacheSaveBank (file, entries, n))
514     {
515         msg_Warn (p_this, "cannot write %s: %s", tmpname,
516                   vlc_strerror_c(errno));
517         clearerr (file);
518         fclose (file);
519         vlc_unlink (tmpname);
520         goto out;
521     }
522
523 #if !defined( _WIN32 ) && !defined( __OS2__ )
524     vlc_rename (tmpname, filename); /* atomically replace old cache */
525     fclose (file);
526 #else
527     vlc_unlink (filename);
528     fclose (file);
529     vlc_rename (tmpname, filename);
530 #endif
531 out:
532     free (filename);
533     free (tmpname);
534
535     for (size_t i = 0; i < n; i++)
536         free (entries[i].path);
537     free (entries);
538 }
539
540 static int CacheSaveSubmodule (FILE *, const module_t *);
541
542 static int CacheSaveBank (FILE *file, const module_cache_t *cache,
543                           size_t i_cache)
544 {
545     uint32_t i_file_size = 0;
546
547     /* Contains version number */
548     if (fputs (CACHE_STRING, file) == EOF)
549         goto error;
550 #ifdef DISTRO_VERSION
551     /* Allow binary maintaner to pass a string to detect new binary version*/
552     if (fputs( DISTRO_VERSION, file ) == EOF)
553         goto error;
554 #endif
555     /* Sub-version number (to avoid breakage in the dev version when cache
556      * structure changes) */
557     i_file_size = CACHE_SUBVERSION_NUM;
558     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 )
559         goto error;
560
561     /* Header marker */
562     i_file_size = ftell( file );
563     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
564         goto error;
565
566     for (unsigned i = 0; i < i_cache; i++)
567     {
568         module_t *module = cache[i].p_module;
569         uint32_t i_submodule;
570
571         /* Save additional infos */
572         SAVE_STRING(module->psz_shortname);
573         SAVE_STRING(module->psz_longname);
574         SAVE_STRING(module->psz_help);
575         SAVE_IMMEDIATE(module->i_shortcuts);
576         for (unsigned j = 0; j < module->i_shortcuts; j++)
577             SAVE_STRING(module->pp_shortcuts[j]);
578
579         SAVE_STRING(module->psz_capability);
580         SAVE_IMMEDIATE(module->i_score);
581         SAVE_IMMEDIATE(module->b_unloadable);
582
583         /* Config stuff */
584         if (CacheSaveModuleConfig (file, module))
585             goto error;
586
587         SAVE_STRING(module->domain);
588
589         i_submodule = module->submodule_count;
590         SAVE_IMMEDIATE( i_submodule );
591         if (CacheSaveSubmodule (file, module->submodule))
592             goto error;
593
594         /* Save common info */
595         SAVE_STRING(cache[i].path);
596         SAVE_IMMEDIATE(cache[i].mtime);
597         SAVE_IMMEDIATE(cache[i].size);
598     }
599
600     if (fflush (file)) /* flush libc buffers */
601         goto error;
602     return 0; /* success! */
603
604 error:
605     return -1;
606 }
607
608 static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
609 {
610     if( !p_module )
611         return 0;
612     if( CacheSaveSubmodule( file, p_module->next ) )
613         goto error;
614
615     SAVE_STRING( p_module->psz_shortname );
616     SAVE_STRING( p_module->psz_longname );
617     SAVE_IMMEDIATE( p_module->i_shortcuts );
618     for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
619          SAVE_STRING( p_module->pp_shortcuts[j] );
620
621     SAVE_STRING( p_module->psz_capability );
622     SAVE_IMMEDIATE( p_module->i_score );
623     return 0;
624
625 error:
626     return -1;
627 }
628
629 /*****************************************************************************
630  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
631  *****************************************************************************/
632 void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
633 {
634     (void)p_this;
635
636     p_cache->pf_activate = p_module->pf_activate;
637     p_cache->pf_deactivate = p_module->pf_deactivate;
638     p_cache->handle = p_module->handle;
639
640     /* FIXME: This looks too simplistic an algorithm to me. What if the module
641      * file was altered such that the number of order of submodules was
642      * altered... after VLC started -- Courmisch, 09/2008 */
643     module_t *p_child = p_module->submodule,
644              *p_cchild = p_cache->submodule;
645     while( p_child && p_cchild )
646     {
647         p_cchild->pf_activate = p_child->pf_activate;
648         p_cchild->pf_deactivate = p_child->pf_deactivate;
649         p_child = p_child->next;
650         p_cchild = p_cchild->next;
651     }
652
653     p_cache->b_loaded = true;
654     p_module->b_loaded = false;
655 }
656
657 /**
658  * Looks up a plugin file in a table of cached plugins.
659  */
660 module_t *CacheFind (module_cache_t *cache, size_t count,
661                      const char *path, const struct stat *st)
662 {
663     while (count > 0)
664     {
665         if (cache->path != NULL
666          && !strcmp (cache->path, path)
667          && cache->mtime == st->st_mtime
668          && cache->size == st->st_size)
669        {
670             module_t *module = cache->p_module;
671             cache->p_module = NULL;
672             return module;
673        }
674        cache++;
675        count--;
676     }
677
678     return NULL;
679 }
680
681 /** Adds entry to the cache */
682 int CacheAdd (module_cache_t **cachep, size_t *countp,
683               const char *path, const struct stat *st, module_t *module)
684 {
685     module_cache_t *cache = *cachep;
686     const size_t count = *countp;
687
688     cache = realloc (cache, (count + 1) * sizeof (*cache));
689     if (unlikely(cache == NULL))
690         return -1;
691     *cachep = cache;
692
693     cache += count;
694     /* NOTE: strdup() could be avoided, but it would be a bit ugly */
695     cache->path = strdup (path);
696     cache->mtime = st->st_mtime;
697     cache->size = st->st_size;
698     cache->p_module = module;
699     *countp = count + 1;
700     return 0;
701 }
702
703 #endif /* HAVE_DYNAMIC_PLUGINS */