exipick version 20060216.1
[exim.git] / src / src / spool_mbox.c
index d1cae29a164845fc875b9633b3461daafe2ad48d..e96dca48f25ef004a3ff6ec04a67a4c95257a7ff 100644 (file)
@@ -1,4 +1,4 @@
-/* $Cambridge: exim/src/src/spool_mbox.c,v 1.6 2005/06/06 18:49:35 tom Exp $ */
+/* $Cambridge: exim/src/src/spool_mbox.c,v 1.10 2005/08/01 14:41:25 ph10 Exp $ */
 
 /*************************************************
 *     Exim - an Internet mail transport agent    *
@@ -48,15 +48,15 @@ FILE *spool_mbox(unsigned long *mbox_file_size) {
     };
 
     /* create temp directory inside scan dir */
-    snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, message_id);
+    (void)string_format(mbox_path, 1024, "%s/scan/%s", spool_directory, message_id);
     if (!directory_make(NULL, mbox_path, 0750, FALSE)) {
       debug_printf("unable to create directory: %s/scan/%s\n", spool_directory, message_id);
       return NULL;
     };
 
     /* open [message_id].eml file for writing */
-    snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
-    mbox_file = Ufopen(mbox_path,"w");
+    (void)string_format(mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
+    mbox_file = Ufopen(mbox_path,"wb");
 
     if (mbox_file == NULL) {
       debug_printf("unable to open file for writing: %s\n", mbox_path);
@@ -70,7 +70,7 @@ FILE *spool_mbox(unsigned long *mbox_file_size) {
         i = fwrite(mbox_delimiter, 1, Ustrlen(mbox_delimiter), mbox_file);
         if (i != Ustrlen(mbox_delimiter)) {
           debug_printf("error/short write on writing in: %s", mbox_path);
-          fclose(mbox_file);
+          (void)fclose(mbox_file);
           return NULL;
         };
       };
@@ -84,7 +84,7 @@ FILE *spool_mbox(unsigned long *mbox_file_size) {
         i = fwrite(my_envelope_from, 1, Ustrlen(my_envelope_from), mbox_file);
         if (i != Ustrlen(my_envelope_from)) {
           debug_printf("error/short write on writing in: %s", mbox_path);
-          fclose(mbox_file);
+          (void)fclose(mbox_file);
           return NULL;
         };
       };
@@ -98,7 +98,7 @@ FILE *spool_mbox(unsigned long *mbox_file_size) {
         i = fwrite(my_envelope_to, 1, Ustrlen(my_envelope_to), mbox_file);
         if (i != Ustrlen(my_envelope_to)) {
           debug_printf("error/short write on writing in: %s", mbox_path);
-          fclose(mbox_file);
+          (void)fclose(mbox_file);
           return NULL;
         };
       };
@@ -117,7 +117,7 @@ FILE *spool_mbox(unsigned long *mbox_file_size) {
       i = fwrite(my_headerlist->text, 1, my_headerlist->slen, mbox_file);
       if (i != my_headerlist->slen) {
         debug_printf("error/short write on writing in: %s", mbox_path);
-        fclose(mbox_file);
+        (void)fclose(mbox_file);
         return NULL;
       };
 
@@ -129,40 +129,52 @@ FILE *spool_mbox(unsigned long *mbox_file_size) {
     for (i = 0; i < 2; i++) {
       message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
       sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
-      data_file = Ufopen(mbox_path,"r");
+      data_file = Ufopen(mbox_path,"rb");
       if (data_file != NULL)
         break;
     };
 
-    fread(data_buffer, 1, 18, data_file);
+    /* The code used to use this line, but it doesn't work in Cygwin.
+     *
+     *  (void)fread(data_buffer, 1, 18, data_file);
+     *
+     * What's happening is that spool_mbox used to use an fread to jump over the
+     * file header. That fails under Cygwin because the header is locked, but
+     * doing an fseek succeeds. We have to output the leading newline
+     * explicitly, because the one in the file is parted of the locked area.
+     */
+
+    (void)fwrite("\n", 1, 1, mbox_file);
+    (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
 
     do {
       j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
+
       if (j > 0) {
         i = fwrite(data_buffer, 1, j, mbox_file);
         if (i != j) {
           debug_printf("error/short write on writing in: %s", mbox_path);
-          fclose(mbox_file);
-          fclose(data_file);
+          (void)fclose(mbox_file);
+          (void)fclose(data_file);
           return NULL;
         };
       };
     } while (j > 0);
 
-    fclose(data_file);
-    fclose(mbox_file);
+    (void)fclose(data_file);
+    (void)fclose(mbox_file);
     Ustrcpy(spooled_message_id, message_id);
     spool_mbox_ok = 1;
   };
 
-  snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
+  (void)string_format(mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
 
   /* get the size of the mbox message */
   stat(CS mbox_path, &statbuf);
   *mbox_file_size = statbuf.st_size;
 
   /* open [message_id].eml file for reading */
-  mbox_file = Ufopen(mbox_path,"r");
+  mbox_file = Ufopen(mbox_path,"rb");
 
   return mbox_file;
 }
@@ -192,7 +204,7 @@ void unspool_mbox(void) {
       struct dirent *entry;
       DIR *tempdir;
 
-      snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
+      (void)string_format(mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
 
   tempdir = opendir(CS mbox_path);
   /* loop thru dir & delete entries */
@@ -200,7 +212,7 @@ void unspool_mbox(void) {
   do {
     entry = readdir(tempdir);
     if (entry == NULL) break;
-    snprintf(CS file_path, 1024,"%s/scan/%s/%s", spool_directory, spooled_message_id, entry->d_name);
+    (void)string_format(file_path, 1024,"%s/scan/%s/%s", spool_directory, spooled_message_id, entry->d_name);
     if ( (Ustrcmp(entry->d_name,"..") != 0) && (Ustrcmp(entry->d_name,".") != 0) ) {
       debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
               n = unlink(CS file_path);