Changeset 7eba9008c5a57a90048772adb5e37be4327d309a

Show
Ignore:
Timestamp:
12/12/07 21:22:32 (10 months ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1197490952 +0000
git-parent:

[b25337f62a87919e1c9173d85338903d8745ac9d]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1197490952 +0000
Message:

Handle I/O errors while writing the cache file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/modules/cache.c

    re1c8708 r7eba900  
    7676#ifdef HAVE_DYNAMIC_PLUGINS 
    7777static int    CacheLoadConfig  ( module_t *, FILE * ); 
    78 static void   CacheSaveConfig  ( module_t *, FILE * ); 
     78static int    CacheSaveConfig  ( module_t *, FILE * ); 
    7979static char * CacheName        ( void ); 
    8080 
     
    452452    int i, j, i_cache; 
    453453    module_cache_t **pp_cache; 
    454     int32_t i_file_size = 0; 
     454    uint32_t i_file_size = 0; 
    455455    libvlc_global_data_t *p_libvlc_global = vlc_global(); 
    456456 
     
    458458    if( !psz_cachedir ) /* XXX: this should never happen */ 
    459459    { 
    460         msg_Err( p_this, "Unable to get cache directory" ); 
     460        msg_Err( p_this, "unable to get cache directory" ); 
    461461        return; 
    462462    } 
     
    468468              "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir ); 
    469469    file = utf8_fopen( psz_filename, "wb" ); 
    470     if( file ) 
    471     { 
    472         fwrite( psz_tag, 1, strlen(psz_tag), file ); 
     470    if (file != NULL) 
     471    { 
     472        if (fwrite (psz_tag, 1, sizeof (psz_tag) - 1, file) != 1) 
     473            clearerr (file); /* what else can we do? */ 
    473474        fclose( file ); 
    474475    } 
     
    476477    snprintf( psz_filename, sizeof( psz_filename ), 
    477478              "%s"DIR_SEP"%s", psz_cachedir, CacheName() ); 
    478     msg_Dbg( p_this, "saving plugins cache file %s", psz_filename ); 
     479    msg_Dbg( p_this, "writing plugins cache %s", psz_filename ); 
    479480 
    480481    file = utf8_fopen( psz_filename, "wb" ); 
    481     if( !file ) 
    482     { 
    483         msg_Warn( p_this, "could not open plugins cache file %s for writing", 
    484                   psz_filename ); 
    485         return; 
    486     } 
     482    if (file == NULL) 
     483        goto error; 
    487484 
    488485    /* Empty space for file size */ 
    489     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file ); 
     486    if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1) 
     487        goto error; 
    490488 
    491489    /* Contains version number */ 
    492     fprintf( file, "%s", "cache " COPYRIGHT_MESSAGE ); 
     490    if (fputs ("cache "COPYRIGHT_MESSAGE, file) == EOF) 
     491        goto error; 
    493492 
    494493    /* Sub-version number (to avoid breakage in the dev version when cache 
    495494     * structure changes) */ 
    496495    i_file_size = CACHE_SUBVERSION_NUM; 
    497     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file ); 
     496    if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1 ) 
     497        goto error; 
    498498 
    499499    /* Language */ 
    500     fprintf( file, "%5.5s", _("C") ); 
     500    if (fprintf (file, "%5.5s", _("C")) == EOF) 
     501        goto error; 
    501502 
    502503    /* Header marker */ 
    503504    i_file_size = ftell( file ); 
    504     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file ); 
     505    if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1) 
     506        goto error; 
    505507 
    506508    i_cache = p_libvlc_global->p_module_bank->i_cache; 
    507509    pp_cache = p_libvlc_global->p_module_bank->pp_cache; 
    508510 
    509     fwrite( &i_cache, sizeof(char), sizeof(i_cache), file ); 
    510  
    511 #define SAVE_IMMEDIATE(a) \ 
    512     fwrite( &a, sizeof(char), sizeof(a), file ) 
    513 #define SAVE_STRING(a) \ 
    514     { i_size = a ? strlen( a ) + 1 : 0; \ 
    515       fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \ 
    516       if( a ) fwrite( a, sizeof(char), i_size, file ); \ 
     511    if (fwrite( &i_cache, sizeof (i_cache), 1, file) != 1) 
     512        goto error; 
     513 
     514#define SAVE_IMMEDIATE( a ) \ 
     515    if (fwrite (&a, sizeof(a), 1, file) != 1) \ 
     516        goto error 
     517#define SAVE_STRING( a ) \ 
     518    { \ 
     519        uint16_t i_size = (a != NULL) ? (strlen (a) + 1) : 0; \ 
     520        if ((fwrite (&i_size, sizeof (i_size), 1, file) != 1) \ 
     521         || (a && (fwrite (a, 1, i_size, file) != i_size))) \ 
     522            goto error; \ 
    517523    } while(0) 
    518524 
    519525    for( i = 0; i < i_cache; i++ ) 
    520526    { 
    521         uint16_t i_size; 
    522527        uint32_t i_submodule; 
    523528 
     
    547552 
    548553        /* Config stuff */ 
    549         CacheSaveConfig( pp_cache[i]->p_module, file ); 
     554        if (CacheSaveConfig (pp_cache[i]->p_module, file)) 
     555            goto error; 
    550556 
    551557        SAVE_STRING( pp_cache[i]->p_module->psz_filename ); 
     
    579585    i_file_size = ftell( file ); 
    580586    fseek( file, 0, SEEK_SET ); 
    581     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file ); 
    582  
    583     fclose( file ); 
     587    if (fwrite (&i_file_size, sizeof (i_file_size), 1, file) != 1) 
     588        goto error; 
     589 
     590    if (fclose (file) == 0) 
     591        return; /* success! */ 
     592 
     593    file = NULL; 
     594error: 
     595    msg_Warn (p_this, "could not write plugins cache %s (%m)", 
     596              psz_filename); 
     597    if (file != NULL) 
     598    { 
     599        clearerr (file); 
     600        fclose (file); 
     601    } 
    584602} 
    585603 
    586 static void CacheSaveConfig( module_t *p_module, FILE *file ) 
     604static int CacheSaveConfig( module_t *p_module, FILE *file ) 
    587605{ 
    588606    uint32_t i_lines = p_module->confsize; 
    589     uint16_t i_size; 
    590607 
    591608    SAVE_IMMEDIATE( p_module->i_config_items ); 
     
    630647        SAVE_IMMEDIATE( p_module->p_config[i].pf_callback ); 
    631648    } 
     649    return 0; 
     650 
     651error: 
     652    return -1; 
    632653} 
    633654