Mercurial > noffle
changeset 281:5eece4dfd945 noffle
[svn] * src/log.c,src/log.h: Add Log_fatal() for reporting fatal errors
and exiting, Log_gdbm_fatal() for the the same but specifically as
a GDBM error reporting function, and a new log debug level AUTH for
a forthcoming authentication mechanism.
* src/database.c,src/group.c: Provide new gdbm error function to all
gdbm opens.
* src/noffle.c: Add atexit() to always close databases on a program-
inspired exit.
* src/content.c,src/dynamicstring.c,src/fetchlist.c,src/filter.c,
src/itemlist.c,src/log.c,src/log.h,src/over.c,src/protocol.h,
src/request.c,src/util.c: Use Log_fatal where appropriate.
author | bears |
---|---|
date | Fri, 27 Dec 2002 21:48:25 +0000 |
parents | 9c54bf672ca1 |
children | baa6408d1bbc |
files | ChangeLog src/content.c src/database.c src/dynamicstring.c src/fetchlist.c src/filter.c src/group.c src/itemlist.c src/log.c src/log.h src/noffle.c src/over.c src/protocol.h src/request.c src/util.c |
diffstat | 15 files changed, 335 insertions(+), 294 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Tue Dec 24 09:08:59 2002 +0000 +++ b/ChangeLog Fri Dec 27 21:48:25 2002 +0000 @@ -1,3 +1,17 @@ +Fri Dec 27 2002 Jim Hague <jim.hague@acm.org> + +* src/log.c,src/log.h: Add Log_fatal() for reporting fatal errors + and exiting, Log_gdbm_fatal() for the the same but specifically as + a GDBM error reporting function, and a new log debug level AUTH for + a forthcoming authentication mechanism. +* src/database.c,src/group.c: Provide new gdbm error function to all + gdbm opens. +* src/noffle.c: Add atexit() to always close databases on a program- + inspired exit. +* src/content.c,src/dynamicstring.c,src/fetchlist.c,src/filter.c, + src/itemlist.c,src/log.c,src/log.h,src/over.c,src/protocol.h, + src/request.c,src/util.c: Use Log_fatal where appropriate. + Tue Dec 24 2002 Jim Hague <jim.hague@acm.org> * src/client.c: doGetGrps() was supposed to fall back to "LIST" if
--- a/src/content.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/content.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* content.c - $Id: content.c 387 2002-06-26 13:15:44Z bears $ + $Id: content.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -50,10 +50,7 @@ if ( ! ( cont.elem = realloc( cont.elem, ( cont.max + 500 ) * sizeof( cont.elem[ 0 ] ) ) ) ) - { - Log_err( "Could not realloc overview list" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Could not realloc overview list" ); cont.max += 500; } ASSERT( cont.vecFirst > 0 );
--- a/src/database.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/database.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* database.c - $Id: database.c 408 2002-11-10 18:31:25Z bears $ + $Id: database.c 413 2002-12-27 21:48:25Z bears $ Uses GNU gdbm library. Using Berkeley db (included in libc6) was cumbersome. It is based on Berkeley db 1.85, which has severe bugs @@ -79,7 +79,7 @@ snprintf( name, MAXCHAR, ARTICLE_FILENAME_FMT, Cfg_spoolDir() ); flags = GDBM_WRCREAT | GDBM_FAST; - if ( ! ( db.dbf = gdbm_open( name, 512, flags, 0644, NULL ) ) ) + if ( ! ( db.dbf = gdbm_open( name, 512, flags, 0644, Log_gdbm_fatal ) ) ) { Log_err( "Error opening %s for r/w (%s)", name, errMsg() ); return FALSE; @@ -625,7 +625,7 @@ snprintf( name, MAXCHAR, ARTICLE_NEW_FILENAME_FMT, Cfg_spoolDir() ); flags = GDBM_WRCREAT | GDBM_FAST; - if ( ! ( dbNew.dbf = gdbm_open( name, 512, flags, 0644, NULL ) ) ) + if ( ! ( dbNew.dbf = gdbm_open( name, 512, flags, 0644, Log_gdbm_fatal ) ) ) { Log_err( "Error opening %s for r/w (%s)", name, errMsg() ); return FALSE;
--- a/src/dynamicstring.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/dynamicstring.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* dynamicstring.c - $Id: dynamicstring.c 316 2001-10-31 11:44:53Z bears $ + $Id: dynamicstring.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -27,10 +27,7 @@ if ( max <= self->max ) return; if ( ! ( self->str = (char *)realloc( self->str, (size_t)max ) ) ) - { - Log_err( "Realloc of DynStr failed" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Realloc of DynStr failed" ); if ( self->max == 0 ) /* First allocation? */ *(self->str) = '\0'; self->max = max; @@ -42,10 +39,7 @@ DynStr *s; if ( ! ( s = malloc( sizeof( DynStr ) ) ) ) - { - Log_err( "Allocation of DynStr failed" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Allocation of DynStr failed" ); s->len = 0; s->max = 0; s->str = NULL;
--- a/src/fetchlist.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/fetchlist.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* fetchlist.c - $Id: fetchlist.c 368 2002-02-14 17:14:34Z bears $ + $Id: fetchlist.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -68,10 +68,7 @@ = realloc( fetchlist.elem, ( fetchlist.max + 50 ) * sizeof( fetchlist.elem[ 0 ] ) ) ) ) - { - Log_err( "Could not realloc fetchlist" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Could not realloc fetchlist" ); fetchlist.max += 50; } strcpy( elem.name, name );
--- a/src/filter.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/filter.c Fri Dec 27 21:48:25 2002 +0000 @@ -3,7 +3,7 @@ Article filtering. - $Id: filter.c 381 2002-05-14 14:25:45Z mirkol $ + $Id: filter.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -220,10 +220,7 @@ ( filter.maxFilters + 5 ) * sizeof( Filter * ) ); if ( filter.filters == NULL ) - { - Log_err( "Could not realloc filter list" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Could not realloc filter list" ); filter.maxFilters += 5; } filter.filters[ filter.nFilters++ ] = f; @@ -348,10 +345,7 @@ Filter *f; if ( ! ( f = ( Filter * ) malloc( sizeof( Filter ) ) ) ) - { - Log_err( "Cannot allocate Filter" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Cannot allocate Filter" ); f->nRules = 0; f->maxRules = 0; f->rules = NULL; @@ -421,10 +415,7 @@ * sizeof( FilterRule ) ); if ( f->rules == NULL ) - { - Log_err( "Could not realloc rule list" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Could not realloc rule list" ); f->maxRules += 5; } f->rules[ f->nRules++ ] = rule;
--- a/src/group.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/group.c Fri Dec 27 21:48:25 2002 +0000 @@ -7,7 +7,7 @@ loadGrp() and saveGrp(). This is done transparently. Access to the groups database is done by group name, by the functions defined in group.h. - $Id: group.c 396 2002-08-05 22:05:02Z bears $ + $Id: group.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -100,7 +100,7 @@ ASSERT( grp.dbf == NULL ); snprintf( name, MAXCHAR, "%s/data/groupinfo.gdbm", Cfg_spoolDir() ); flags = GDBM_WRCREAT | GDBM_FAST; - if ( ! ( grp.dbf = gdbm_open( name, 512, flags, 0644, NULL ) ) ) + if ( ! ( grp.dbf = gdbm_open( name, 512, flags, 0644, Log_gdbm_fatal ) ) ) { Log_err( "Error opening %s for r/w (%s)", errMsg() ); return FALSE;
--- a/src/itemlist.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/itemlist.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* itemlist.c - $Id: itemlist.c 316 2001-10-31 11:44:53Z bears $ + $Id: itemlist.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -39,17 +39,11 @@ res = malloc( sizeof( ItemList ) ); if ( res == NULL ) - { - Log_err( "Malloc of ItemList failed." ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Malloc of ItemList failed." ); res->list = malloc ( strlen(list) + 2 ); if ( res->list == NULL ) - { - Log_err( "Malloc of ItemList.list failed." ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Malloc of ItemList.list failed." ); strcpy( res->list, list ); res->count = 0;
--- a/src/log.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/log.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* log.c - $Id: log.c 406 2002-11-10 15:24:43Z bears $ + $Id: log.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -11,6 +11,7 @@ #include <syslog.h> #include <stdarg.h> #include "common.h" +#include "lock.h" #include "log.h" #include "portable.h" @@ -92,3 +93,25 @@ log.debugMask = mask; } + +/* + * A fatal error. Log it, close down as much as possible and + * exit with EXIT_FAILURE. + */ +void +Log_fatal( const char *fmt, ... ) +{ + DO_LOG( LOG_ERR ); + exit( EXIT_FAILURE ); + /* NOTREACHED */ +} + +/* Fatal error function for gdbm */ +void +Log_gdbm_fatal( const char *msg ) +{ + Log_fatal( "gdbm: %s", msg ); + /* NOTREACHED */ +} + +
--- a/src/log.h Tue Dec 24 09:08:59 2002 +0000 +++ b/src/log.h Fri Dec 27 21:48:25 2002 +0000 @@ -3,7 +3,7 @@ Print log messages to syslog, stdout/stderr. - $Id: log.h 300 2001-08-05 08:24:22Z bears $ + $Id: log.h 413 2002-12-27 21:48:25Z bears $ */ #ifndef LOG_H @@ -31,6 +31,7 @@ #define LOG_DBG_PROTOCOL (0x0100) #define LOG_DBG_REQUESTS (0x0200) #define LOG_DBG_SERVER (0x0400) +#define LOG_DBG_AUTH (0x0800) #define LOG_DBG_ALL (0xffff) #define LOG_DBG_NONE (0x0000) @@ -68,4 +69,12 @@ void Log_setDbgMask( unsigned mask ); +/* Fatal error */ +void +Log_fatal( const char *fmt, ... ); + +/* Fatal error in gdbm */ +void +Log_gdbm_fatal( const char *msg ); + #endif
--- a/src/noffle.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/noffle.c Fri Dec 27 21:48:25 2002 +0000 @@ -10,7 +10,7 @@ received for some seconds (to allow multiple clients connect at the same time). - $Id: noffle.c 402 2002-11-10 11:30:40Z bears $ + $Id: noffle.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -613,7 +613,7 @@ static void closeNoffle( void ) { - if ( noffle.lockAtStartup ) + if ( Lock_gotLock() ) Lock_closeDatabases(); Lock_syncDatabases(); } @@ -730,7 +730,7 @@ f = fopen( argv[0], "r" ); if ( f == NULL ) { - Log_err( "Can't access %s (%s).", argv[0], strerror( errno ) ); + fprintf( stderr, "Can't access %s (%s).", argv[0], strerror( errno ) ); return EXIT_FAILURE; } } @@ -745,7 +745,6 @@ if ( f != stdin ) fclose( f ); - closeNoffle(); return result; } @@ -804,6 +803,9 @@ int c, result; const char *cmdname, *p; + /* Attempt to ensure databases are properly closed when we exit */ + atexit( closeNoffle ); + signal( SIGSEGV, bugReport ); signal( SIGABRT, logSignal ); signal( SIGFPE, logSignal ); @@ -1011,6 +1013,5 @@ default: abort(); /* Never reached */ } - closeNoffle(); return result; }
--- a/src/over.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/over.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* over.c - $Id: over.c 408 2002-11-10 18:31:25Z bears $ + $Id: over.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -70,10 +70,7 @@ Over *ov; if ( ! ( ov = malloc( sizeof( Over ) ) ) ) - { - Log_err( "Cannot allocate Over" ); - exit( EXIT_FAILURE ); - } + Log_fatal( "Cannot allocate Over" ); ov->numb = 0; Utl_allocAndCpy( &ov->subj, subj ); Utl_allocAndCpy( &ov->from, from );
--- a/src/protocol.h Tue Dec 24 09:08:59 2002 +0000 +++ b/src/protocol.h Fri Dec 27 21:48:25 2002 +0000 @@ -4,7 +4,7 @@ Functions related with the NNTP protocol which are useful for both the server and the client. - $Id: protocol.h 316 2001-10-31 11:44:53Z bears $ + $Id: protocol.h 413 2002-12-27 21:48:25Z bears $ */ #ifndef PRT_H @@ -33,11 +33,10 @@ #define STAT_OVERS_FOLLOW 224 #define STAT_NEW_GRP_FOLLOW 231 #define STAT_POST_OK 240 -#define STAT_AUTH_ACCEPTED 250 -#define STAT_AUTH_ACCEPTED_DEPREC 281 +#define STAT_AUTH_ACCEPTED 281 #define STAT_SEND_ART 340 -#define STAT_MORE_AUTH_REQUIRED_DEPREC 381 +#define STAT_MORE_AUTH_REQUIRED 381 #define STAT_NO_SUCH_GRP 411 #define STAT_NO_GRP_SELECTED 412 @@ -48,16 +47,20 @@ #define STAT_NO_SUCH_ID 430 #define STAT_ART_REJECTED 437 #define STAT_POST_FAILED 441 -#define STAT_AUTH_REQUIRED 450 -#define STAT_AUTH_REJECTED 452 -#define STAT_AUTH_REQUIRED_DEPREC 480 -#define STAT_AUTH_REJECTED_DEPREC 482 +#define STAT_AUTH_REQUIRED 480 +#define STAT_AUTH_REJECTED 482 #define STAT_NO_SUCH_CMD 500 #define STAT_SYNTAX_ERR 501 #define STAT_NO_PERMISSION 502 #define STAT_PROGRAM_FAULT 503 +/* Statuses from AUTHINFO SIMPLE - deprecated */ +#define STAT_AUTH_ACCEPTED_DEPREC 250 +#define STAT_AUTH_REQUIRED_DEPREC 450 +#define STAT_AUTH_REJECTED_DEPREC 452 +#define STAT_MORE_AUTH_REQUIRED_DEPREC 350 + /* Read next line from f into Str, up to "\n" or "\r\n". Don't save "\n" or "\r\n" in line. Terminate with '\0'.
--- a/src/request.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/request.c Fri Dec 27 21:48:25 2002 +0000 @@ -3,7 +3,7 @@ Collection of articles that are marked for download. - $Id: request.c 381 2002-05-14 14:25:45Z mirkol $ + $Id: request.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -34,19 +34,19 @@ typedef struct Reqserv Reqserv; struct Reqserv { - char* serv; /* Server the messages are to be requested - from */ - char** reql; /* List of message IDs of requested - messages. Some entries (that have been - deleted) may be NULL */ - int reql_length; /* Number of string pointers in reql, - including NULL entries */ - int reql_capacity; /* maximum number of string pointers reql - can hold */ - Bool dirty; /* whether the request list needs to be - rewritten to disk */ - Reqserv* next; /* next Reqserv in list */ - time_t mtime; /* last modification time of request file */ + char* serv; /* Server the messages are to be requested + from */ + char** reql; /* List of message IDs of requested + messages. Some entries (that have been + deleted) may be NULL */ + int reql_length; /* Number of string pointers in reql, + including NULL entries */ + int reql_capacity; /* maximum number of string pointers reql + can hold */ + Bool dirty; /* whether the request list needs to be + rewritten to disk */ + Reqserv* next; /* next Reqserv in list */ + time_t mtime; /* last modification time of request file */ }; /* List of servers */ @@ -61,39 +61,46 @@ /* local functions */ -static Reqserv* newReqserv (const char* serv); -static Bool getReqserv (const char* serv, Reqserv** rsz); -static void fileRequest (Str file, const char *serv); -static char** searchMsgId (const Reqserv * rs, const char *msgId); -static Bool storeMsgId (Reqserv* rs, const char* msgId); -static Bool readRequestfile (const char* serv, Reqserv** rsz); -static time_t get_mtime (const char* serv); +static Reqserv* newReqserv ( const char* serv ); +static Bool getReqserv ( const char* serv, Reqserv** rsz ); +static void fileRequest ( Str file, const char *serv ); +static char** searchMsgId ( const Reqserv * rs, const char *msgId ); +static Bool storeMsgId ( Reqserv* rs, const char* msgId ); +static Bool readRequestfile ( const char* serv, Reqserv** rsz ); +static time_t get_mtime ( const char* serv ); /* read modification time of request file */ -static time_t get_mtime(const char* serv) +static time_t +get_mtime( const char* serv ) { - Str filename; - struct stat stat1; + Str filename; + struct stat stat1; - fileRequest(filename, serv); - stat(filename, &stat1); - return stat1.st_mtime; + fileRequest( filename, serv ); + stat( filename, &stat1 ); + return stat1.st_mtime; } /* create new Reqserv and queue it */ -static Reqserv* newReqserv(const char* serv) +static Reqserv* +newReqserv( const char* serv ) { - Reqserv* rs = malloc(sizeof(Reqserv)); - rs->serv = strcpy(malloc(strlen(serv)+1), serv); - rs->reql = 0; - rs->reql_length = 0; - rs->reql_capacity = 0; - rs->next = reqserv; - rs->dirty = FALSE; - rs->mtime = 0; - reqserv = rs; - return rs; + Reqserv *rs; + + rs = malloc( sizeof( Reqserv ) ); + if ( rs == NULL ) + Log_fatal( "Malloc of Reqserv failed." ); + + Utl_allocAndCpy( &(rs->serv), serv ); + rs->reql = 0; + rs->reql_length = 0; + rs->reql_capacity = 0; + rs->next = reqserv; + rs->dirty = FALSE; + rs->mtime = 0; + reqserv = rs; + return rs; } @@ -101,15 +108,18 @@ necessary. Return TRUE on success. Otherwise log errors and return FALSE. (details in errno) */ -static Bool getReqserv(const char* serv, Reqserv** rsz) +static Bool +getReqserv( const char* serv, Reqserv** rsz ) { - Reqserv* rs; - for (rs = reqserv; rs; rs = rs->next) - if (!strcmp(serv, rs->serv)) { - *rsz = rs; - return TRUE; - } - return readRequestfile(serv, rsz); + Reqserv* rs; + + for ( rs = reqserv; rs; rs = rs->next ) + if ( !strcmp( serv, rs->serv ) ) + { + *rsz = rs; + return TRUE; + } + return readRequestfile( serv, rsz ); } @@ -117,94 +127,96 @@ static void cleanupReqserv( void ) { - Reqserv *rs, *prev, *next; + Reqserv *rs, *prev, *next; - rs = reqserv; - prev = NULL; - while ( rs != NULL ) - { - ASSERT( ! rs->dirty ); - next = rs->next; - if ( get_mtime( rs->serv ) != rs->mtime ) - { - if ( prev != NULL ) - prev->next = next; - else - reqserv = next; - free( rs->serv ); - rs->serv = NULL; - free( rs->reql ); - rs->reql = NULL; - free( rs ); - } - prev = rs; - rs = next; - } + rs = reqserv; + prev = NULL; + while ( rs != NULL ) + { + ASSERT( ! rs->dirty ); + next = rs->next; + if ( get_mtime( rs->serv ) != rs->mtime ) + { + if ( prev != NULL ) + prev->next = next; + else + reqserv = next; + free( rs->serv ); + rs->serv = NULL; + free( rs->reql ); + rs->reql = NULL; + free( rs ); + } + prev = rs; + rs = next; + } } /* Save name of file storing requests from server "serv" in "file" */ -static void fileRequest( Str file, const char *serv) +static void +fileRequest( Str file, const char *serv ) { - snprintf( file, MAXCHAR, "%s/requested/%s", Cfg_spoolDir(), serv); + snprintf( file, MAXCHAR, "%s/requested/%s", Cfg_spoolDir(), serv); } /* Search for msgid in Reqserv. Return pointer to list entry. Return 0 if list does not contain msgid. */ -static char** searchMsgId(const Reqserv * rs, const char *msgId ) +static char** +searchMsgId( const Reqserv * rs, const char *msgId ) { - char** rz; - ASSERT(rs != 0); + char** rz; + ASSERT( rs != 0 ); - if (!rs->reql) + if ( !rs->reql ) + return 0; + + for ( rz = rs->reql; rz < rs->reql + rs->reql_length; rz++ ) + if ( *rz && !strcmp( *rz, msgId ) ) + return rz; + return 0; - - for (rz = rs->reql; rz < rs->reql + rs->reql_length; rz++) - if (*rz && !strcmp(*rz, msgId)) - return rz; - - return 0; } Bool -Req_contains(const char *serv, const char *msgId) +Req_contains( const char *serv, const char *msgId ) { - Reqserv* rs; - ASSERT( is_open ); - if (getReqserv(serv, &rs) == FALSE) - return FALSE; - return searchMsgId(rs, msgId) ? TRUE : FALSE; + Reqserv *rs; + ASSERT( is_open ); + if (getReqserv( serv, &rs ) == FALSE) + return FALSE; + return ( searchMsgId( rs, msgId ) != NULL ); } -static Bool storeMsgId(Reqserv* rs, const char* msgId) +static Bool +storeMsgId( Reqserv* rs, const char* msgId ) { - char* msgid; + char *msgid; - if (searchMsgId(rs, msgId)) - /* already recorded */ - return FALSE; + if ( searchMsgId( rs, msgId ) ) + /* already recorded */ + return FALSE; - msgid = strcpy(malloc(strlen(msgId)+1), msgId); + Utl_allocAndCpy( &msgid, msgId ); - if (rs->reql_length >= rs->reql_capacity) { - int c1 = rs->reql_capacity*2 + 10; - if ( ! ( rs->reql = (char**) realloc(rs->reql, c1*sizeof(char*) ) ) ) + if ( rs->reql_length >= rs->reql_capacity ) { - Log_err( "Could not realloc requests." ); - exit( EXIT_FAILURE ); + int c1 = rs->reql_capacity * 2 + 10; + rs->reql = ( char ** ) realloc( rs->reql, c1 * sizeof( char * ) ); + if ( rs->reql == NULL ) + Log_fatal( "Could not realloc requests." ); + rs->reql_capacity = c1; } - rs->reql_capacity = c1; - } - *(rs->reql + rs->reql_length++) = msgid; - rs->dirty = TRUE; - return TRUE; + *( rs->reql + rs->reql_length++ ) = msgid; + rs->dirty = TRUE; + return TRUE; } static Bool -appRequest (Reqserv* rs, const char *msgId) +appRequest( Reqserv* rs, const char *msgId ) { Str filename; FILE* file; @@ -213,19 +225,19 @@ Log_dbg( LOG_DBG_REQUESTS, "appending to request file %s", filename ); if (Log_check((file = fopen(filename, "a")) != 0, - "could not open %s for appending: %s", - filename, strerror(errno))) - return FALSE; + "could not open %s for appending: %s", + filename, strerror(errno))) + return FALSE; if (Log_check( fputs(msgId, file) != EOF - && fputs("\n", file) != EOF, - "write error: %s", strerror(errno))) - return FALSE; + && fputs("\n", file) != EOF, + "write error: %s", strerror(errno))) + return FALSE; if (Log_check(fclose(file) != EOF, - "could not close %s properly: %s\n", - filename, strerror(errno))) - return FALSE; + "could not close %s properly: %s\n", + filename, strerror(errno))) + return FALSE; return TRUE; } @@ -234,16 +246,17 @@ /* Add request for message "msgIg" from server "serv". Return TRUE iff successful. */ -Bool Req_add(const char *serv, const char *msgId) +Bool +Req_add( const char *serv, const char *msgId ) { Reqserv* rs; ASSERT( is_open ); Log_dbg( LOG_DBG_REQUESTS, "Marking %s on %s for download", msgId, serv ); - if (getReqserv(serv, &rs) == FALSE) - return FALSE; - if (storeMsgId(rs, msgId) == FALSE) /* already recorded */ - return TRUE; + if ( ! getReqserv( serv, &rs ) ) + return FALSE; + if ( ! storeMsgId(rs, msgId) ) /* already recorded */ + return TRUE; return appRequest(rs, msgId); } @@ -264,81 +277,86 @@ "rsz" and return TRUE on success. Returns FALSE on failure, see errno. If the file doesn't exist, an empty Reqserv is returned. */ -static Bool readRequestfile(const char* serv, Reqserv** rsz) +static Bool +readRequestfile( const char* serv, Reqserv** rsz ) { - Str filename; - Str line; - FILE* file; - Reqserv* rs; + Str filename; + Str line; + FILE* file; + Reqserv* rs; + + fileRequest( filename, serv ); + Log_dbg( LOG_DBG_REQUESTS, "reading request file %s", filename ); - fileRequest(filename, serv); - Log_dbg( LOG_DBG_REQUESTS, "reading request file %s", filename ); + file = fopen( filename, "r" ); + if ( !file && ( errno == ENOENT ) ) + { + *rsz = newReqserv( serv ); + (*rsz)->mtime = get_mtime( serv ); + return TRUE; + } + if ( Log_check( file != 0, + "could not open %s for reading: %s", + filename, strerror( errno ) ) ) + return FALSE; + + rs = *rsz = newReqserv( serv ); - file = fopen(filename, "r"); - if (!file && (errno == ENOENT)) { - *rsz = newReqserv(serv); - (*rsz)->mtime = get_mtime(serv); + while( readLn( line, file ) ) + { + char *line1 = Utl_stripWhiteSpace( line ); + if ( *line1 ) + storeMsgId( rs, line1 ); + } + + rs->dirty = FALSE; + + if ( Log_check( fclose( file ) != EOF, + "could not close %s properly: %s\n", + filename, strerror( errno ) ) ) + return FALSE; + return TRUE; - } - if (Log_check(file != 0, - "could not open %s for reading: %s", - filename, strerror(errno))) - return FALSE; - - rs = *rsz = newReqserv(serv); - - while( readLn(line, file) == TRUE) { - char* line1 = Utl_stripWhiteSpace(line); - if (*line1) - storeMsgId(rs, line1); - } - - rs->dirty = FALSE; - - if (Log_check(fclose(file) != EOF, - "could not close %s properly: %s\n", - filename, strerror(errno))) - return FALSE; - - return TRUE; } /* Write out request file for given Reqserv. Return TRUE on success. If an I/O error occurs, it is logged, and FALSE is returned. */ -static Bool writeRequestfile(Reqserv* rs) +static Bool +writeRequestfile( Reqserv* rs ) { - Str filename; - FILE* file; - char** z; + Str filename; + FILE* file; + char** z; - fileRequest(filename, rs->serv); - Log_dbg( LOG_DBG_REQUESTS, "writing request file %s", filename ); + fileRequest( filename, rs->serv ); + Log_dbg( LOG_DBG_REQUESTS, "writing request file %s", filename ); - if (Log_check((file = fopen(filename, "w")) != 0, - "could not open %s for writing: %s", - filename, strerror(errno))) - return FALSE; + if ( Log_check( ( file = fopen( filename, "w" ) ) != 0, + "could not open %s for writing: %s", + filename, strerror( errno ) ) ) + return FALSE; - if (rs->reql) - for (z = rs->reql; z < rs->reql+rs->reql_length; z++) - if (*z) { - if (Log_check( fputs(*z, file) != EOF - && fputs("\n", file) != EOF, - "write error: %s", strerror(errno))) - return FALSE; - } + if ( rs->reql ) + for ( z = rs->reql; z < rs->reql+rs->reql_length; z++ ) + if ( *z ) + { + if ( Log_check( fputs( *z, file ) != EOF + && fputs( "\n", file ) != EOF, + "write error: %s", strerror( errno ) ) ) + return FALSE; + } - if (Log_check(fclose(file) != EOF, - "could not close %s properly: %s\n", - filename, strerror(errno))) - return FALSE; + if ( Log_check( fclose( file ) != EOF, + "could not close %s properly: %s\n", + filename, strerror( errno ) ) ) + return FALSE; - rs->dirty = FALSE; - rs->mtime = get_mtime(rs->serv); + rs->dirty = FALSE; + rs->mtime = get_mtime( rs->serv ); - return TRUE; + return TRUE; } @@ -351,14 +369,14 @@ ASSERT( is_open ); Log_dbg( LOG_DBG_REQUESTS, "Req_remove(\"%s\", \"%s\")", serv, msgId ); - if (getReqserv(serv, &rs) == FALSE) + if ( !getReqserv(serv, &rs) ) return; - z = searchMsgId(rs, msgId); + z = searchMsgId( rs, msgId ); if ( ! z ) return; - free(*z); + free( *z ); *z = 0; rs->dirty = TRUE; } @@ -367,45 +385,47 @@ Bool Req_first( const char *serv, Str msgId ) { - Reqserv* rs; + Reqserv* rs; - ASSERT( is_open ); - ASSERT( !iterator && !iterator_end ); + ASSERT( is_open ); + ASSERT( !iterator && !iterator_end ); - if (getReqserv(serv, &rs) == FALSE) - return FALSE; + if ( !getReqserv( serv, &rs ) ) + return FALSE; - if (!rs->reql) - return FALSE; + if ( !rs->reql ) + return FALSE; - iterator = rs->reql - 1; - iterator_end = rs->reql + rs->reql_length; + iterator = rs->reql - 1; + iterator_end = rs->reql + rs->reql_length; - return Req_next(msgId); + return Req_next( msgId ); } Bool Req_next( Str msgId ) { - ASSERT( is_open ); - ASSERT(iterator && iterator_end); + ASSERT( is_open ); + ASSERT( iterator && iterator_end ); - if (iterator >= iterator_end) - return FALSE; - iterator++; + if ( iterator >= iterator_end ) + return FALSE; + iterator++; - while (iterator < iterator_end) { - if (!*iterator) - iterator++; - else { - Utl_cpyStr(msgId, *iterator); - return TRUE; + while ( iterator < iterator_end ) + { + if ( !*iterator ) + iterator++; + else + { + Utl_cpyStr( msgId, *iterator ); + return TRUE; + } } - } - iterator = iterator_end = 0; - return FALSE; + iterator = iterator_end = 0; + return FALSE; } @@ -414,32 +434,36 @@ changed recently, from cache. These files will be reread on demand. */ Bool -Req_open(void) +Req_open( void ) { - Log_dbg( LOG_DBG_REQUESTS, "opening request database" ); - ASSERT(is_open == FALSE); - cleanupReqserv(); - is_open = TRUE; - return TRUE; + Log_dbg( LOG_DBG_REQUESTS, "opening request database" ); + ASSERT( !is_open ); + + cleanupReqserv(); + is_open = TRUE; + return TRUE; } /* Do not occupy the request files any longer. Write any changes to disk. Return TRUE on success, FALSE if an IO error occurs. */ -void Req_close(void) +void +Req_close(void) { - Bool ret = TRUE; - Reqserv* rs; - Log_dbg( LOG_DBG_REQUESTS, - "closing request database, writing changes to disk" ); - ASSERT(is_open == TRUE); + Bool ret = TRUE; + Reqserv* rs; + Log_dbg( LOG_DBG_REQUESTS, + "closing request database, writing changes to disk" ); + ASSERT( is_open ); - for (rs = reqserv; rs; rs = rs->next) { - if (rs->dirty == TRUE) { - if (!writeRequestfile(rs)) - ret = FALSE; + for ( rs = reqserv; rs; rs = rs->next ) + { + if ( rs->dirty ) + { + if ( !writeRequestfile( rs ) ) + ret = FALSE; + } } - } - is_open = FALSE; + is_open = FALSE; }
--- a/src/util.c Tue Dec 24 09:08:59 2002 +0000 +++ b/src/util.c Fri Dec 27 21:48:25 2002 +0000 @@ -1,7 +1,7 @@ /* util.c - $Id: util.c 411 2002-11-17 15:18:19Z bears $ + $Id: util.c 413 2002-12-27 21:48:25Z bears $ */ #if HAVE_CONFIG_H @@ -557,13 +557,10 @@ void Utl_allocAndCpy( char **dst, const char *src ) { - int len = strlen( src ); - if ( ! ( *dst = malloc( (size_t)len + 1 ) ) ) - { - Log_err( "Cannot allocate string with length %lu", strlen( src ) ); - exit( EXIT_FAILURE ); - } - memcpy( *dst, src, (size_t)len + 1 ); + size_t len = strlen( src ); + if ( ! ( *dst = malloc( len + 1 ) ) ) + Log_fatal( "Cannot allocate string with length %lu", len ); + memcpy( *dst, src, len + 1 ); } SignalHandler