changeset 6083:1b0ef7a74448 HEAD

Moved tee stream handling to index-mail code.
author Timo Sirainen <tss@iki.fi>
date Wed, 18 Jul 2007 09:36:22 +0300
parents d62bddb414ef
children 00495c124ea0
files src/lib-storage/index/cydir/cydir-save.c src/lib-storage/index/index-mail-headers.c src/lib-storage/index/index-mail.h src/lib-storage/index/maildir/maildir-save.c
diffstat 4 files changed, 30 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/cydir/cydir-save.c	Wed Jul 18 09:36:04 2007 +0300
+++ b/src/lib-storage/index/cydir/cydir-save.c	Wed Jul 18 09:36:22 2007 +0300
@@ -3,7 +3,6 @@
 #include "lib.h"
 #include "hostpid.h"
 #include "istream.h"
-#include "istream-tee.h"
 #include "ostream.h"
 #include "ostream-crlf.h"
 #include "str.h"
@@ -27,7 +26,7 @@
 
 	/* updated for each appended mail: */
 	uint32_t seq;
-	struct istream *input, *input2;
+	struct istream *input;
 	struct ostream *output;
 	struct mail *mail, *cur_dest_mail;
 	int fd;
@@ -70,7 +69,6 @@
 	struct cydir_save_context *ctx = t->save_ctx;
 	enum mail_flags save_flags;
 	struct ostream *output;
-	struct tee_istream *tee;
 	const char *path;
 
 	i_assert((t->ictx.flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
@@ -129,12 +127,8 @@
 	if (mail_set_seq(dest_mail, ctx->seq) < 0)
 		i_unreached();
 
-	tee = tee_i_stream_create(input, default_pool);
-	ctx->input = tee_i_stream_create_child(tee, default_pool);
-	ctx->input2 = tee_i_stream_create_child(tee, default_pool);
-
 	ctx->cur_dest_mail = dest_mail;
-	index_mail_cache_parse_init(dest_mail, ctx->input2);
+	ctx->input = index_mail_cache_parse_init(dest_mail, input);
 
 	*ctx_r = &ctx->ctx;
 	return ctx->failed ? -1 : 0;
@@ -149,8 +143,6 @@
 		return -1;
 
 	do {
-		index_mail_cache_parse_continue(ctx->cur_dest_mail);
-
 		if (o_stream_send_istream(ctx->output, ctx->input) < 0) {
 			if (!mail_storage_set_error_from_errno(storage)) {
 				mail_storage_set_critical(storage,
@@ -160,10 +152,12 @@
 			ctx->failed = TRUE;
 			return -1;
 		}
-		/* both input and input2 readers may consume data from our
-		   primary input stream. we'll have to handle all the data
-		   here. */
-	} while (i_stream_read(ctx->input2) > 0);
+		index_mail_cache_parse_continue(ctx->cur_dest_mail);
+
+		/* both tee input readers may consume data from our primary
+		   input stream. we'll have to make sure we don't return with
+		   one of the streams still having data in them. */
+	} while (i_stream_read(ctx->input) > 0);
 	return 0;
 }
 
@@ -202,7 +196,6 @@
 
 	index_mail_cache_parse_deinit(ctx->cur_dest_mail);
 	i_stream_unref(&ctx->input);
-	i_stream_unref(&ctx->input2);
 
 	return ctx->failed ? -1 : 0;
 }
--- a/src/lib-storage/index/index-mail-headers.c	Wed Jul 18 09:36:04 2007 +0300
+++ b/src/lib-storage/index/index-mail-headers.c	Wed Jul 18 09:36:22 2007 +0300
@@ -7,6 +7,7 @@
 #include "str.h"
 #include "message-date.h"
 #include "message-parser.h"
+#include "istream-tee.h"
 #include "istream-header-filter.h"
 #include "imap-envelope.h"
 #include "imap-bodystructure.h"
@@ -339,16 +340,25 @@
 	index_mail_parse_header(mail->data.parts, hdr, mail);
 }
 
-void index_mail_cache_parse_init(struct mail *_mail, struct istream *input)
+struct istream *index_mail_cache_parse_init(struct mail *_mail,
+					    struct istream *input)
 {
 	struct index_mail *mail = (struct index_mail *)_mail;
+	struct tee_istream *tee;
+	struct istream *input2;
 
 	i_assert(mail->data.parser_ctx == NULL);
 
+	tee = tee_i_stream_create(input, default_pool);
+	input = tee_i_stream_create_child(tee, default_pool);
+	input2 = tee_i_stream_create_child(tee, default_pool);
+
 	index_mail_parse_header_init(mail, NULL);
 	mail->data.parser_ctx =
 		message_parser_init(mail->data_pool, input,
 				    hdr_parser_flags, msg_parser_flags);
+	i_stream_unref(&input);
+	return input2;
 }
 
 static void index_mail_init_parser(struct index_mail *mail)
--- a/src/lib-storage/index/index-mail.h	Wed Jul 18 09:36:04 2007 +0300
+++ b/src/lib-storage/index/index-mail.h	Wed Jul 18 09:36:22 2007 +0300
@@ -182,7 +182,8 @@
 void index_mail_cache_add_idx(struct index_mail *mail, unsigned int field_idx,
 			      const void *data, size_t data_size);
 
-void index_mail_cache_parse_init(struct mail *mail, struct istream *input);
+struct istream *index_mail_cache_parse_init(struct mail *mail,
+					    struct istream *input);
 void index_mail_cache_parse_continue(struct mail *mail);
 void index_mail_cache_parse_deinit(struct mail *mail);
 
--- a/src/lib-storage/index/maildir/maildir-save.c	Wed Jul 18 09:36:04 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-save.c	Wed Jul 18 09:36:22 2007 +0300
@@ -5,7 +5,6 @@
 #include "array.h"
 #include "buffer.h"
 #include "istream.h"
-#include "istream-tee.h"
 #include "ostream.h"
 #include "ostream-crlf.h"
 #include "str.h"
@@ -51,7 +50,7 @@
 	buffer_t *keywords_buffer;
 	ARRAY_TYPE(keyword_indexes) keywords_array;
 
-	struct istream *input, *input2;
+	struct istream *input;
 	struct ostream *output;
 	int fd;
 	time_t received_date;
@@ -144,7 +143,6 @@
 {
 	struct maildir_save_context *ctx = t->save_ctx;
 	struct maildir_filename *mf;
-	struct tee_istream *tee;
 
 	/* now, we want to be able to rollback the whole append session,
 	   so we'll just store the name of this temp file and move it later
@@ -204,11 +202,7 @@
 		   cached data directly */
 		ctx->cur_dest_mail = NULL;
 	} else {
-		tee = tee_i_stream_create(ctx->input, default_pool);
-		ctx->input = tee_i_stream_create_child(tee, default_pool);
-		ctx->input2 = tee_i_stream_create_child(tee, default_pool);
-
-		index_mail_cache_parse_init(dest_mail, ctx->input2);
+		ctx->input = index_mail_cache_parse_init(dest_mail, ctx->input);
 		ctx->cur_dest_mail = dest_mail;
 	}
 	return ctx->seq;
@@ -410,8 +404,6 @@
 		return -1;
 
 	do {
-		if (ctx->cur_dest_mail != NULL)
-			index_mail_cache_parse_continue(ctx->cur_dest_mail);
 		if (o_stream_send_istream(ctx->output, ctx->input) < 0) {
 			if (!mail_storage_set_error_from_errno(storage)) {
 				mail_storage_set_critical(storage,
@@ -422,10 +414,13 @@
 			ctx->failed = TRUE;
 			return -1;
 		}
-		/* both input and input2 readers may consume data from our
-		   primary input stream. we'll have to handle all the data
-		   here. */
-	} while (i_stream_read(ctx->input2) > 0);
+		if (ctx->cur_dest_mail != NULL)
+			index_mail_cache_parse_continue(ctx->cur_dest_mail);
+
+		/* both tee input readers may consume data from our primary
+		   input stream. we'll have to make sure we don't return with
+		   one of the streams still having data in them. */
+	} while (i_stream_read(ctx->input) > 0);
 	return 0;
 }
 
@@ -439,7 +434,6 @@
 	if (ctx->cur_dest_mail != NULL) {
 		index_mail_cache_parse_deinit(ctx->cur_dest_mail);
 		i_stream_unref(&ctx->input);
-		i_stream_unref(&ctx->input2);
 	}
 
 	ctx->finished = TRUE;