changeset 292:7a4fac415698 HEAD

Handle properly messages which have only partial header (eg. new From-line comes before \n\n).
author Timo Sirainen <tss@iki.fi>
date Mon, 23 Sep 2002 12:13:58 +0300
parents 57a111fd821b
children 21d53f6b38fc
files src/lib-index/mbox/mbox-append.c src/lib-index/mbox/mbox-fsck.c src/lib-index/mbox/mbox-index.c src/lib-index/mbox/mbox-index.h
diffstat 4 files changed, 61 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mbox/mbox-append.c	Mon Sep 23 11:42:01 2002 +0300
+++ b/src/lib-index/mbox/mbox-append.c	Mon Sep 23 12:13:58 2002 +0300
@@ -23,56 +23,6 @@
 	return rec;
 }
 
-static void mbox_read_message(IOBuffer *inbuf)
-{
-	unsigned char *msg;
-	size_t i, size, startpos;
-	int lastmsg;
-
-	/* read until "[\r]\nFrom " is found */
-	startpos = i = 0; lastmsg = TRUE;
-	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
-		for (i = startpos; i < size; i++) {
-			if (msg[i] == ' ' && i >= 5) {
-				/* See if it's space after "From" */
-				if (msg[i-5] == '\n' && msg[i-4] == 'F' &&
-				    msg[i-3] == 'r' && msg[i-2] == 'o' &&
-				    msg[i-1] == 'm') {
-					/* yes, see if we had \r too */
-					i -= 5;
-					if (i > 0 && msg[i-1] == '\r')
-						i--;
-					break;
-				}
-			}
-		}
-
-		if (i < size) {
-			startpos = i;
-                        lastmsg = FALSE;
-			break;
-		}
-
-		startpos = i < 7 ? i : 7;
-		i -= startpos;
-
-		io_buffer_skip(inbuf, i);
-	}
-
-	if (lastmsg && startpos > 0) {
-		/* end of file, remove the last [\r]\n */
-		msg = io_buffer_get_data(inbuf, &size);
-		if (size == startpos) {
-			if (msg[startpos-1] == '\n')
-				startpos--;
-			if (startpos > 0 && msg[startpos-1] == '\r')
-				startpos--;
-		}
-	}
-
-	io_buffer_skip(inbuf, startpos);
-}
-
 static int mbox_index_append_next(MailIndex *index, IOBuffer *inbuf)
 {
 	MailIndexRecord *rec;
@@ -115,7 +65,7 @@
 	abs_start_offset = inbuf->start_offset + inbuf->offset;
 
 	/* now, find the ending "[\r]\nFrom " */
-	mbox_read_message(inbuf);
+	mbox_skip_message(inbuf);
 	stop_offset = inbuf->offset;
 
 	/* add message to index */
--- a/src/lib-index/mbox/mbox-fsck.c	Mon Sep 23 11:42:01 2002 +0300
+++ b/src/lib-index/mbox/mbox-fsck.c	Mon Sep 23 12:13:58 2002 +0300
@@ -127,6 +127,14 @@
 
 	header_offset = inbuf->offset;
 
+	if (rec->body_size == 0) {
+		/* possibly broken message, find the From-line to make sure
+		   header parser won't pass it. */
+		mbox_skip_message(inbuf);
+		io_buffer_set_read_limit(inbuf, inbuf->offset);
+		io_buffer_seek(inbuf, header_offset);
+	}
+
 	/* get the MD5 sum of fixed headers and the current message flags
 	   in Status and X-Status fields */
         mbox_header_init_context(&ctx, index);
@@ -134,6 +142,7 @@
 	md5_final(&ctx.md5, current_digest);
 
 	mbox_header_free_context(&ctx);
+	io_buffer_set_read_limit(inbuf, 0);
 
 	body_offset = inbuf->offset;
 	do {
--- a/src/lib-index/mbox/mbox-index.c	Mon Sep 23 11:42:01 2002 +0300
+++ b/src/lib-index/mbox/mbox-index.c	Mon Sep 23 12:13:58 2002 +0300
@@ -314,6 +314,56 @@
 	}
 }
 
+void mbox_skip_message(IOBuffer *inbuf)
+{
+	unsigned char *msg;
+	size_t i, size, startpos;
+	int lastmsg;
+
+	/* read until "[\r]\nFrom " is found */
+	startpos = i = 0; lastmsg = TRUE;
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
+		for (i = startpos; i < size; i++) {
+			if (msg[i] == ' ' && i >= 5) {
+				/* See if it's space after "From" */
+				if (msg[i-5] == '\n' && msg[i-4] == 'F' &&
+				    msg[i-3] == 'r' && msg[i-2] == 'o' &&
+				    msg[i-1] == 'm') {
+					/* yes, see if we had \r too */
+					i -= 5;
+					if (i > 0 && msg[i-1] == '\r')
+						i--;
+					break;
+				}
+			}
+		}
+
+		if (i < size) {
+			startpos = i;
+                        lastmsg = FALSE;
+			break;
+		}
+
+		startpos = i < 7 ? i : 7;
+		i -= startpos;
+
+		io_buffer_skip(inbuf, i);
+	}
+
+	if (lastmsg && startpos > 0) {
+		/* end of file, remove the last [\r]\n */
+		msg = io_buffer_get_data(inbuf, &size);
+		if (size == startpos) {
+			if (msg[startpos-1] == '\n')
+				startpos--;
+			if (startpos > 0 && msg[startpos-1] == '\r')
+				startpos--;
+		}
+	}
+
+	io_buffer_skip(inbuf, startpos);
+}
+
 int mbox_mail_get_start_offset(MailIndex *index, MailIndexRecord *rec,
 			       uoff_t *offset)
 {
--- a/src/lib-index/mbox/mbox-index.h	Mon Sep 23 11:42:01 2002 +0300
+++ b/src/lib-index/mbox/mbox-index.h	Mon Sep 23 12:13:58 2002 +0300
@@ -26,6 +26,7 @@
 			 void *context);
 int mbox_skip_crlf(IOBuffer *inbuf);
 void mbox_skip_empty_lines(IOBuffer *inbuf);
+void mbox_skip_message(IOBuffer *inbuf);
 int mbox_mail_get_start_offset(MailIndex *index, MailIndexRecord *rec,
 			       uoff_t *offset);