Commit 271b3d59 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Save plugins cache into corresponding plugins directory

parent d640dbaf
...@@ -58,7 +58,7 @@ static int CacheLoadConfig ( module_t *, FILE * ); ...@@ -58,7 +58,7 @@ static int CacheLoadConfig ( module_t *, FILE * );
/* Sub-version number /* Sub-version number
* (only used to avoid breakage in dev version when cache structure changes) */ * (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 */ /* Format string for the cache filename */
#define CACHENAME_FORMAT \ #define CACHENAME_FORMAT \
...@@ -96,7 +96,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -96,7 +96,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
FILE *file; FILE *file;
int j, i_size, i_read; int j, i_size, i_read;
char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)]; char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
unsigned i_cache; size_t i_cache;
module_cache_t **pp_cache = NULL; module_cache_t **pp_cache = NULL;
int32_t i_file_size, i_marker; int32_t i_file_size, i_marker;
...@@ -197,7 +197,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -197,7 +197,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
if( i_cache ) 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, pp_cache = realloc( p_bank->pp_loaded_cache,
(i_already + i_cache) * sizeof(*pp_cache) ); (i_already + i_cache) * sizeof(*pp_cache) );
if( unlikely(pp_cache == NULL) ) if( unlikely(pp_cache == NULL) )
...@@ -231,7 +231,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -231,7 +231,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; uint16_t i_size;
int i_submodules; int i_submodules;
...@@ -433,52 +433,49 @@ static int CacheLoadConfig( module_t *p_module, FILE *file ) ...@@ -433,52 +433,49 @@ static int CacheLoadConfig( module_t *p_module, FILE *file )
return VLC_EGENERIC; 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 * 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); char *filename, *tmpname;
if( !psz_cachedir ) /* XXX: this should never happen */
config_CreateDir( p_this, dir );
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; return;
} }
char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)]; FILE *file = utf8_fopen (tmpname, "wb");
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" );
if (file == NULL) if (file == NULL)
goto error; goto error;
msg_Dbg (p_this, "saving plugins cache %s", psz_filename); msg_Dbg (p_this, "saving plugins cache %s", tmpname);
if (CacheSaveBank (file, p_bank)) if (CacheSaveBank (file, pp_cache, n))
goto error; goto error;
#ifndef WIN32 #ifndef WIN32
utf8_rename (psz_tmpname, psz_filename); /* atomically replace old cache */ utf8_rename (tmpname, filename); /* atomically replace old cache */
fclose (file); fclose (file);
#else #else
utf8_unlink (psz_filename); utf8_unlink (filename);
fclose (file); fclose (file);
utf8_rename (psz_tmpname, psz_filename); utf8_rename (tmpname, filename);
#endif #endif
return; /* success! */ return; /* success! */
error: error:
msg_Warn (p_this, "could not write plugins cache %s (%m)", msg_Warn (p_this, "cannot write %s (%m)", tmpname);
psz_filename); free (filename);
free (tmpname);
if (file != NULL) if (file != NULL)
{ {
clearerr (file); clearerr (file);
...@@ -489,7 +486,8 @@ error: ...@@ -489,7 +486,8 @@ error:
static int CacheSaveConfig (FILE *, const module_t *); static int CacheSaveConfig (FILE *, const module_t *);
static int CacheSaveSubmodule (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; uint32_t i_file_size = 0;
...@@ -516,9 +514,6 @@ static int CacheSaveBank (FILE *file, module_bank_t *p_bank) ...@@ -516,9 +514,6 @@ static int CacheSaveBank (FILE *file, module_bank_t *p_bank)
if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1) if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1)
goto error; 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) if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1)
goto error; goto error;
......
...@@ -214,8 +214,6 @@ void module_LoadPlugins( vlc_object_t * p_this ) ...@@ -214,8 +214,6 @@ void module_LoadPlugins( vlc_object_t * p_this )
p_module_bank->b_cache = var_InheritBool( p_this, "plugins-cache" ); p_module_bank->b_cache = var_InheritBool( p_this, "plugins-cache" );
AllocateAllPlugins( p_this, p_module_bank ); AllocateAllPlugins( p_this, p_module_bank );
if( p_module_bank->b_cache )
CacheSave( p_this, p_bank );
config_UnsortConfig (); config_UnsortConfig ();
config_SortConfig (); config_SortConfig ();
} }
...@@ -865,6 +863,7 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank ) ...@@ -865,6 +863,7 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
if( !path ) if( !path )
continue; continue;
size_t offset = p_module_bank->i_loaded_cache;
if( b_reset ) if( b_reset )
CacheDelete( p_this, path ); CacheDelete( p_this, path );
else else
...@@ -875,6 +874,8 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank ) ...@@ -875,6 +874,8 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
/* Don't go deeper than 5 subdirectories */ /* Don't go deeper than 5 subdirectories */
AllocatePluginDir( p_this, p_bank, path, 5 ); AllocatePluginDir( p_this, p_bank, path, 5 );
CacheSave( p_this, path, p_module_bank->pp_loaded_cache + offset,
p_module_bank->i_loaded_cache - offset );
free( path ); free( path );
} }
......
...@@ -160,7 +160,7 @@ void module_Unload (module_handle_t); ...@@ -160,7 +160,7 @@ void module_Unload (module_handle_t);
void CacheMerge (vlc_object_t *, module_t *, module_t *); void CacheMerge (vlc_object_t *, module_t *, module_t *);
void CacheDelete(vlc_object_t *, const char *); void CacheDelete(vlc_object_t *, const char *);
void CacheLoad (vlc_object_t *, module_bank_t *, const char *); void CacheLoad (vlc_object_t *, module_bank_t *, const char *);
void CacheSave (vlc_object_t *, module_bank_t *); void CacheSave (vlc_object_t *, const char *, module_cache_t *const *, size_t);
module_cache_t * CacheFind (module_bank_t *, const char *, int64_t, int64_t); module_cache_t * CacheFind (module_bank_t *, const char *, int64_t, int64_t);
#endif /* !__LIBVLC_MODULES_H */ #endif /* !__LIBVLC_MODULES_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment