changeset 463:95697d7c97a1 noffle

[svn] * src/outgoing.c,src/util.h,src/util.c: Create Utl_createDir() and Utl_writeFile() and rework outgoing.c to use them.
author bears
date Wed, 23 Jul 2003 10:31:01 +0100
parents de7f674d1224
children d9035d08fe51
files src/outgoing.c src/util.c src/util.h
diffstat 3 files changed, 72 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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(). */
--- 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