test for the mbox utils. (copy_file_chunk): fixed a nasty bug.
authorbertrand <bertrand@helixcode.com>
Mon, 17 Jan 2000 08:40:10 +0000 (08:40 +0000)
committerBertrand Guiheneuf <bertrand@src.gnome.org>
Mon, 17 Jan 2000 08:40:10 +0000 (08:40 +0000)
2000-01-17  bertrand  <bertrand@helixcode.com>

* tests/test9.c (main): test for the mbox utils.
(copy_file_chunk): fixed a nasty bug.
(camel_mbox_write_xev): create the copy file descriptor
with the proper arguments. Exceptions implememnted.
(camel_mbox_write_xev): changed the way bytes are counted.
No more uses the message size cause it did not take into
account the message separators characters.
(camel_mbox_write_xev): hopefully fixed the last bugs.
works ok now.

Summary information / X-Evolution header generation should all
work ok now.

camel/providers/mbox/camel-mbox-folder.c
camel/providers/mbox/camel-mbox-parser.c
camel/providers/mbox/camel-mbox-utils.c

index c502a19..82e5bc1 100644 (file)
@@ -340,7 +340,7 @@ _exists (CamelFolder *folder, CamelException *ex)
                return FALSE;
        }
 
-       exists = S_REG (stat_buf.st_mode);
+       exists = S_ISREG (stat_buf.st_mode);
        /* we should  check the rights here  */
        
        CAMEL_LOG_FULL_DEBUG ("Leaving CamelMboxFolder::exists\n");
index 87752be..eee41b8 100644 (file)
@@ -35,7 +35,7 @@
 
 
 
-#define MBOX_PARSER_BUF_SIZE 1000
+#define MBOX_PARSER_BUF_SIZE 10000
  
 #define MBOX_PARSER_FROM_KW "from:"               
 #define MBOX_PARSER_FROM_KW_SZ 5
index 9eb4bd1..f4cbe34 100644 (file)
@@ -51,6 +51,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
+#include <string.h>
 
 
 #include <glib.h>
@@ -172,7 +173,7 @@ copy_file_chunk (gint fd_src,
        while (nb_to_read > 0) {
                
                do {
-                       nb_read = read (fd_src, buffer, MAX (1000, nb_to_read));
+                       nb_read = read (fd_src, buffer, MIN (1000, nb_to_read));
                } while (nb_read == -1 && errno == EINTR);
                
                if (nb_read == -1) {
@@ -190,9 +191,11 @@ copy_file_chunk (gint fd_src,
                } while (v == -1 && errno == EINTR);
 
                if (v == -1) {
-                       camel_exception_set (ex, 
+                       camel_exception_setv (ex, 
                                             CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
-                                            "could write to the mbox copy file");
+                                            "could not write to the mbox copy file\n"
+                                             "Full error is : %s\n",
+                                             strerror (errno));
                        return;
                }
 
@@ -214,6 +217,9 @@ camel_mbox_write_xev (gchar *mbox_file_name,
        CamelMboxParserMessageInfo *cur_msg_info;
        gint fd1, fd2;
        guint bytes_to_copy = 0;
+       glong cur_pos = 0;
+       glong cur_offset = 0;
+       glong end_of_last_message;
        glong next_free_uid;
        gchar xev_header[20] = "X-Evolution:XXXX-X\n";
        gchar *tmp_file_name;
@@ -221,40 +227,58 @@ camel_mbox_write_xev (gchar *mbox_file_name,
        gint rename_result;
        gint unlink_result;
        
-       tmp_file_name = g_strdup_printf ("__%s__.ev_tmp", mbox_file_name);
-       tmp_file_name_secure = g_strdup_printf ("__%s__.ev_tmp_secure", mbox_file_name);
+       tmp_file_name = g_strdup_printf ("%s__.ev_tmp", mbox_file_name);
+       tmp_file_name_secure = g_strdup_printf ("%s__.ev_tmp_secure", mbox_file_name);
 
        fd1 = open (mbox_file_name, O_RDONLY);
-       fd2 = open (tmp_file_name, O_RDWR);
+       fd2 = open (tmp_file_name, O_WRONLY | O_CREAT | O_TRUNC );
+       if (fd2 == -1) {
+                       camel_exception_setv (ex, 
+                                            CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
+                                            "could not create the temporary mbox copy file\n"
+                                             "\t%s\n"
+                                             "Full error is : %s\n",
+                                             tmp_file_name,
+                                             strerror (errno));
+                       return next_uid;
+               }
        
        next_free_uid = next_uid;
        for (cur_msg = 0; cur_msg < summary_information->len; cur_msg++) {
                
                cur_msg_info = (CamelMboxParserMessageInfo *)(summary_information->data) + cur_msg;
-               if (cur_msg_info->x_evolution) {
+               end_of_last_message = cur_msg_info->message_position + cur_msg_info->size;
 
-                       bytes_to_copy += cur_msg_info->size;
-               } else {
+               if ( !cur_msg_info->x_evolution) {
                        
-                       bytes_to_copy += cur_msg_info->end_of_headers_offset;
+                       bytes_to_copy = cur_msg_info->message_position 
+                               + cur_msg_info->end_of_headers_offset
+                               - cur_pos;
+
+                       cur_pos = cur_msg_info->message_position 
+                               + cur_msg_info->end_of_headers_offset;
+
                        copy_file_chunk (fd1, fd2, bytes_to_copy, ex);
                        if (camel_exception_get_id (ex)) {
                                close (fd1);
                                close (fd2);
                                goto end;
                        }
-
+                       
+                       printf ("Writing the x-ev header\n");
+                       printf ("Current message number : %d\n", cur_msg);
                        camel_mbox_xev_write_header_content (xev_header + 12, next_free_uid++, 0);
                        write (fd2, xev_header, 19);
-                       bytes_to_copy = cur_msg_info->size - cur_msg_info->end_of_headers_offset;
+                       cur_offset += 19;
                        cur_msg_info->size += 19;
                        cur_msg_info->x_evolution_offset = cur_msg_info->end_of_headers_offset;
                        cur_msg_info->x_evolution = g_strdup_printf ("%.6s", xev_header + 12);
                        cur_msg_info->end_of_headers_offset += 19;
-               }
+               } 
+               cur_msg_info->message_position += cur_offset;
        }
 
-       if (bytes_to_copy > 0)
+       bytes_to_copy = end_of_last_message - cur_pos;
                copy_file_chunk (fd1, fd2, bytes_to_copy, ex);