[dovecot-cvs] dovecot/src/lib-index/mbox istream-mbox.c,1.7,1.8 mbox-append.c,1.45,1.46 mbox-index.c,1.84,1.85 mbox-index.h,1.32,1.33 mbox-open.c,1.22,1.23 mbox-rewrite.c,1.68,1.69 mbox-sync-full.c,1.21,1.22

cras at procontrol.fi cras at procontrol.fi
Sun Nov 9 20:26:27 EET 2003


Update of /home/cvs/dovecot/src/lib-index/mbox
In directory danu:/tmp/cvs-serv3937/lib-index/mbox

Modified Files:
	istream-mbox.c mbox-append.c mbox-index.c mbox-index.h 
	mbox-open.c mbox-rewrite.c mbox-sync-full.c 
Log Message:
istream rewrite. instead of directly setting any limits to stream, you now
have to use i_stream_create_limit() to existing stream. this should make the
istreams much easier to create and understand how they work.



Index: istream-mbox.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/istream-mbox.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- istream-mbox.c	29 Oct 2003 15:31:45 -0000	1.7
+++ istream-mbox.c	9 Nov 2003 18:26:25 -0000	1.8
@@ -12,8 +12,7 @@
 	struct istream *input;
 
 	buffer_t *headers;
-	uoff_t body_offset, body_size;
-	struct message_size header_size;
+	uoff_t v_header_size, body_offset, body_size;
 };
 
 static void _close(struct _iostream *stream __attr_unused__)
@@ -49,42 +48,27 @@
 	struct mbox_istream *mstream = (struct mbox_istream *) stream;
 	ssize_t ret;
 	size_t pos;
-	uoff_t limit, old_limit;
-	off_t vsize_diff;
+	uoff_t offset;
 
-	if (stream->istream.v_offset < mstream->header_size.virtual_size) {
+	if (stream->istream.v_offset < mstream->v_header_size) {
 		/* we don't support mixing headers and body.
 		   it shouldn't be needed. */
 		return -2;
 	}
 
-	/* may be positive or negative, depending on how much there was CRs
-	   and how much headers were hidden */
-	vsize_diff = mstream->header_size.virtual_size -
-		mstream->header_size.physical_size;
-
-	limit = stream->istream.v_limit - vsize_diff;
-	old_limit = mstream->input->v_limit;
-	if (limit != old_limit)
-		i_stream_set_read_limit(mstream->input, limit);
-
-	if (mstream->input->v_offset != stream->istream.v_offset - vsize_diff) {
-		i_stream_seek(mstream->input,
-			      stream->istream.v_offset - vsize_diff);
-	}
+	offset = stream->istream.v_offset - mstream->v_header_size;
+	if (mstream->input->v_offset != offset)
+		i_stream_seek(mstream->input, offset);
 
 	ret = i_stream_read(mstream->input);
 
-	mstream->istream.pos -= mstream->istream.skip;
-	mstream->istream.skip = 0;
-	mstream->istream.buffer = i_stream_get_data(mstream->input, &pos);
+	stream->pos -= stream->skip;
+	stream->skip = 0;
+	stream->buffer = i_stream_get_data(mstream->input, &pos);
 
-	ret = pos <= mstream->istream.pos ? -1 :
-		(ssize_t) (pos - mstream->istream.pos);
+	ret = pos <= stream->pos ? -1 :
+		(ssize_t) (pos - stream->pos);
 	mstream->istream.pos = pos;
-
-	if (limit != old_limit)
-		i_stream_set_read_limit(mstream->input, old_limit);
 	return ret;
 }
 
@@ -93,54 +77,51 @@
 	struct mbox_istream *mstream = (struct mbox_istream *) stream;
 
 	stream->istream.v_offset = v_offset;
-	if (v_offset < mstream->header_size.virtual_size) {
+	if (v_offset < mstream->v_header_size) {
 		/* still in headers */
 		stream->skip = v_offset;
-		stream->pos = stream->high_pos =
-			mstream->header_size.virtual_size;
+		stream->pos = mstream->v_header_size;
 		stream->buffer = buffer_get_data(mstream->headers, NULL);
 	} else {
 		/* body - use our real input stream */
-		stream->skip = stream->pos = stream->high_pos = 0;
+		stream->skip = stream->pos = 0;
 		stream->buffer = NULL;
-
-		v_offset += (off_t)mstream->header_size.physical_size -
-			(off_t)mstream->header_size.virtual_size;
-		i_stream_seek(mstream->input, v_offset);
 	}
 }
 
-static void _skip(struct _istream *stream, uoff_t count)
-{
-	i_stream_seek(&stream->istream, stream->istream.v_offset + count);
-}
-
 struct istream *i_stream_create_mbox(pool_t pool, struct istream *input,
-				     uoff_t body_size)
+				     uoff_t offset, uoff_t body_size)
 {
 	struct mbox_istream *mstream;
+	struct istream *hdr_input;
 
 	mstream = p_new(pool, struct mbox_istream, 1);
-	mstream->input = input;
 	mstream->body_size = body_size;
 
 	if (body_size == 0) {
 		/* possibly broken message, find the next From-line
 		   and make sure header parser won't pass it. */
 		mbox_skip_header(input);
-		i_stream_set_read_limit(input, input->v_offset);
-		i_stream_seek(input, 0);
+		hdr_input = i_stream_create_limit(pool, input,
+						  0, input->v_offset);
+	} else {
+		hdr_input = input;
+		i_stream_ref(input);
 	}
 
 	mstream->headers = buffer_create_dynamic(default_pool,
 						 8192, (size_t)-1);
-	mbox_hide_headers(input, mstream->headers,
-			  &mstream->header_size);
-	mstream->body_offset = input->v_offset;
-	i_stream_set_read_limit(input, mstream->body_offset + body_size);
+	i_stream_seek(hdr_input, offset);
+	mbox_read_headers(hdr_input, mstream->headers);
+	mstream->v_header_size = buffer_get_used_size(mstream->headers);
+	mstream->body_offset = hdr_input->v_offset;
+	i_stream_unref(hdr_input);
+
+	mstream->input = i_stream_create_limit(pool, input,
+					       mstream->body_offset, body_size);
 
 	mstream->istream.buffer = buffer_get_data(mstream->headers, NULL);
-	mstream->istream.pos = mstream->header_size.virtual_size;
+	mstream->istream.pos = mstream->v_header_size;
 
 	mstream->istream.iostream.close = _close;
 	mstream->istream.iostream.destroy = _destroy;
@@ -148,9 +129,8 @@
 	mstream->istream.iostream.set_blocking = _set_blocking;
 
 	mstream->istream.read = _read;
-	mstream->istream.skip_count = _skip;
 	mstream->istream.seek = _seek;
 
-	return _i_stream_create(&mstream->istream, pool, -1, 0,
-				mstream->header_size.virtual_size + body_size);
+	return _i_stream_create(&mstream->istream, pool, -1,
+				mstream->v_header_size + body_size);
 }

Index: mbox-append.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-append.c,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -d -r1.45 -r1.46
--- mbox-append.c	5 Nov 2003 08:42:12 -0000	1.45
+++ mbox-append.c	9 Nov 2003 18:26:25 -0000	1.46
@@ -14,10 +14,11 @@
 				  struct istream *input)
 {
 	struct mail_index_record *rec;
-        struct mbox_header_context ctx;
+	struct mbox_header_context ctx;
+	struct istream *hdr_stream;
 	enum mail_index_record_flag index_flags;
 	time_t received_date;
-	uoff_t abs_start_offset, eoh_offset;
+	uoff_t hdr_offset, body_offset, end_offset;
 	const unsigned char *data;
 	unsigned char md5_digest[16];
 	size_t size, pos;
@@ -38,8 +39,7 @@
 	if (size == 0)
 		return -1;
 
-	if (pos == size || size <= 5 ||
-	    strncmp((const char *) data, "From ", 5) != 0) {
+	if (pos == size || size <= 5 || memcmp(data, "From ", 5) != 0) {
 		/* a) no \n found, or line too long
 		   b) not a From-line */
 		index_set_error(index, "Error indexing mbox file %s: "
@@ -55,12 +55,12 @@
 		received_date = ioloop_time;
 
 	i_stream_skip(input, pos+1);
-	abs_start_offset = input->start_offset + input->v_offset;
+	hdr_offset = input->v_offset;
 
 	/* now, find the end of header. also stops at "\nFrom " if it's
 	   found (broken messages) */
 	mbox_skip_header(input);
-	eoh_offset = input->v_offset;
+	body_offset = input->v_offset;
 
 	index_flags = 0;
 
@@ -72,17 +72,31 @@
 	   reading the headers. it uses Content-Length if available or finds
 	   the next From-line. */
 	mbox_header_init_context(&ctx, index, input);
-        ctx.set_read_limit = TRUE;
 
-	i_stream_seek(input, abs_start_offset - input->start_offset);
-	i_stream_set_read_limit(input, eoh_offset);
+	hdr_stream = i_stream_create_limit(default_pool, input,
+					   hdr_offset,
+					   body_offset - hdr_offset);
+	i_stream_seek(hdr_stream, 0);
+	message_parse_header(NULL, hdr_stream, NULL, mbox_header_cb, &ctx);
+	i_stream_unref(hdr_stream);
 
-	message_parse_header(NULL, input, NULL, mbox_header_cb, &ctx);
+	dirty = FALSE;
 
-	i_stream_seek(input, input->v_limit);
-	i_stream_set_read_limit(input, 0);
+	/* try Content-Length */
+	end_offset = body_offset + ctx.content_length;
+	if (ctx.content_length == (uoff_t)-1 ||
+	    !mbox_verify_end_of_body(input, end_offset)) {
+		/* failed, search for From-line */
+		if (ctx.content_length != (uoff_t)-1) {
+			/* broken, rewrite it */
+			dirty = TRUE;
+		}
+
+		i_stream_seek(input, body_offset);
+		mbox_skip_message(input);
+		ctx.content_length = input->v_offset - body_offset;
+	}
 
-	dirty = ctx.content_length_broken;
 	if (index->header->messages_count == 0 &&
 	    ctx.uid_validity != index->header->uid_validity) {
 		/* UID validity is different */
@@ -139,7 +153,7 @@
 
 	/* location offset = beginning of headers in message */
 	if (!mail_cache_add(trans_ctx, rec, MAIL_CACHE_LOCATION_OFFSET,
-			    &abs_start_offset, sizeof(abs_start_offset)))
+			    &hdr_offset, sizeof(hdr_offset)))
 		return -1;
 
 	if (!mail_cache_add(trans_ctx, rec, MAIL_CACHE_RECEIVED_DATE,
@@ -167,11 +181,6 @@
 	uoff_t offset;
 	int ret;
 
-	if (input->eof) {
-		/* no new data */
-		return TRUE;
-	}
-
 	if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
 		return FALSE;
 
@@ -180,7 +189,7 @@
 
 	do {
 		offset = input->v_offset;
-		if (input->start_offset + input->v_offset != 0) {
+		if (input->v_offset != 0) {
 			/* we're at the [\r]\n before the From-line,
 			   skip it */
 			if (!mbox_skip_crlf(input)) {
@@ -195,14 +204,14 @@
 			}
 		}
 
-		t_push();
-		ret = mbox_index_append_next(index, trans_ctx, input);
-		t_pop();
-
 		if (input->eof) {
 			ret = 1;
 			break;
 		}
+
+		t_push();
+		ret = mbox_index_append_next(index, trans_ctx, input);
+		t_pop();
 
 		if (ret == 0) {
 			/* we want to rescan this message with exclusive

Index: mbox-index.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-index.c,v
retrieving revision 1.84
retrieving revision 1.85
diff -u -d -r1.84 -r1.85
--- mbox-index.c	5 Nov 2003 08:42:12 -0000	1.84
+++ mbox-index.c	9 Nov 2003 18:26:25 -0000	1.85
@@ -55,11 +55,9 @@
 	return TRUE;
 }
 
-struct istream *mbox_get_stream(struct mail_index *index, uoff_t offset,
+struct istream *mbox_get_stream(struct mail_index *index,
 				enum mail_lock_type lock_type)
 {
-	i_assert(offset < OFF_T_MAX);
-
 	switch (lock_type) {
 	case MAIL_LOCK_SHARED:
 	case MAIL_LOCK_EXCLUSIVE:
@@ -93,10 +91,7 @@
 		}
 	}
 
-	i_stream_set_read_limit(index->mbox_stream, 0);
-	i_stream_set_start_offset(index->mbox_stream, (uoff_t)offset);
 	i_stream_seek(index->mbox_stream, 0);
-
 	i_stream_ref(index->mbox_stream);
 	return index->mbox_stream;
 }
@@ -245,37 +240,10 @@
 		    struct message_header_line *hdr, void *context)
 {
 	struct mbox_header_context *ctx = context;
-	uoff_t start_offset, end_offset;
 	size_t i;
 	int fixed = FALSE;
 
-	if (hdr == NULL) {
-		/* End of headers */
-		if (!ctx->set_read_limit)
-			return;
-
-		/* a) use Content-Length, b) search for "From "-line */
-		start_offset = ctx->input->v_offset;
-		i_stream_set_read_limit(ctx->input, 0);
-
-		end_offset = start_offset + ctx->content_length;
-		if (ctx->content_length == (uoff_t)-1 ||
-		    !mbox_verify_end_of_body(ctx->input, end_offset)) {
-			if (ctx->content_length != (uoff_t)-1) {
-				i_stream_seek(ctx->input, start_offset);
-				ctx->content_length_broken = TRUE;
-			}
-			mbox_skip_message(ctx->input);
-			end_offset = ctx->input->v_offset;
-			ctx->content_length = end_offset - start_offset;
-		}
-
-		i_stream_seek(ctx->input, start_offset);
-		i_stream_set_read_limit(ctx->input, end_offset);
-		return;
-	}
-
-	if (hdr->eoh)
+	if (hdr == NULL || hdr->eoh)
 		return;
 
 	/* Pretty much copy&pasted from popa3d by Solar Designer */
@@ -293,8 +261,7 @@
 
 	case 'C':
 	case 'c':
-		if (ctx->set_read_limit &&
-		    strcasecmp(hdr->name, "Content-Length") == 0) {
+		if (strcasecmp(hdr->name, "Content-Length") == 0) {
 			/* manual parsing, so we can deal with uoff_t */
 			ctx->content_length = 0;
 			for (i = 0; i < hdr->value_len; i++) {
@@ -655,12 +622,6 @@
 	if (i_stream_read_data(input, &data, &size, 6) < 0)
 		return FALSE;
 
-	if (input->eof) {
-		/* end of file. a bit unexpected though,
-		   since \n is missing. */
-		return TRUE;
-	}
-
 	/* either there should be the next From-line,
 	   or [\r]\n at end of file */
 	if (size > 0 && data[0] == '\r') {
@@ -725,20 +686,15 @@
 	return TRUE;
 }
 
-void mbox_hide_headers(struct istream *input, buffer_t *dest,
-		       struct message_size *hdr_size)
+void mbox_read_headers(struct istream *input, buffer_t *dest)
 {
 	struct message_header_parser_ctx *hdr_ctx;
 	struct message_header_line *hdr;
-	uoff_t virtual_size = 0;
 
-	hdr_ctx = message_parse_header_init(input, hdr_size);
+	hdr_ctx = message_parse_header_init(input, NULL);
 	while ((hdr = message_parse_header_next(hdr_ctx)) != NULL) {
 		if (hdr->eoh) {
-			if (dest != NULL)
-				buffer_append(dest, "\r\n", 2);
-			else
-				virtual_size += 2;
+			buffer_append(dest, "\r\n", 2);
 			break;
 		}
 
@@ -750,26 +706,16 @@
 		    strcasecmp(hdr->name, "Content-Length") == 0 ||
 		    strcasecmp(hdr->name, "Status") == 0) {
 			/* ignore */
-		} else if (dest != NULL) {
+		} else {
 			if (!hdr->continued) {
 				buffer_append(dest, hdr->name, hdr->name_len);
 				buffer_append(dest, ": ", 2);
 			}
 			buffer_append(dest, hdr->value, hdr->value_len);
 			buffer_append(dest, "\r\n", 2);
-		} else {
-			if (!hdr->continued)
-				virtual_size += hdr->name_len + 2;
-			virtual_size += hdr->value_len + 2;
 		}
 	}
 	message_parse_header_deinit(hdr_ctx);
-
-	if (dest != NULL)
-		virtual_size = buffer_get_used_size(dest);
-
-	hdr_size->virtual_size = virtual_size;
-	hdr_size->lines = 0;
 }
 
 struct mail_index *

Index: mbox-index.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-index.h,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- mbox-index.h	2 Sep 2003 22:33:33 -0000	1.32
+++ mbox-index.h	9 Nov 2003 18:26:25 -0000	1.33
@@ -18,7 +18,6 @@
 
 	struct istream *input;
 	uoff_t content_length;
-	int set_read_limit, content_length_broken;
 };
 
 int mbox_set_syscall_error(struct mail_index *index, const char *function);
@@ -27,7 +26,7 @@
    which is useful when you want to be sure you're not accessing a deleted
    mbox file. */
 int mbox_file_open(struct mail_index *index);
-struct istream *mbox_get_stream(struct mail_index *index, uoff_t offset,
+struct istream *mbox_get_stream(struct mail_index *index,
 				enum mail_lock_type lock_type);
 void mbox_file_close_stream(struct mail_index *index);
 void mbox_file_close_fd(struct mail_index *index);
@@ -50,8 +49,7 @@
 int mbox_mail_get_location(struct mail_index *index,
 			   struct mail_index_record *rec,
 			   uoff_t *offset, uoff_t *body_size);
-void mbox_hide_headers(struct istream *input, buffer_t *dest,
-		       struct message_size *hdr_size);
+void mbox_read_headers(struct istream *input, buffer_t *dest);
 
 struct mail_index *
 mbox_index_alloc(const char *mbox_path, const char *index_dir,
@@ -71,6 +69,6 @@
 int mbox_index_rewrite(struct mail_index *index);
 
 struct istream *i_stream_create_mbox(pool_t pool, struct istream *input,
-				     uoff_t body_size);
+				     uoff_t offset, uoff_t body_size);
 
 #endif

Index: mbox-open.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-open.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- mbox-open.c	2 Sep 2003 22:33:33 -0000	1.22
+++ mbox-open.c	9 Nov 2003 18:26:25 -0000	1.23
@@ -27,7 +27,7 @@
 	if (!mbox_mail_get_location(index, rec, &offset, &body_size))
 		return NULL;
 
-	input = mbox_get_stream(index, offset, MAIL_LOCK_SHARED);
+	input = mbox_get_stream(index, MAIL_LOCK_SHARED);
 	if (input == NULL)
 		return NULL;
 
@@ -35,6 +35,5 @@
 		*received_date = index->get_received_date(index, rec);
 
 	i_assert(index->mbox_sync_counter == index->mbox_lock_counter);
-
-	return i_stream_create_mbox(default_pool, input, body_size);
+	return i_stream_create_mbox(default_pool, input, offset, body_size);
 }

Index: mbox-rewrite.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-rewrite.c,v
retrieving revision 1.68
retrieving revision 1.69
diff -u -d -r1.68 -r1.69
--- mbox-rewrite.c	5 Nov 2003 08:42:13 -0000	1.68
+++ mbox-rewrite.c	9 Nov 2003 18:26:25 -0000	1.69
@@ -69,13 +69,11 @@
 static int mbox_write(struct mail_index *index, struct istream *input,
 		      struct ostream *output, uoff_t end_offset)
 {
-	uoff_t old_limit;
 	int failed;
 
 	i_assert(input->v_offset <= end_offset);
 
-	old_limit = input->v_limit;
-	i_stream_set_read_limit(input, end_offset);
+	input = i_stream_create_limit(default_pool, input, 0, end_offset);
 	if (o_stream_send_istream(output, input) < 0) {
 		index_set_error(index, "Error rewriting mbox file %s: %s",
 				index->mailbox_path,
@@ -90,7 +88,7 @@
 		failed = FALSE;
 	}
 
-	i_stream_set_read_limit(input, old_limit);
+	i_stream_unref(input);
 	return !failed;
 }
 
@@ -406,6 +404,7 @@
 	struct message_header_parser_ctx *hdr_ctx;
 	struct message_header_line *hdr;
 	struct message_size hdr_size;
+	struct istream *hdr_input;
 	uoff_t offset;
 	int force_filler;
 
@@ -422,16 +421,19 @@
 	ctx.uid_last = index->header->next_uid-1;
 	ctx.custom_flags = mail_custom_flags_list_get(index->custom_flags);
 
-	if (body_size == 0) {
+	if (body_size != 0) {
+		hdr_input = input;
+		i_stream_ref(hdr_input);
+	} else {
 		/* possibly broken message, find the next From-line
 		   and make sure header parser won't pass it. */
 		offset = input->v_offset;
 		mbox_skip_header(input);
-		i_stream_set_read_limit(input, input->v_offset);
-		i_stream_seek(input, offset);
-	}
+		hdr_input = i_stream_create_limit(default_pool, input, offset,
+						  input->v_offset);
+	} 
 
-	hdr_ctx = message_parse_header_init(input, &hdr_size);
+	hdr_ctx = message_parse_header_init(hdr_input, &hdr_size);
 	while ((hdr = message_parse_header_next(hdr_ctx)) != NULL) {
 		t_push();
 		write_header(&ctx, hdr);
@@ -440,7 +442,7 @@
 	message_parse_header_deinit(hdr_ctx);
 	*hdr_input_size = hdr_size.physical_size;
 
-	i_stream_set_read_limit(input, 0);
+	i_stream_unref(hdr_input);
 
 	/* append the flag fields */
 	if (seq == 1 && !ctx.ximapbase_found) {
@@ -476,7 +478,7 @@
 static int fd_copy(struct mail_index *index, int in_fd, int out_fd,
 		   uoff_t out_offset, uoff_t size)
 {
-	struct istream *input;
+	struct istream *input, *input2;
 	struct ostream *output;
 	struct stat st;
 	int ret;
@@ -506,21 +508,22 @@
 
 	t_push();
 
-	input = i_stream_create_mmap(in_fd, pool_datastack_create(),
-				     1024*256, 0, 0, FALSE);
-	i_stream_set_read_limit(input, size);
+	input = i_stream_create_file(in_fd, pool_datastack_create(),
+				     1024*256, FALSE);
+	input2 = i_stream_create_limit(pool_datastack_create(), input, 0, size);
 
 	output = o_stream_create_file(out_fd, pool_datastack_create(),
 				      1024, FALSE);
 	o_stream_set_blocking(output, 60000, NULL, NULL);
 
-	ret = o_stream_send_istream(output, input);
+	ret = o_stream_send_istream(output, input2);
 	if (ret < 0) {
 		errno = output->stream_errno;
 		mbox_set_syscall_error(index, "o_stream_send_istream()");
 	}
 
 	o_stream_unref(output);
+	i_stream_unref(input2);
 	i_stream_unref(input);
 	t_pop();
 
@@ -625,7 +628,7 @@
 				break;
 		}
 
-		input = mbox_get_stream(index, 0, MAIL_LOCK_EXCLUSIVE);
+		input = mbox_get_stream(index, MAIL_LOCK_EXCLUSIVE);
 		if (input == NULL)
 			break;
 
@@ -714,6 +717,7 @@
 				break;
 			}
 			offset += hdr_size;
+			i_assert(input->v_offset == offset);
 
 			if (dirty_found &&
 			    offset - dirty_offset == output->offset) {

Index: mbox-sync-full.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-sync-full.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- mbox-sync-full.c	5 Nov 2003 08:42:13 -0000	1.21
+++ mbox-sync-full.c	9 Nov 2003 18:26:25 -0000	1.22
@@ -77,49 +77,58 @@
 			     unsigned int *seq, struct istream *input,
 			     struct mail_index_record **next_rec, int *dirty)
 {
-	struct message_size hdr_parsed_size;
 	struct mbox_header_context ctx;
 	struct mail_index_record *first_rec, *last_rec;
+	struct istream *hdr_input;
         enum mail_index_record_flag index_flags;
-	uoff_t header_offset, body_offset, offset;
-	uoff_t hdr_size, body_size;
+	uoff_t header_offset, body_offset, offset, body_size, eoh_offset;
 	unsigned char current_digest[16];
 	unsigned int first_seq, last_seq;
-	int ret, hdr_size_fixed;
+	int ret, hdr_parsed;
 
 	*next_rec = NULL;
 
 	/* skip the From-line */
 	skip_line(input);
-	if (input->eof)
-		return -1;
 	header_offset = input->v_offset;
 
 	first_rec = last_rec = NULL;
 	first_seq = last_seq = 0;
-	ret = 0; hdr_size = 0; body_offset = 0; hdr_size_fixed = FALSE;
+	ret = 0; body_offset = 0; eoh_offset = (uoff_t)-1; hdr_parsed = FALSE;
 	do {
 		if (!mbox_mail_get_location(index, rec, &offset, &body_size))
 			return -1;
 
-		i_stream_seek(input, header_offset);
-
-		if (body_size == 0 && !hdr_size_fixed) {
+		if (body_size == 0 && eoh_offset == (uoff_t)-1) {
 			/* possibly broken message, find the next From-line
 			   and make sure header parser won't pass it. */
-			mbox_skip_header(input);
-			i_stream_set_read_limit(input, input->v_offset);
 			i_stream_seek(input, header_offset);
-			hdr_size_fixed = TRUE;
-			hdr_size = 0;
+			mbox_skip_header(input);
+			eoh_offset = input->v_offset;
+			hdr_parsed = FALSE;
 		}
 
-		if (hdr_size == 0) {
+		if (!hdr_parsed) {
 			/* get the MD5 sum of fixed headers and the current
 			   message flags in Status and X-Status fields */
-			mbox_header_init_context(&ctx, index, input);
-			message_parse_header(NULL, input, &hdr_parsed_size,
+			if (eoh_offset == (uoff_t)-1)
+				hdr_input = input;
+			else {
+				hdr_input = i_stream_create_limit(default_pool,
+						input, 0, eoh_offset);
+			}
+			i_stream_seek(hdr_input, header_offset);
+
+			mbox_header_init_context(&ctx, index, hdr_input);
+			message_parse_header(NULL, hdr_input, NULL,
 					     mbox_header_cb, &ctx);
+
+			hdr_parsed = TRUE;
+			body_offset = hdr_input->v_offset;
+
+			if (eoh_offset != (uoff_t)-1)
+				i_stream_unref(hdr_input);
+			hdr_input = NULL;
 			md5_final(&ctx.md5, current_digest);
 
 			if (*seq == 1) {
@@ -135,10 +144,6 @@
 						ctx.uid_last+1;
 				}
 			}
-
-			i_stream_set_read_limit(input, 0);
-
-			body_offset = input->v_offset;
 		}
 
 		if (verify_header(index, rec, ctx.uid, current_digest) &&
@@ -277,7 +282,7 @@
 		index->header->flags &= ~MAIL_INDEX_HDR_FLAG_DIRTY_MESSAGES;
 	}
 
-	if (input->eof || (index->set_flags & MAIL_INDEX_HDR_FLAG_REBUILD))
+	if ((index->set_flags & MAIL_INDEX_HDR_FLAG_REBUILD))
 		return TRUE;
 	else
 		return mbox_index_append_stream(index, input);
@@ -292,7 +297,7 @@
 
 	i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
 
-	input = mbox_get_stream(index, 0, MAIL_LOCK_SHARED);
+	input = mbox_get_stream(index, MAIL_LOCK_SHARED);
 	if (input == NULL)
 		return FALSE;
 
@@ -316,7 +321,7 @@
 		if (!mbox_unlock(index))
 			return FALSE;
 
-		input = mbox_get_stream(index, 0, MAIL_LOCK_EXCLUSIVE);
+		input = mbox_get_stream(index, MAIL_LOCK_EXCLUSIVE);
 		if (input == NULL)
 			return FALSE;
 



More information about the dovecot-cvs mailing list