Changeset a1f17d4 for storage_text.c


Ignore:
Timestamp:
2005-12-08T14:14:28Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
7989fcf3
Parents:
1ee6c18
Message:

Simplify storage API a bit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • storage_text.c

    r1ee6c18 ra1f17d4  
    3636}
    3737
    38 static int text_load ( const char *my_nick, const char* password, irc_t *irc )
     38static storage_status_t text_load ( const char *my_nick, const char* password, irc_t *irc )
    3939{
    4040        char s[512];
     
    5050        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".accounts" );
    5151        fp = fopen( s, "r" );
    52         if( !fp ) return( 0 );
     52        if( !fp ) return STORAGE_NO_SUCH_USER;
    5353       
    5454        fscanf( fp, "%32[^\n]s", s );
     
    5656        {
    5757                fclose( fp );
    58                 return( -1 );
     58                return STORAGE_INVALID_PASSWORD;
    5959        }
    6060       
     
    7474        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" );
    7575        fp = fopen( s, "r" );
    76         if( !fp ) return( 0 );
     76        if( !fp ) return STORAGE_NO_SUCH_USER;
    7777        while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 )
    7878        {
     
    8888        }
    8989       
    90         return( 1 );
    91 }
    92 
    93 static int text_save( irc_t *irc )
     90        return STORAGE_OK;
     91}
     92
     93static storage_status_t text_save( irc_t *irc, int overwrite )
    9494{
    9595        char s[512];
     
    102102        FILE *fp;
    103103        char *hash;
     104
     105        if (!overwrite) {
     106                g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
     107                if (access( path, F_OK ) != -1)
     108                        return STORAGE_ALREADY_EXISTS;
     109       
     110                g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
     111                if (access( path, F_OK ) != -1)
     112                        return STORAGE_ALREADY_EXISTS;
     113        }
    104114       
    105115        /*\
     
    127137        g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" );
    128138        fp = fopen( path, "w" );
    129         if( !fp ) return( 0 );
     139        if( !fp ) return STORAGE_OTHER_ERROR;
    130140        for( n = irc->nicks; n; n = n->next )
    131141        {
     
    138148                        irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    139149                        fclose( fp );
    140                         return( 0 );
     150                        return STORAGE_OTHER_ERROR;
    141151                }
    142152        }
     
    144154        {
    145155                irc_usermsg( irc, "fclose() reported an error. Disk full?" );
    146                 return( 0 );
     156                return STORAGE_OTHER_ERROR;
    147157        }
    148158 
     
    153163                {
    154164                        irc_usermsg( irc, "Error while removing old .nicks file" );
    155                         return( 0 );
     165                        return STORAGE_OTHER_ERROR;
    156166                }
    157167        }
     
    159169        {
    160170                irc_usermsg( irc, "Error while renaming new .nicks file" );
    161                 return( 0 );
     171                return STORAGE_OTHER_ERROR;
    162172        }
    163173       
    164174        g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" );
    165175        fp = fopen( path, "w" );
    166         if( !fp ) return( 0 );
     176        if( !fp ) return STORAGE_OTHER_ERROR;
    167177        if( fprintf( fp, "%s", hash ) != strlen( hash ) )
    168178        {
    169179                irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    170180                fclose( fp );
    171                 return( 0 );
     181                return STORAGE_OTHER_ERROR;
    172182        }
    173183        g_free( hash );
     
    188198                                irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    189199                                fclose( fp );
    190                                 return( 0 );
     200                                return STORAGE_OTHER_ERROR;
    191201                        }
    192202                }
     
    206216                                        irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    207217                                        fclose( fp );
    208                                         return( 0 );
     218                                        return STORAGE_OTHER_ERROR;
    209219                                }
    210220                        }
     
    223233                                irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    224234                                fclose( fp );
    225                                 return( 0 );
     235                                return STORAGE_OTHER_ERROR;
    226236                        }
    227237                }
     
    231241        {
    232242                irc_usermsg( irc, "fclose() reported an error. Disk full?" );
    233                 return( 0 );
     243                return STORAGE_OTHER_ERROR;
    234244        }
    235245       
     
    240250                {
    241251                        irc_usermsg( irc, "Error while removing old .accounts file" );
    242                         return( 0 );
     252                        return STORAGE_OTHER_ERROR;
    243253                }
    244254        }
     
    246256        {
    247257                irc_usermsg( irc, "Error while renaming new .accounts file" );
    248                 return( 0 );
     258                return STORAGE_OTHER_ERROR;
    249259        }
    250260       
    251261        umask( ou );
    252262       
    253         return( 1 );
    254 }
    255 
    256 static int text_exists( const char *nick )
    257 {
    258         char path[512];
    259         int checkie;
    260 
    261         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
    262         checkie = access( path, F_OK );
    263        
    264         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
    265         checkie += access( path, F_OK );
    266        
    267         return ( checkie != -2 );
    268 }
    269 
    270 static int text_remove( const char *nick )
     263        return STORAGE_OK;
     264}
     265
     266static storage_status_t text_remove( const char *nick, const char *password )
    271267{
    272268        char s[512];
     269        FILE *fp;
     270       
     271        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
     272        fp = fopen( s, "r" );
     273        if (!fp)
     274                return STORAGE_NO_SUCH_USER;
     275
     276        fscanf( fp, "%32[^\n]s", s );
     277        fclose( fp );
     278
     279        /*FIXME Test if password is correct */
    273280
    274281        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
    275282        if (unlink( s ) == -1)
    276                 return( 1 );
     283                return STORAGE_OTHER_ERROR;
    277284       
    278285        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
    279286        if (unlink( s ) == -1)
    280                 return( 1 );
    281 
    282         return( 0 );
    283 }
    284 
    285 static int text_check_pass( const char *nick, const char *password )
    286 {
    287         char s[512];
    288         FILE *fp;
    289        
    290         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
    291         fp = fopen( s, "r" );
    292 
    293         fscanf( fp, "%32[^\n]s", s );
    294         fclose( fp );
    295 
    296         /* FIXME */
    297         return( 0 );
     287                return STORAGE_OTHER_ERROR;
     288
     289        return STORAGE_OK;
    298290}
    299291
     
    301293        .name = "text",
    302294        .init = text_init,
    303         .exists = text_exists,
    304         .check_pass = text_check_pass,
    305295        .remove = text_remove,
    306296        .load = text_load,
Note: See TracChangeset for help on using the changeset viewer.