From 271b3d593a41f117351e5022faad55d538cbe221 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Sat, 30 Jan 2010 23:52:59 +0200 Subject: [PATCH] Save plugins cache into corresponding plugins directory --- src/modules/cache.c | 59 ++++++++++++++++++++----------------------- src/modules/modules.c | 5 ++-- src/modules/modules.h | 2 +- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/modules/cache.c b/src/modules/cache.c index 257ff45dd7..046f997f55 100644 --- a/src/modules/cache.c +++ b/src/modules/cache.c @@ -58,7 +58,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 +96,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 +197,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 +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; int i_submodules; @@ -433,52 +433,49 @@ 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; + + 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; } - 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; - msg_Dbg (p_this, "saving plugins cache %s", psz_filename); - if (CacheSaveBank (file, p_bank)) + msg_Dbg (p_this, "saving plugins cache %s", tmpname); + if (CacheSaveBank (file, pp_cache, n)) goto error; #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); + msg_Warn (p_this, "cannot write %s (%m)", tmpname); + free (filename); + free (tmpname); if (file != NULL) { clearerr (file); @@ -489,7 +486,8 @@ error: 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 +514,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; diff --git a/src/modules/modules.c b/src/modules/modules.c index 2dcf303236..b648861ec2 100644 --- a/src/modules/modules.c +++ b/src/modules/modules.c @@ -214,8 +214,6 @@ void module_LoadPlugins( vlc_object_t * p_this ) p_module_bank->b_cache = var_InheritBool( p_this, "plugins-cache" ); AllocateAllPlugins( p_this, p_module_bank ); - if( p_module_bank->b_cache ) - CacheSave( p_this, p_bank ); config_UnsortConfig (); config_SortConfig (); } @@ -865,6 +863,7 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank ) if( !path ) continue; + size_t offset = p_module_bank->i_loaded_cache; if( b_reset ) CacheDelete( p_this, path ); else @@ -875,6 +874,8 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank ) /* Don't go deeper than 5 subdirectories */ 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 ); } diff --git a/src/modules/modules.h b/src/modules/modules.h index 7469832f3e..9aa911f698 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -160,7 +160,7 @@ void module_Unload (module_handle_t); void CacheMerge (vlc_object_t *, module_t *, module_t *); void CacheDelete(vlc_object_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); #endif /* !__LIBVLC_MODULES_H */ -- 2.39.2