dovecot: Fixed saving messages

dovecot at dovecot.org dovecot at dovecot.org
Sat Oct 20 17:06:38 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/f9a4bd4ea2bb
changeset: 6556:f9a4bd4ea2bb
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Oct 20 17:06:33 2007 +0300
description:
Fixed saving messages

diffstat:

1 file changed, 49 insertions(+), 31 deletions(-)
src/lib-storage/index/mbox/mbox-save.c |   80 +++++++++++++++++++-------------

diffs (104 lines):

diff -r b3392a7da84f -r f9a4bd4ea2bb src/lib-storage/index/mbox/mbox-save.c
--- a/src/lib-storage/index/mbox/mbox-save.c	Tue Oct 16 20:12:07 2007 +0300
+++ b/src/lib-storage/index/mbox/mbox-save.c	Sat Oct 20 17:06:33 2007 +0300
@@ -489,6 +489,51 @@ int mbox_save_init(struct mailbox_transa
 	return ctx->failed ? -1 : 0;
 }
 
+static int mbox_save_body_input(struct mbox_save_context *ctx)
+{
+	const unsigned char *data;
+	size_t size;
+
+	data = i_stream_get_data(ctx->input, &size);
+	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);
+	return 0;
+}
+
+static int mbox_save_body(struct mbox_save_context *ctx)
+{
+	ssize_t ret;
+
+	while ((ret = i_stream_read(ctx->input)) != -1) {
+		if (ctx->mail != NULL) {
+			/* i_stream_read() may have returned 0 at EOF
+			   because of this parser */
+			index_mail_cache_parse_continue(ctx->mail);
+		}
+		if (ret == 0)
+			return 0;
+
+		if (mbox_save_body_input(ctx) < 0)
+			return -1;
+	}
+
+	if (ctx->last_char != '\n') {
+		/* if mail doesn't end with LF, we'll do that.
+		   otherwise some mbox parsers don't like the result.
+		   this makes it impossible to save a mail that doesn't
+		   end with LF though. */
+		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;
+}
+
 int mbox_save_continue(struct mail_save_context *_ctx)
 {
 	struct mbox_save_context *ctx = (struct mbox_save_context *)_ctx;
@@ -501,35 +546,7 @@ int mbox_save_continue(struct mail_save_
 
 	if (ctx->eoh_offset != (uoff_t)-1) {
 		/* writing body */
-		while ((ret = i_stream_read(ctx->input)) != -1) {
-			if (ctx->mail != NULL) {
-				/* i_stream_read() may have returned 0 at EOF
-				   because of this parser */
-				index_mail_cache_parse_continue(ctx->mail);
-			}
-			if (ret == 0)
-				return 0;
-
-			data = i_stream_get_data(ctx->input, &size);
-			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);
-		}
-
-		if (ctx->last_char != '\n') {
-			/* if mail doesn't end with LF, we'll do that.
-			   otherwise some mbox parsers don't like the result.
-			   this makes it impossible to save a mail that doesn't
-			   end with LF though. */
-			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;
+		return mbox_save_body(ctx);
 	}
 
 	while ((ret = i_stream_read(ctx->input)) != -1) {
@@ -602,8 +619,9 @@ int mbox_save_continue(struct mail_save_
 	ctx->eoh_offset = ctx->output->offset;
 
 	/* write body */
-	(void)i_stream_get_data(ctx->input, &size);
-	return ctx->input->eof && size == 0 ? 0 : mbox_save_continue(_ctx);
+	if (mbox_save_body_input(ctx) < 0)
+		return -1;
+	return ctx->input->eof ? 0 : mbox_save_body(ctx);
 }
 
 int mbox_save_finish(struct mail_save_context *_ctx)


More information about the dovecot-cvs mailing list