]> git.sesse.net Git - vlc/blobdiff - src/modules/cache.c
Fix memory leak and dummy warning
[vlc] / src / modules / cache.c
index d8efe76b09b7efda0d94196befc98ac9b3837da7..ff06fa371c6327ca2e66082228b1f0518e061c08 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
@@ -58,7 +59,7 @@ static int    CacheLoadConfig  ( module_t *, FILE * );
 
 /* Sub-version number
  * (only used to avoid breakage in dev version when cache structure changes) */
-#define CACHE_SUBVERSION_NUM 10
+#define CACHE_SUBVERSION_NUM 11
 
 /* Format string for the cache filename */
 #define CACHENAME_FORMAT \
@@ -96,7 +97,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
     FILE *file;
     int j, i_size, i_read;
     char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
-    unsigned i_cache;
+    size_t i_cache;
     module_cache_t **pp_cache = NULL;
     int32_t i_file_size, i_marker;
 
@@ -197,7 +198,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
 
     if( i_cache )
     {
-        unsigned i_already = p_bank->i_loaded_cache;
+        size_t i_already = p_bank->i_loaded_cache;
         pp_cache = realloc( p_bank->pp_loaded_cache,
                             (i_already + i_cache) * sizeof(*pp_cache) );
         if( unlikely(pp_cache == NULL) )
@@ -231,7 +232,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
     } \
 }
 
-    for( unsigned i = 0; i < i_cache; i++ )
+    for( size_t i = 0; i < i_cache; i++ )
     {
         uint16_t i_size;
         int i_submodules;
@@ -433,63 +434,62 @@ static int CacheLoadConfig( module_t *p_module, FILE *file )
     return VLC_EGENERIC;
 }
 
-static int CacheSaveBank( FILE *file, module_bank_t *p_bank );
+static int CacheSaveBank( FILE *file, module_cache_t *const *, size_t );
 
 /*****************************************************************************
  * SavePluginsCache: saves the plugins cache to a file
  *****************************************************************************/
-void CacheSave( vlc_object_t *p_this, module_bank_t *p_bank )
+void CacheSave (vlc_object_t *p_this, const char *dir,
+                module_cache_t *const *pp_cache, size_t n)
 {
-    char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
-    if( !psz_cachedir ) /* XXX: this should never happen */
+    char *filename, *tmpname;
+
+    if (asprintf (&filename, "%s"DIR_SEP CACHENAME_FORMAT, dir,
+                  CACHENAME_VALUES ) == -1)
+        return;
+
+    if (asprintf (&tmpname, "%s.%"PRIu32, filename, (uint32_t)getpid ()) == -1)
     {
-        msg_Err( p_this, "unable to get cache directory" );
+        free (filename);
         return;
     }
+    msg_Dbg (p_this, "saving plugins cache %s", filename);
 
-    char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
-    config_CreateDir( p_this, psz_cachedir );
-
-    snprintf( psz_filename, sizeof( psz_filename ),
-              "%s"DIR_SEP CACHENAME_FORMAT, psz_cachedir,
-              CACHENAME_VALUES );
-    free( psz_cachedir );
-
-    char psz_tmpname[sizeof (psz_filename) + 12];
-    snprintf (psz_tmpname, sizeof (psz_tmpname), "%s.%"PRIu32, psz_filename,
-              (uint32_t)getpid ());
-    FILE *file = utf8_fopen( psz_tmpname, "wb" );
+    FILE *file = utf8_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", psz_filename);
-    if (CacheSaveBank (file, p_bank))
-        goto error;
+    if (CacheSaveBank (file, pp_cache, n))
+    {
+        msg_Warn (p_this, "cannot write %s (%m)", tmpname);
+        clearerr (file);
+        fclose (file);
+        utf8_unlink (tmpname);
+        goto out;
+    }
 
 #ifndef WIN32
-    utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */
+    utf8_rename (tmpname, filename); /* atomically replace old cache */
     fclose (file);
 #else
-    utf8_unlink (psz_filename);
+    utf8_unlink (filename);
     fclose (file);
-    utf8_rename (psz_tmpname, psz_filename);
+    utf8_rename (tmpname, filename);
 #endif
-    return; /* success! */
-
-error:
-    msg_Warn (p_this, "could not write plugins cache %s (%m)",
-              psz_filename);
-    if (file != NULL)
-    {
-        clearerr (file);
-        fclose (file);
-    }
+out:
+    free (filename);
+    free (tmpname);
 }
 
-static int CacheSaveConfig (FILE *, module_t *);
-static int CacheSaveSubmodule (FILE *, module_t *);
+static int CacheSaveConfig (FILE *, const module_t *);
+static int CacheSaveSubmodule (FILE *, const module_t *);
 
-static int CacheSaveBank (FILE *file, module_bank_t *p_bank)
+static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
+                          size_t i_cache)
 {
     uint32_t i_file_size = 0;
 
@@ -516,9 +516,6 @@ static int CacheSaveBank (FILE *file, module_bank_t *p_bank)
     if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
         goto error;
 
-    module_cache_t **pp_cache = p_bank->pp_cache;
-    uint32_t i_cache = p_bank->i_cache;
-
     if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
         goto error;
 
@@ -581,7 +578,7 @@ error:
     return -1;
 }
 
-static int CacheSaveSubmodule( FILE *file, module_t *p_module )
+static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
 {
     if( !p_module )
         return 0;
@@ -606,7 +603,7 @@ error:
 }
 
 
-static int CacheSaveConfig (FILE *file, module_t *p_module)
+static int CacheSaveConfig (FILE *file, const module_t *p_module)
 {
     uint32_t i_lines = p_module->confsize;