dovecot: Use crlf input streams instead of output streams so mes...

dovecot at dovecot.org dovecot at dovecot.org
Sat Oct 6 01:50:58 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/aedda93baa2c
changeset: 6528:aedda93baa2c
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Oct 06 01:48:16 2007 +0300
description:
Use crlf input streams instead of output streams so message parser doesn't
end up saving broken offsets.

diffstat:

5 files changed, 43 insertions(+), 49 deletions(-)
src/lib-storage/index/cydir/cydir-save.c     |   13 ++++++-----
src/lib-storage/index/dbox/dbox-save.c       |   13 +++++------
src/lib-storage/index/index-mail.c           |   13 ++---------
src/lib-storage/index/maildir/maildir-save.c |   24 ++++++++++-----------
src/lib-storage/index/mbox/mbox-save.c       |   29 +++++++++++++-------------

diffs (288 lines):

diff -r ba65c858b7dd -r aedda93baa2c src/lib-storage/index/cydir/cydir-save.c
--- a/src/lib-storage/index/cydir/cydir-save.c	Sat Oct 06 01:46:22 2007 +0300
+++ b/src/lib-storage/index/cydir/cydir-save.c	Sat Oct 06 01:48:16 2007 +0300
@@ -3,8 +3,8 @@
 #include "lib.h"
 #include "hostpid.h"
 #include "istream.h"
+#include "istream-crlf.h"
 #include "ostream.h"
-#include "ostream-crlf.h"
 #include "str.h"
 #include "index-mail.h"
 #include "cydir-storage.h"
@@ -69,7 +69,7 @@ int cydir_save_init(struct mailbox_trans
 	struct cydir_mailbox *mbox = (struct cydir_mailbox *)t->ictx.ibox;
 	struct cydir_save_context *ctx = t->save_ctx;
 	enum mail_flags save_flags;
-	struct ostream *output;
+	struct istream *crlf_input;
 	const char *path;
 
 	i_assert((t->ictx.flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
@@ -86,9 +86,7 @@ int cydir_save_init(struct mailbox_trans
 	path = cydir_get_save_path(ctx, ctx->mail_count);
 	ctx->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0660);
 	if (ctx->fd != -1) {
-		output = o_stream_create_fd_file(ctx->fd, 0, FALSE);
-		ctx->output = o_stream_create_crlf(output);
-		o_stream_unref(&output);
+		ctx->output = o_stream_create_fd_file(ctx->fd, 0, FALSE);
 		o_stream_cork(ctx->output);
 	} else {
 		mail_storage_set_critical(_t->box->storage,
@@ -116,8 +114,11 @@ int cydir_save_init(struct mailbox_trans
 	}
 	mail_set_seq(dest_mail, ctx->seq);
 
+	crlf_input = i_stream_create_crlf(input);
+	ctx->input = index_mail_cache_parse_init(dest_mail, crlf_input);
+	i_stream_unref(&crlf_input);
+
 	ctx->cur_dest_mail = dest_mail;
-	ctx->input = index_mail_cache_parse_init(dest_mail, input);
 	ctx->cur_received_date = received_date;
 
 	*ctx_r = &ctx->ctx;
diff -r ba65c858b7dd -r aedda93baa2c src/lib-storage/index/dbox/dbox-save.c
--- a/src/lib-storage/index/dbox/dbox-save.c	Sat Oct 06 01:46:22 2007 +0300
+++ b/src/lib-storage/index/dbox/dbox-save.c	Sat Oct 06 01:48:16 2007 +0300
@@ -5,8 +5,8 @@
 #include "hex-dec.h"
 #include "str.h"
 #include "istream.h"
+#include "istream-crlf.h"
 #include "ostream.h"
-#include "ostream-crlf.h"
 #include "write-full.h"
 #include "index-mail.h"
 #include "dbox-storage.h"
@@ -71,9 +71,9 @@ int dbox_save_init(struct mailbox_transa
 		(struct dbox_transaction_context *)_t;
 	struct dbox_mailbox *mbox = (struct dbox_mailbox *)t->ictx.ibox;
 	struct dbox_save_context *ctx = t->save_ctx;
-	struct ostream *output;
 	struct dbox_message_header dbox_msg_hdr;
 	struct dbox_save_mail *save_mail;
+	struct istream *crlf_input;
 	enum mail_flags save_flags;
 	const struct stat *st;
 	uoff_t mail_size;
@@ -117,7 +117,10 @@ int dbox_save_init(struct mailbox_transa
 	mail_set_seq(dest_mail, ctx->seq);
 
 	ctx->cur_dest_mail = dest_mail;
-	ctx->input = index_mail_cache_parse_init(dest_mail, input);
+
+	crlf_input = i_stream_create_lf(input);
+	ctx->input = index_mail_cache_parse_init(dest_mail, crlf_input);
+	i_stream_unref(&crlf_input);
 
 	save_mail = array_append_space(&ctx->mails);
 	save_mail->file = ctx->cur_file;
@@ -129,10 +132,6 @@ int dbox_save_init(struct mailbox_transa
 	memset(&dbox_msg_hdr, 0, sizeof(dbox_msg_hdr));
 	o_stream_cork(ctx->cur_output);
 	o_stream_send(ctx->cur_output, &dbox_msg_hdr, sizeof(dbox_msg_hdr));
-
-	output = o_stream_create_lf(ctx->cur_output);
-	o_stream_unref(&ctx->cur_output);
-	ctx->cur_output = output;
 
 	ctx->cur_received_date = received_date != (time_t)-1 ?
 		received_date : ioloop_time;
diff -r ba65c858b7dd -r aedda93baa2c src/lib-storage/index/index-mail.c
--- a/src/lib-storage/index/index-mail.c	Sat Oct 06 01:46:22 2007 +0300
+++ b/src/lib-storage/index/index-mail.c	Sat Oct 06 01:48:16 2007 +0300
@@ -584,16 +584,9 @@ static void index_mail_body_parsed_cache
 }
 
 static void index_mail_parse_body_finish(struct index_mail *mail,
-					 enum index_cache_field field,
-					 bool appended_mail)
+					 enum index_cache_field field)
 {
 	mail->data.parts = message_parser_deinit(&mail->data.parser_ctx);
-
-	if (appended_mail) {
-		bool use_crlf = (mail->ibox->box.storage->flags &
-				 MAIL_STORAGE_FLAG_SAVE_CRLF) != 0;
-		message_parser_set_crlfs(mail->data.parts, use_crlf);
-	}
 
 	(void)get_cached_msgpart_sizes(mail);
 
@@ -640,7 +633,7 @@ static int index_mail_parse_body(struct 
 			null_message_part_header_callback, NULL);
 	}
 	ret = index_mail_stream_check_failure(mail);
-	index_mail_parse_body_finish(mail, field, FALSE);
+	index_mail_parse_body_finish(mail, field);
 
 	i_stream_seek(data->stream, old_offset);
 	return ret;
@@ -1181,7 +1174,7 @@ void index_mail_cache_parse_deinit(struc
 
 	mail->data.save_bodystructure_body = FALSE;
 	mail->data.parsed_bodystructure = TRUE;
-	index_mail_parse_body_finish(mail, 0, TRUE);
+	index_mail_parse_body_finish(mail, 0);
 	index_mail_cache_dates(mail);
 }
 
diff -r ba65c858b7dd -r aedda93baa2c src/lib-storage/index/maildir/maildir-save.c
--- a/src/lib-storage/index/maildir/maildir-save.c	Sat Oct 06 01:46:22 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-save.c	Sat Oct 06 01:48:16 2007 +0300
@@ -5,8 +5,8 @@
 #include "array.h"
 #include "buffer.h"
 #include "istream.h"
+#include "istream-crlf.h"
 #include "ostream.h"
-#include "ostream-crlf.h"
 #include "str.h"
 #include "index-mail.h"
 #include "maildir-storage.h"
@@ -143,6 +143,7 @@ uint32_t maildir_save_add(struct maildir
 {
 	struct maildir_save_context *ctx = t->save_ctx;
 	struct maildir_filename *mf;
+	struct istream *input;
 
 	/* 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
@@ -201,7 +202,9 @@ uint32_t maildir_save_add(struct maildir
 		   cached data directly */
 		ctx->cur_dest_mail = NULL;
 	} else {
-		ctx->input = index_mail_cache_parse_init(dest_mail, ctx->input);
+		input = index_mail_cache_parse_init(dest_mail, ctx->input);
+		i_stream_unref(&ctx->input);
+		ctx->input = input;
 		ctx->cur_dest_mail = dest_mail;
 	}
 	return ctx->seq;
@@ -354,7 +357,6 @@ int maildir_save_init(struct mailbox_tra
 		(struct maildir_transaction_context *)_t;
 	struct maildir_save_context *ctx;
 	struct maildir_mailbox *mbox = (struct maildir_mailbox *)t->ictx.ibox;
-	struct ostream *output;
 	const char *fname;
 
 	i_assert((t->ictx.flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0);
@@ -374,13 +376,11 @@ int maildir_save_init(struct mailbox_tra
 	}
 
 	ctx->received_date = received_date;
-	ctx->input = input;
-
-	output = o_stream_create_fd_file(ctx->fd, 0, FALSE);
-	ctx->output = (ctx->mbox->storage->storage.flags &
-		       MAIL_STORAGE_FLAG_SAVE_CRLF) != 0 ?
-		o_stream_create_crlf(output) : o_stream_create_lf(output);
-	o_stream_unref(&output);
+	ctx->input = (ctx->mbox->storage->storage.flags &
+		      MAIL_STORAGE_FLAG_SAVE_CRLF) != 0 ?
+		i_stream_create_crlf(input) : i_stream_create_lf(input);
+
+	ctx->output = o_stream_create_fd_file(ctx->fd, 0, FALSE);
 	o_stream_cork(ctx->output);
 
 	flags &= ~MAIL_RECENT;
@@ -479,8 +479,8 @@ int maildir_save_finish(struct mail_save
 	if (ctx->cur_dest_mail != NULL) {
 		index_mail_cache_parse_deinit(ctx->cur_dest_mail,
 					      ctx->received_date);
-		i_stream_unref(&ctx->input);
-	}
+	}
+	i_stream_unref(&ctx->input);
 
 	/* remember the size in case we want to add it to filename */
 	ctx->file_last->size = ctx->output->offset;
diff -r ba65c858b7dd -r aedda93baa2c src/lib-storage/index/mbox/mbox-save.c
--- a/src/lib-storage/index/mbox/mbox-save.c	Sat Oct 06 01:46:22 2007 +0300
+++ b/src/lib-storage/index/mbox/mbox-save.c	Sat Oct 06 01:48:16 2007 +0300
@@ -11,7 +11,7 @@
 #include "str.h"
 #include "write-full.h"
 #include "istream-header-filter.h"
-#include "ostream-crlf.h"
+#include "istream-crlf.h"
 #include "message-parser.h"
 #include "index-mail.h"
 #include "mbox-storage.h"
@@ -46,7 +46,7 @@ struct mbox_save_context {
 	uint32_t seq, next_uid, uid_validity, first_saved_uid;
 
 	struct istream *input;
-	struct ostream *output, *body_output;
+	struct ostream *output;
 	uoff_t extra_hdr_offset, eoh_offset;
 	char last_char;
 
@@ -465,17 +465,18 @@ int mbox_save_init(struct mailbox_transa
 	if (write_from_line(ctx, received_date, from_envelope) < 0)
 		ctx->failed = TRUE;
 	else {
-		ctx->input = i_stream_create_header_filter(input,
+		input = i_stream_create_header_filter(input,
 						HEADER_FILTER_EXCLUDE |
 						HEADER_FILTER_NO_CR,
 						mbox_save_drop_headers,
 						mbox_save_drop_headers_count,
 						save_header_callback, ctx);
-		ctx->body_output =
-			(mbox->storage->storage.flags &
-			 MAIL_STORAGE_FLAG_SAVE_CRLF) != 0 ?
-			o_stream_create_crlf(ctx->output) :
-			o_stream_create_lf(ctx->output);
+		ctx->input = (mbox->storage->storage.flags &
+			      MAIL_STORAGE_FLAG_SAVE_CRLF) != 0 ?
+			i_stream_create_crlf(input) :
+			i_stream_create_lf(input);
+		i_stream_unref(&input);
+
 		if (ctx->mail != NULL) {
 			input = index_mail_cache_parse_init(ctx->mail,
 							    ctx->input);
@@ -510,7 +511,7 @@ int mbox_save_continue(struct mail_save_
 				return 0;
 
 			data = i_stream_get_data(ctx->input, &size);
-			if (o_stream_send(ctx->body_output, data, size) < 0)
+			if (o_stream_send(ctx->output, data, size) < 0)
 				return write_error(ctx);
 			ctx->last_char = data[size-1];
 			i_stream_skip(ctx->input, size);
@@ -521,7 +522,11 @@ int mbox_save_continue(struct mail_save_
 			   otherwise some mbox parsers don't like the result.
 			   this makes it impossible to save a mail that doesn't
 			   end with LF though. */
-			if (o_stream_send(ctx->body_output, "\n", 1) < 0)
+			const char *linefeed =
+				(ctx->mbox->storage->storage.flags &
+				 MAIL_STORAGE_FLAG_SAVE_CRLF) != 0 ?
+				"\r\n" : "\n";
+			if (o_stream_send_str(ctx->output, linefeed) < 0)
 				return write_error(ctx);
 		}
 		return 0;
@@ -618,8 +623,6 @@ int mbox_save_finish(struct mail_save_co
 		index_mail_cache_parse_deinit(ctx->mail, ctx->received_date);
 	if (ctx->input != NULL)
 		i_stream_destroy(&ctx->input);
-	if (ctx->body_output != NULL)
-		o_stream_destroy(&ctx->body_output);
 
 	if (ctx->failed && ctx->mail_offset != (uoff_t)-1) {
 		/* saving this mail failed - truncate back to beginning of it */
@@ -641,8 +644,6 @@ void mbox_save_cancel(struct mail_save_c
 
 static void mbox_transaction_save_deinit(struct mbox_save_context *ctx)
 {
-	i_assert(ctx->body_output == NULL);
-
 	if (ctx->output != NULL)
 		o_stream_destroy(&ctx->output);
 	if (ctx->mail != NULL)


More information about the dovecot-cvs mailing list