changeset 9160:d639063977cc HEAD

mbox: Don't write garbage to mbox if message doesn't have a body.
author Timo Sirainen <tss@iki.fi>
date Wed, 24 Jun 2009 16:54:41 -0400
parents 5aa15e065760
children af28db3176cf
files src/lib-storage/index/mbox/mbox-save.c
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/mbox/mbox-save.c	Wed Jun 24 14:20:11 2009 -0400
+++ b/src/lib-storage/index/mbox/mbox-save.c	Wed Jun 24 16:54:41 2009 -0400
@@ -139,6 +139,8 @@
 	const char *str;
 	size_t len;
 
+	i_assert(ctx->eoh_offset != (uoff_t)-1);
+
 	if (ctx->mbox->mbox_writeonly) {
 		/* we can't seek, don't set Content-Length */
 		return 0;
@@ -551,6 +553,19 @@
 	return 0;
 }
 
+static int mbox_save_finish_headers(struct mbox_save_context *ctx)
+{
+	i_assert(ctx->eoh_offset == (uoff_t)-1);
+
+	/* append our own headers and ending empty line */
+	ctx->extra_hdr_offset = ctx->output->offset;
+	if (o_stream_send(ctx->output, str_data(ctx->headers),
+			  str_len(ctx->headers)) < 0)
+		return write_error(ctx);
+	ctx->eoh_offset = ctx->output->offset;
+	return 0;
+}
+
 int mbox_save_continue(struct mail_save_context *_ctx)
 {
 	struct mbox_save_context *ctx = (struct mbox_save_context *)_ctx;
@@ -626,12 +641,8 @@
 				      hdr_md5_sum, NULL);
 	}
 
-	/* append our own headers and ending empty line */
-	ctx->extra_hdr_offset = ctx->output->offset;
-	if (o_stream_send(ctx->output, str_data(ctx->headers),
-			  str_len(ctx->headers)) < 0)
-		return write_error(ctx);
-	ctx->eoh_offset = ctx->output->offset;
+	if (mbox_save_finish_headers(ctx) < 0)
+		return -1;
 
 	/* write body */
 	if (mbox_save_body_input(ctx) < 0)
@@ -643,6 +654,9 @@
 {
 	struct mbox_save_context *ctx = (struct mbox_save_context *)_ctx;
 
+	if (!ctx->failed && ctx->eoh_offset == (uoff_t)-1)
+		(void)mbox_save_finish_headers(ctx);
+
 	if (ctx->output != NULL) {
 		/* make sure everything is written */
 		if (o_stream_flush(ctx->output) < 0)