Changeset 5898ef8 for storage_xml.c


Ignore:
Timestamp:
2006-06-15T12:46:31Z (17 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
10efa91
Parents:
c121f89 (diff), 79e826a (diff), 3af70b0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

xml_save(), decent error handling and merging fixed autosaving.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • storage_xml.c

    rc121f89 r5898ef8  
    169169static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
    170170{
     171        struct xml_parsedata *xd = data;
     172        // irc_t *irc = xd->irc;
     173       
     174        if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting )
     175        {
     176                g_free( xd->current_setting );
     177                xd->current_setting = NULL;
     178        }
     179        else if( g_strcasecmp( element_name, "account" ) == 0 )
     180        {
     181                xd->current_account = NULL;
     182        }
    171183}
    172184
     
    188200                xd->current_setting = NULL;
    189201        }
    190 }
    191 
    192 static void xml_error( GMarkupParseContext *ctx, GError *error, gpointer data )
    193 {
    194202}
    195203
     
    200208        xml_text,
    201209        NULL,
    202         xml_error
     210        NULL
    203211};
    204212
     
    219227        int fd, st;
    220228       
    221         if( irc->status >= USTATUS_IDENTIFIED )
     229        if( irc->status & USTATUS_IDENTIFIED )
    222230                return( 1 );
    223231       
     
    244252                {
    245253                        g_markup_parse_context_free( ctx );
     254                        close( fd );
    246255                       
    247                         /* TODO: Display useful error msg */
    248                        
     256                        /* Slightly dirty... */
    249257                        if( gerr && strcmp( gerr->message, XML_PASS_ERRORMSG ) == 0 )
    250258                                return STORAGE_INVALID_PASSWORD;
    251259                        else
     260                        {
     261                                if( gerr )
     262                                        irc_usermsg( irc, "Error from XML-parser: %s", gerr->message );
     263                               
    252264                                return STORAGE_OTHER_ERROR;
     265                        }
    253266                }
    254267        }
    255268       
    256269        g_markup_parse_context_free( ctx );
    257        
    258         irc->status = USTATUS_IDENTIFIED;
     270        close( fd );
     271       
     272        irc->status |= USTATUS_IDENTIFIED;
    259273       
    260274        if( set_getint( irc, "auto_connect" ) )
     
    268282}
    269283
     284static int xml_printf( int fd, char *fmt, ... )
     285{
     286        va_list params;
     287        char *out;
     288        int len;
     289       
     290        va_start( params, fmt );
     291        out = g_markup_vprintf_escaped( fmt, params );
     292        va_end( params );
     293       
     294        len = strlen( out );
     295        len -= write( fd, out, len );
     296        g_free( out );
     297       
     298        return len == 0;
     299}
     300
    270301static storage_status_t xml_save( irc_t *irc, int overwrite )
    271302{
    272 /*      if (!overwrite) {
    273                 g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
    274                 if (access( path, F_OK ) != -1)
    275                         return STORAGE_ALREADY_EXISTS;
    276        
    277                 g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
    278                 if (access( path, F_OK ) != -1)
    279                         return STORAGE_ALREADY_EXISTS;
    280         }
    281 */     
     303        char path[512], *path2;
     304        set_t *set;
     305        nick_t *nick;
     306        account_t *acc;
     307        int fd;
     308       
     309        g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, irc->nick, ".xml" );
     310       
     311        if( !overwrite && access( path, F_OK ) != -1 )
     312                return STORAGE_ALREADY_EXISTS;
     313       
     314        strcat( path, "~" );
     315        if( ( fd = open( path, O_WRONLY | O_CREAT, 0600 ) ) < 0 )
     316        {
     317                irc_usermsg( irc, "Error while opening configuration file." );
     318                return STORAGE_OTHER_ERROR;
     319        }
     320       
     321        if( !xml_printf( fd, "<user nick=\"%s\" password=\"%s\">\n", irc->nick, irc->password ) )
     322                goto write_error;
     323       
     324        for( set = irc->set; set; set = set->next )
     325                if( set->value && set->def )
     326                        if( !xml_printf( fd, "\t<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
     327                                goto write_error;
     328       
     329        for( acc = irc->accounts; acc; acc = acc->next )
     330        {
     331                if( !xml_printf( fd, "\t<account protocol=\"%s\" handle=\"%s\" password=\"%s\" autoconnect=\"%s\"", acc->prpl->name, acc->user, acc->pass, "yes" ) )
     332                        goto write_error;
     333                if( acc->server && acc->server[0] && !xml_printf( fd, " server=\"%s\"", acc->server ) )
     334                        goto write_error;
     335                if( !xml_printf( fd, ">\n" ) )
     336                        goto write_error;
     337               
     338                for( nick = irc->nicks; nick; nick = nick->next )
     339                        if( nick->proto == acc->prpl )
     340                                if( !xml_printf( fd, "\t\t<buddy handle=\"%s\" nick=\"%s\" />\n", nick->handle, nick->nick ) )
     341                                        goto write_error;
     342               
     343                if( !xml_printf( fd, "\t</account>\n" ) )
     344                        goto write_error;
     345        }
     346       
     347        if( !xml_printf( fd, "</user>\n" ) )
     348                goto write_error;
     349       
     350        close( fd );
     351       
     352        path2 = g_strndup( path, strlen( path ) - 1 );
     353        if( rename( path, path2 ) != 0 )
     354        {
     355                irc_usermsg( irc, "Error while renaming temporary configuration file." );
     356               
     357                g_free( path2 );
     358                unlink( path );
     359               
     360                return STORAGE_OTHER_ERROR;
     361        }
     362       
     363        g_free( path2 );
     364       
    282365        return STORAGE_OK;
     366
     367write_error:
     368        irc_usermsg( irc, "Write error. Disk full?" );
     369        close( fd );
     370       
     371        return STORAGE_OTHER_ERROR;
    283372}
    284373
Note: See TracChangeset for help on using the changeset viewer.