changeset 21540:6a69f65921ea

lib-storage: Remove unnecessary mail_save_context.dest_mail==NULL checks It can never be NULL after the previous change: "lib-storage: Always create mail_save_context.dest_mail". The code removal in maildir_transaction_save_commit_pre() seemed potentially dangerous, but I don't think such code path is possible anymore. Also even if it is, it's probably fine since the mail_free() is called even earlier than before (although that itself might have been a problem). This also removes last traces of code that made it possible to save mails to mbox without assigning UID to the mail. The previous commit already caused this, so this is just removing dead code.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 09 Feb 2017 17:14:57 +0200
parents eafb085567bb
children 4026d71bfe18
files src/lib-storage/index/cydir/cydir-save.c src/lib-storage/index/dbox-common/dbox-save.c src/lib-storage/index/dbox-common/dbox-save.h src/lib-storage/index/dbox-multi/mdbox-save.c src/lib-storage/index/dbox-single/sdbox-copy.c src/lib-storage/index/dbox-single/sdbox-save.c src/lib-storage/index/imapc/imapc-save.c src/lib-storage/index/index-mail.c src/lib-storage/index/index-storage.c src/lib-storage/index/maildir/maildir-save.c src/lib-storage/index/mbox/mbox-save.c src/lib-storage/index/mbox/mbox-sync-private.h src/lib-storage/index/mbox/mbox-sync.c
diffstat 13 files changed, 21 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/cydir/cydir-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/cydir/cydir-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -28,7 +28,6 @@
 	/* updated for each appended mail: */
 	uint32_t seq;
 	struct istream *input;
-	struct mail *mail;
 	int fd;
 
 	unsigned int failed:1;
@@ -115,11 +114,6 @@
 					 _ctx->data.min_modseq);
 	}
 
-	if (_ctx->dest_mail == NULL) {
-		if (ctx->mail == NULL)
-			ctx->mail = mail_alloc(trans, 0, NULL);
-		_ctx->dest_mail = ctx->mail;
-	}
 	mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
 
 	crlf_input = i_stream_create_crlf(input);
@@ -291,9 +285,6 @@
 			return -1;
 		}
 	}
-
-	if (ctx->mail != NULL)
-		mail_free(&ctx->mail);
 	return 0;
 }
 
@@ -321,8 +312,6 @@
 	if (ctx->sync_ctx != NULL)
 		(void)cydir_sync_finish(&ctx->sync_ctx, FALSE);
 
-	if (ctx->mail != NULL)
-		mail_free(&ctx->mail);
 	i_free(ctx->tmp_basename);
 	i_free(ctx);
 }
--- a/src/lib-storage/index/dbox-common/dbox-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/dbox-common/dbox-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -44,11 +44,6 @@
 
 	dbox_save_add_to_index(ctx);
 
-	if (_ctx->dest_mail == NULL) {
-		if (ctx->mail == NULL)
-			ctx->mail = mail_alloc(_ctx->transaction, 0, NULL);
-		_ctx->dest_mail = ctx->mail;
-	}
 	mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
 
 	crlf_input = i_stream_create_lf(input);
--- a/src/lib-storage/index/dbox-common/dbox-save.h	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/dbox-common/dbox-save.h	Thu Feb 09 17:14:57 2017 +0200
@@ -10,7 +10,6 @@
 	/* updated for each appended mail: */
 	uint32_t seq;
 	struct istream *input;
-	struct mail *mail;
 
 	struct ostream *dbox_output;
 
--- a/src/lib-storage/index/dbox-multi/mdbox-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/dbox-multi/mdbox-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -361,9 +361,6 @@
 		mail_index_sync_set_reason(ctx->sync_ctx->index_sync_ctx, "saving");
 	}
 
-	if (ctx->ctx.mail != NULL)
-		mail_free(&ctx->ctx.mail);
-
 	_t->changes->uid_validity = hdr->uid_validity;
 	return 0;
 }
@@ -424,8 +421,6 @@
 	if (ctx->sync_ctx != NULL)
 		(void)mdbox_sync_finish(&ctx->sync_ctx, FALSE);
 
-	if (ctx->ctx.mail != NULL)
-		mail_free(&ctx->ctx.mail);
 	array_free(&ctx->mails);
 	i_free(ctx);
 }
@@ -485,8 +480,7 @@
 	save_mail = array_append_space(&ctx->mails);
 	save_mail->seq = ctx->ctx.seq;
 
-	if (_ctx->dest_mail != NULL)
-		mail_set_seq_saving(_ctx->dest_mail, ctx->ctx.seq);
+	mail_set_seq_saving(_ctx->dest_mail, ctx->ctx.seq);
 	index_save_context_free(_ctx);
 	return 0;
 }
--- a/src/lib-storage/index/dbox-single/sdbox-copy.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/dbox-single/sdbox-copy.c	Thu Feb 09 17:14:57 2017 +0200
@@ -155,8 +155,7 @@
 	index_copy_cache_fields(_ctx, mail, ctx->seq);
 
 	sdbox_save_add_file(_ctx, dest_file);
-	if (_ctx->dest_mail != NULL)
-		mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
+	mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
 	dbox_file_unref(&src_file);
 	return 1;
 }
--- a/src/lib-storage/index/dbox-single/sdbox-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/dbox-single/sdbox-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -307,8 +307,6 @@
 
 	if (array_count(&ctx->files) == 0) {
 		/* the mail must be freed in the commit_pre() */
-		if (ctx->ctx.mail != NULL)
-			mail_free(&ctx->ctx.mail);
 		return 0;
 	}
 
@@ -340,9 +338,6 @@
 		}
 	}
 
-	if (ctx->ctx.mail != NULL)
-		mail_free(&ctx->ctx.mail);
-
 	_t->changes->uid_validity = hdr->uid_validity;
 	return 0;
 }
@@ -387,8 +382,5 @@
 
 	if (ctx->sync_ctx != NULL)
 		(void)sdbox_sync_finish(&ctx->sync_ctx, FALSE);
-
-	if (ctx->ctx.mail != NULL)
-		mail_free(&ctx->ctx.mail);
 	i_free(ctx);
 }
--- a/src/lib-storage/index/imapc/imapc-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/imapc/imapc-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -141,9 +141,6 @@
 	struct index_mail *imail = (struct index_mail *)_mail;
 	uint32_t seq;
 
-	if (_mail == NULL)
-		return;
-
 	/* we'll temporarily append messages and at commit time expunge
 	   them all, since we can't guarantee that no one else has saved
 	   messages to remote server during our transaction */
--- a/src/lib-storage/index/index-mail.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/index-mail.c	Thu Feb 09 17:14:57 2017 +0200
@@ -2307,9 +2307,6 @@
 {
 	struct index_mail *imail = (struct index_mail *)ctx->dest_mail;
 
-	if (imail == NULL)
-		return;
-
 	if (ctx->data.from_envelope != NULL &&
 	    imail->data.from_envelope == NULL) {
 		imail->data.from_envelope =
--- a/src/lib-storage/index/index-storage.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/index-storage.c	Thu Feb 09 17:14:57 2017 +0200
@@ -878,9 +878,8 @@
 		if (mail_cache_lookup_field(src_mail->transaction->cache_view, buf,
 					    src_mail->seq, src_field_idx) <= 0)
 			buffer_set_used_size(buf, 0);
-		else if (ctx->dest_mail != NULL &&
-			 (strcmp(name, "size.physical") == 0 ||
-			  strcmp(name, "size.virtual") == 0)) {
+		else if (strcmp(name, "size.physical") == 0 ||
+			 strcmp(name, "size.virtual") == 0) {
 			/* FIXME: until mail_cache_lookup() can read unwritten
 			   cached data from buffer, we'll do this optimization
 			   to make quota plugin's work faster */
--- a/src/lib-storage/index/maildir/maildir-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/maildir/maildir-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -48,7 +48,7 @@
 	struct maildir_uidlist_sync_ctx *uidlist_sync_ctx;
 	struct maildir_keywords_sync_ctx *keywords_sync_ctx;
 	struct maildir_index_sync_context *sync_ctx;
-	struct mail *mail, *cur_dest_mail;
+	struct mail *cur_dest_mail;
 
 	const char *tmpdir, *newdir, *curdir;
 	struct maildir_filename *files, **files_tail, *file_last;
@@ -208,11 +208,6 @@
 		i_assert(ctx->files->next == NULL);
 	}
 
-	if (_ctx->dest_mail == NULL) {
-		if (ctx->mail == NULL)
-			ctx->mail = mail_alloc(_ctx->transaction, 0, NULL);
-		_ctx->dest_mail = ctx->mail;
-	}
 	mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
 
 	if (ctx->input == NULL) {
@@ -981,12 +976,8 @@
 	i_assert(_ctx->data.output == NULL);
 	i_assert(ctx->last_save_finished);
 
-	if (ctx->files_count == 0) {
-		/* the mail must be freed in the commit_pre() */
-		if (ctx->mail != NULL)
-			mail_free(&ctx->mail);
+	if (ctx->files_count == 0)
 		return 0;
-	}
 
 	sync_flags = MAILDIR_UIDLIST_SYNC_PARTIAL |
 		MAILDIR_UIDLIST_SYNC_NOREFRESH;
@@ -1041,13 +1032,6 @@
 	_t->changes->uid_validity =
 		maildir_uidlist_get_uid_validity(ctx->mbox->uidlist);
 
-	if (ctx->mail != NULL) {
-		/* Mail freeing may trigger cache updates and a call to
-		   maildir_save_file_get_path(). Do this before finishing index
-		   sync so we still have keywords_sync_ctx. */
-		mail_free(&ctx->mail);
-	}
-
 	if (ctx->locked) {
 		/* It doesn't matter if index syncing fails */
 		ctx->keywords_sync_ctx = NULL;
@@ -1109,7 +1093,5 @@
 	if (ctx->locked)
 		maildir_uidlist_unlock(ctx->mbox->uidlist);
 
-	if (ctx->mail != NULL)
-		mail_free(&ctx->mail);
 	pool_unref(&ctx->pool);
 }
--- a/src/lib-storage/index/mbox/mbox-save.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/mbox/mbox-save.c	Thu Feb 09 17:14:57 2017 +0200
@@ -36,7 +36,6 @@
 
 	struct mbox_mailbox *mbox;
 	struct mail_index_transaction *trans;
-	struct mail *mail;
 	uoff_t append_offset, mail_offset;
 	time_t orig_atime;
 
@@ -269,12 +268,11 @@
 
 static int
 mbox_save_init_file(struct mbox_save_context *ctx,
-		    struct mbox_transaction_context *t, bool want_mail)
+		    struct mbox_transaction_context *t)
 {
 	struct mailbox_transaction_context *_t = &t->t;
 	struct mbox_mailbox *mbox = ctx->mbox;
 	struct mail_storage *storage = &mbox->storage->storage;
-	bool empty = FALSE;
 	int ret;
 
 	if (mbox_is_backend_readonly(ctx->mbox)) {
@@ -283,10 +281,6 @@
 		return -1;
 	}
 
-	if ((_t->flags & MAILBOX_TRANSACTION_FLAG_ASSIGN_UIDS) != 0 ||
-	    ctx->ctx.data.uid != 0)
-		want_mail = TRUE;
-
 	if (ctx->append_offset == (uoff_t)-1) {
 		/* first appended mail in this transaction */
 		if (t->write_lock_id == 0) {
@@ -300,18 +294,12 @@
 		}
 
 		/* update mbox_sync_dirty state */
-		ret = mbox_sync_has_changed_full(mbox, TRUE, &empty);
+		ret = mbox_sync_has_changed(mbox, TRUE);
 		if (ret < 0)
 			return -1;
-		if (!want_mail && ret == 0) {
-			/* we're not required to assign UIDs for the appended
-			   mails immediately. do it only if it doesn't require
-			   syncing. */
-			mbox_save_init_sync(_t);
-		}
 	}
 
-	if (!ctx->synced && (want_mail || empty)) {
+	if (!ctx->synced) {
 		/* we'll need to assign UID for the mail immediately. */
 		if (mbox_sync(mbox, 0) < 0)
 			return -1;
@@ -416,13 +404,10 @@
 		i_stream_create_crlf(filter) : i_stream_create_lf(filter);
 	i_stream_unref(&filter);
 
-	if (ctx->ctx.dest_mail != NULL) {
-		/* caching creates a tee stream */
-		cache_input =
-			index_mail_cache_parse_init(ctx->ctx.dest_mail, ret);
-		i_stream_unref(&ret);
-		ret = cache_input;
-	}
+	/* caching creates a tee stream */
+	cache_input = index_mail_cache_parse_init(ctx->ctx.dest_mail, ret);
+	i_stream_unref(&ret);
+	ret = cache_input;
 	return ret;
 }
 
@@ -463,7 +448,7 @@
 	ctx->failed = FALSE;
 	ctx->seq = 0;
 
-	if (mbox_save_init_file(ctx, t, _ctx->dest_mail != NULL) < 0) {
+	if (mbox_save_init_file(ctx, t) < 0) {
 		ctx->failed = TRUE;
 		return -1;
 	}
@@ -506,13 +491,6 @@
 		ctx->next_uid++;
 
 		/* parse and cache the mail headers as we read it */
-		if (_ctx->dest_mail == NULL) {
-			if (ctx->mail == NULL) {
-				ctx->mail = mail_alloc(_ctx->transaction,
-						       0, NULL);
-			}
-			_ctx->dest_mail = ctx->mail;
-		}
 		mail_set_seq_saving(_ctx->dest_mail, ctx->seq);
 	}
 	mbox_save_append_flag_headers(ctx->headers, save_flags);
@@ -556,11 +534,9 @@
 	while ((ret = i_stream_read(ctx->input)) != -1) {
 		if (mbox_save_body_input(ctx) < 0)
 			return -1;
-		if (ctx->ctx.dest_mail != NULL) {
-			/* i_stream_read() may have returned 0 at EOF
-			   because of this parser */
-			index_mail_cache_parse_continue(ctx->ctx.dest_mail);
-		}
+		/* i_stream_read() may have returned 0 at EOF
+		   because of this parser */
+		index_mail_cache_parse_continue(ctx->ctx.dest_mail);
 		if (ret == 0)
 			return 0;
 	}
@@ -628,8 +604,7 @@
 		i_assert(size > 0);
 		ctx->last_char = data[size-1];
 		i_stream_skip(ctx->input, size);
-		if (ctx->ctx.dest_mail != NULL)
-			index_mail_cache_parse_continue(ctx->ctx.dest_mail);
+		index_mail_cache_parse_continue(ctx->ctx.dest_mail);
 	}
 	if (ret == 0)
 		return 0;
@@ -697,11 +672,9 @@
 		} T_END;
 	}
 
-	if (ctx->ctx.dest_mail != NULL) {
-		index_mail_cache_parse_deinit(ctx->ctx.dest_mail,
-					      ctx->ctx.data.received_date,
-					      !ctx->failed);
-	}
+	index_mail_cache_parse_deinit(ctx->ctx.dest_mail,
+				      ctx->ctx.data.received_date,
+				      !ctx->failed);
 	if (ctx->input != NULL)
 		i_stream_destroy(&ctx->input);
 
@@ -737,8 +710,6 @@
 {
 	if (ctx->output != NULL)
 		o_stream_destroy(&ctx->output);
-	if (ctx->mail != NULL)
-		mail_free(&ctx->mail);
 	str_free(&ctx->headers);
 }
 
--- a/src/lib-storage/index/mbox/mbox-sync-private.h	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-private.h	Thu Feb 09 17:14:57 2017 +0200
@@ -153,8 +153,6 @@
 int mbox_sync_header_refresh(struct mbox_mailbox *mbox);
 int mbox_sync(struct mbox_mailbox *mbox, enum mbox_sync_flags flags);
 int mbox_sync_has_changed(struct mbox_mailbox *mbox, bool leave_dirty);
-int mbox_sync_has_changed_full(struct mbox_mailbox *mbox, bool leave_dirty,
-			       bool *empty_r);
 void mbox_sync_set_critical(struct mbox_sync_context *sync_ctx,
 			    const char *fmt, ...) ATTR_FORMAT(2, 3);
 
--- a/src/lib-storage/index/mbox/mbox-sync.c	Thu Feb 09 16:53:47 2017 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync.c	Thu Feb 09 17:14:57 2017 +0200
@@ -1731,14 +1731,6 @@
 
 int mbox_sync_has_changed(struct mbox_mailbox *mbox, bool leave_dirty)
 {
-	bool empty;
-
-	return mbox_sync_has_changed_full(mbox, leave_dirty, &empty);
-}
-
-int mbox_sync_has_changed_full(struct mbox_mailbox *mbox, bool leave_dirty,
-			       bool *empty_r)
-{
 	const struct stat *st;
 	struct stat statbuf;
 
@@ -1763,7 +1755,6 @@
 		}
 		st = &statbuf;
 	}
-	*empty_r = st->st_size == 0;
 
 	if (mbox_sync_header_refresh(mbox) < 0)
 		return -1;