# HG changeset patch # User bears # Date 1058952661 -3600 # Node ID 95697d7c97a175f701a32434731922874d9d0766 # Parent de7f674d1224858bfa01db08e584b951ef224665 [svn] * src/outgoing.c,src/util.h,src/util.c: Create Utl_createDir() and Utl_writeFile() and rework outgoing.c to use them. diff -r de7f674d1224 -r 95697d7c97a1 src/outgoing.c --- a/src/outgoing.c Wed Jul 23 10:26:49 2003 +0100 +++ b/src/outgoing.c Wed Jul 23 10:31:01 2003 +0100 @@ -1,7 +1,7 @@ /* outgoing.c - $Id: outgoing.c 529 2003-05-22 08:26:25Z bears $ + $Id: outgoing.c 608 2003-07-23 09:31:01Z bears $ */ #if HAVE_CONFIG_H @@ -46,37 +46,15 @@ Cfg_spoolDir(), serv, msgId ); } -static void -createDir( const char *serv ) -{ - Str dir; - int r; - - snprintf( dir, MAXCHAR, "%s/outgoing/%s", Cfg_spoolDir(), serv ); - r = mkdir( dir, 0755 ); - if ( r != 0 ) - Log_dbg( LOG_DBG_NEWSBASE, "mkdir: %s", strerror( errno ) ); -} - Bool Out_add( const char *serv, const char *msgId, const DynStr *artTxt ) { - Str file; - FILE *f; + Str outDir; - fileOutgoing( file, serv, msgId ); - if ( ! ( f = fopen( file, "w" ) ) ) - { - createDir( serv ); - if ( ! ( f = fopen( file, "w" ) ) ) - { - Log_err( "Cannot open %s", file ); - return FALSE; - } - } - fprintf( f, "%s", DynStr_str( artTxt ) ); - fclose( f ); - return TRUE; + Utl_cpyStr( outDir, Cfg_spoolDir() ); + Utl_catStr( outDir, "/outgoing" ); + + return Utl_writeFile( outDir, serv, msgId, DynStr_str( artTxt ) ); } Bool diff -r de7f674d1224 -r 95697d7c97a1 src/util.c --- a/src/util.c Wed Jul 23 10:26:49 2003 +0100 +++ b/src/util.c Wed Jul 23 10:31:01 2003 +0100 @@ -1,7 +1,7 @@ /* util.c - $Id: util.c 427 2003-01-31 15:35:14Z bears $ + $Id: util.c 608 2003-07-23 09:31:01Z bears $ */ #if HAVE_CONFIG_H @@ -603,6 +603,61 @@ return getHostFQDN( result ); } +Bool +Utl_createDir( const char *dir ) +{ + struct stat st; + int err; + + err = stat( dir, &st ); + if ( err == 0 && S_ISDIR( st.st_mode ) ) + return TRUE; + + err = mkdir( dir, 0755 ); + if ( err != 0 ) + { + Log_dbg( LOG_DBG_NEWSBASE, "mkdir: %s", strerror( errno ) ); + return FALSE; + } + + return TRUE; +} + + +Bool +Utl_writeFile( const char *dirPrefix, const char *dir, + const char *file, const char *text) +{ + Str dirPath; + Str filePath; + FILE *f; + + if ( dirPrefix != NULL ) + { + if ( ! Utl_createDir( dirPrefix ) ) + return FALSE; + snprintf( dirPath, MAXCHAR, "%s/%s", dirPrefix, dir ); + } + else + Utl_cpyStr( dirPath, dir ); + + if ( ! Utl_createDir( dirPath ) ) + return FALSE; + + snprintf( filePath, MAXCHAR, "%s/%s", dirPath, file); + + if ( ! ( f = fopen( filePath, "w" ) ) ) + { + Log_err( "Cannot open %s", filePath ); + return FALSE; + } + fprintf( f, "%s", text ); + fclose( f ); + return TRUE; + +} + + #if defined(UTIL_TEST) /* Test code borrowed from wildmat.c. Yep, still uses gets(). */ diff -r de7f674d1224 -r 95697d7c97a1 src/util.h --- a/src/util.h Wed Jul 23 10:26:49 2003 +0100 +++ b/src/util.h Wed Jul 23 10:31:01 2003 +0100 @@ -3,7 +3,7 @@ Miscellaneous helper functions. - $Id: util.h 403 2002-11-10 11:32:17Z bears $ + $Id: util.h 608 2003-07-23 09:31:01Z bears $ */ #ifndef UTL_H @@ -116,4 +116,13 @@ Bool Utl_getFQDN( Str result ); +/* Ensure a directory exists and create it if not. */ +Bool +Utl_createDir( const char *dir ); + +/* Write text to file in given dir, creating dir if necessary. */ +Bool +Utl_writeFile( const char *dirPrefix, const char *dir, + const char *file, const char *text); + #endif