]> git.sesse.net Git - vlc/blobdiff - src/modules/cache.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / modules / cache.c
index 046f997f55027464fc7c794ffed90e2465b5dbe0..3f09ad8325ed6c3b9b0cd8d817ada68a5fcdebc6 100644 (file)
@@ -36,6 +36,7 @@
 #include <string.h>                                              /* strdup() */
 #include <vlc_plugin.h>
 #include <vlc_cpu.h>
+#include <errno.h>
 
 #include <sys/types.h>
 #ifdef HAVE_UNISTD_H
@@ -45,7 +46,7 @@
 
 #include "config/configuration.h"
 
-#include "vlc_charset.h"
+#include <vlc_fs.h>
 
 #include "modules/modules.h"
 
@@ -66,6 +67,7 @@ static int    CacheLoadConfig  ( module_t *, FILE * );
 /* Magic for the cache filename */
 #define CACHENAME_VALUES \
     sizeof(int), sizeof(void *), *(uint8_t *)&(uint16_t){ 0xbe1e }, vlc_CPU()
+#define CACHE_STRING "cache "PACKAGE_NAME" "PACKAGE_VERSION
 
 
 void CacheDelete( vlc_object_t *obj, const char *dir )
@@ -78,7 +80,7 @@ void CacheDelete( vlc_object_t *obj, const char *dir )
                   dir, CACHENAME_VALUES ) == -1 )
         return;
     msg_Dbg( obj, "removing plugins cache file %s", path );
-    utf8_unlink( path );
+    vlc_unlink( path );
     free( path );
 }
 
@@ -94,8 +96,8 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
 {
     char *psz_filename;
     FILE *file;
-    int j, i_size, i_read;
-    char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
+    int i_size, i_read;
+    char p_cachestring[sizeof(CACHE_STRING)];
     size_t i_cache;
     module_cache_t **pp_cache = NULL;
     int32_t i_file_size, i_marker;
@@ -111,7 +113,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
 
     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
 
-    file = utf8_fopen( psz_filename, "rb" );
+    file = vlc_fopen( psz_filename, "rb" );
     if( !file )
     {
         msg_Warn( p_this, "cannot read %s (%m)",
@@ -142,10 +144,10 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
     fseek( file, sizeof(i_file_size), SEEK_SET );
 
     /* Check the file is a plugins cache */
-    i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
+    i_size = sizeof(CACHE_STRING) - 1;
     i_read = fread( p_cachestring, 1, i_size, file );
     if( i_read != i_size ||
-        memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
+        memcmp( p_cachestring, CACHE_STRING, i_size ) )
     {
         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
         fclose( file );
@@ -251,10 +253,20 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
         LOAD_STRING( pp_cache[i]->p_module->psz_help );
-        for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
+
+        LOAD_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
+        if( pp_cache[i]->p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
+            goto error;
+        else if( pp_cache[i]->p_module->i_shortcuts == 0 )
+            pp_cache[i]->p_module->pp_shortcuts = NULL;
+        else
         {
-            LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
+            pp_cache[i]->p_module->pp_shortcuts =
+                    xmalloc( sizeof( char ** ) * pp_cache[i]->p_module->i_shortcuts );
+            for( unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++ )
+                LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
         }
+
         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
@@ -275,14 +287,24 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
         {
             module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
             free( p_module->psz_object_name );
+            free( p_module->pp_shortcuts );
             LOAD_STRING( p_module->psz_object_name );
             LOAD_STRING( p_module->psz_shortname );
             LOAD_STRING( p_module->psz_longname );
             LOAD_STRING( p_module->psz_help );
-            for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
+
+            LOAD_IMMEDIATE( p_module->i_shortcuts );
+            if( p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
+                goto error;
+            else if( p_module->i_shortcuts == 0 )
+                p_module->pp_shortcuts = NULL;
+            else
             {
-                LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
+                p_module->pp_shortcuts = xmalloc( sizeof( char ** ) * p_module->i_shortcuts );
+                for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
+                    LOAD_STRING( p_module->pp_shortcuts[j] );
             }
+
             LOAD_STRING( p_module->psz_capability );
             LOAD_IMMEDIATE( p_module->i_score );
             LOAD_IMMEDIATE( p_module->b_unloadable );
@@ -443,7 +465,6 @@ void CacheSave (vlc_object_t *p_this, const char *dir,
 {
     char *filename, *tmpname;
 
-    config_CreateDir( p_this, dir );
     if (asprintf (&filename, "%s"DIR_SEP CACHENAME_FORMAT, dir,
                   CACHENAME_VALUES ) == -1)
         return;
@@ -453,34 +474,36 @@ void CacheSave (vlc_object_t *p_this, const char *dir,
         free (filename);
         return;
     }
+    msg_Dbg (p_this, "saving plugins cache %s", filename);
 
-    FILE *file = utf8_fopen (tmpname, "wb");
+    FILE *file = vlc_fopen (tmpname, "wb");
     if (file == NULL)
-        goto error;
+    {
+        if (errno != EACCES && errno != ENOENT)
+            msg_Warn (p_this, "cannot create %s (%m)", tmpname);
+        goto out;
+    }
 
-    msg_Dbg (p_this, "saving plugins cache %s", tmpname);
     if (CacheSaveBank (file, pp_cache, n))
-        goto error;
+    {
+        msg_Warn (p_this, "cannot write %s (%m)", tmpname);
+        clearerr (file);
+        fclose (file);
+        vlc_unlink (tmpname);
+        goto out;
+    }
 
 #ifndef WIN32
-    utf8_rename (tmpname, filename); /* atomically replace old cache */
+    vlc_rename (tmpname, filename); /* atomically replace old cache */
     fclose (file);
 #else
-    utf8_unlink (filename);
+    vlc_unlink (filename);
     fclose (file);
-    utf8_rename (tmpname, filename);
+    vlc_rename (tmpname, filename);
 #endif
-    return; /* success! */
-
-error:
-    msg_Warn (p_this, "cannot write %s (%m)", tmpname);
+out:
     free (filename);
     free (tmpname);
-    if (file != NULL)
-    {
-        clearerr (file);
-        fclose (file);
-    }
 }
 
 static int CacheSaveConfig (FILE *, const module_t *);
@@ -496,7 +519,7 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
         goto error;
 
     /* Contains version number */
-    if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF)
+    if (fputs (CACHE_STRING, file) == EOF)
         goto error;
 #ifdef DISTRO_VERSION
     /* Allow binary maintaner to pass a string to detect new binary version*/
@@ -542,10 +565,10 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
         SAVE_STRING( pp_cache[i]->p_module->psz_help );
-        for (unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++)
-        {
-            SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
-        }
+        SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
+        for (unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++)
+            SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
+
         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
@@ -587,8 +610,9 @@ static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
     SAVE_STRING( p_module->psz_shortname );
     SAVE_STRING( p_module->psz_longname );
     SAVE_STRING( p_module->psz_help );
-    for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
-         SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
+    SAVE_IMMEDIATE( p_module->i_shortcuts );
+    for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
+         SAVE_STRING( p_module->pp_shortcuts[j] );
 
     SAVE_STRING( p_module->psz_capability );
     SAVE_IMMEDIATE( p_module->i_score );