changeset 20555:d5642dc6cccc

lib-lda: Code cleanup - move code to mail_deliver_open_mail() No functional changes. On its own this is a rather useless cleanup, but simplifies the next patch.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 02 Aug 2016 21:09:34 +0300
parents dcf9cc6ee647
children 86d9ea2f8fc8
files src/lib-lda/mail-deliver.c
diffstat 1 files changed, 33 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-lda/mail-deliver.c	Tue Aug 02 20:09:49 2016 +0300
+++ b/src/lib-lda/mail-deliver.c	Tue Aug 02 21:09:34 2016 +0300
@@ -290,6 +290,35 @@
 	}
 }
 
+static struct mail *
+mail_deliver_open_mail(struct mailbox *box,
+		       const struct mail_transaction_commit_changes *changes,
+		       enum mail_fetch_field wanted_fields,
+		       struct mailbox_transaction_context **trans_r)
+{
+	struct mailbox_transaction_context *t;
+	struct mail *mail;
+	const struct seq_range *range;
+
+	*trans_r = NULL;
+
+	if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FAST) < 0)
+		return NULL;
+
+	range = array_idx(&changes->saved_uids, 0);
+	i_assert(range[0].seq1 == range[0].seq2);
+
+	t = mailbox_transaction_begin(box, 0);
+	mail = mail_alloc(t, wanted_fields, NULL);
+
+	if (!mail_set_uid(mail, range[0].seq1)) {
+		mail_free(&mail);
+		mailbox_transaction_rollback(&t);
+	}
+	*trans_r = t;
+	return mail;
+}
+
 int mail_deliver_save(struct mail_deliver_context *ctx, const char *mailbox,
 		      enum mail_flags flags, const char *const *keywords,
 		      struct mail_storage **storage_r)
@@ -304,7 +333,6 @@
 	enum mail_error error;
 	const char *mailbox_name, *errstr, *guid;
 	struct mail_transaction_commit_changes changes;
-	const struct seq_range *range;
 	bool default_save;
 	int ret = 0;
 
@@ -366,20 +394,13 @@
 	if (ret == 0) {
 		ctx->saved_mail = TRUE;
 		mail_deliver_log(ctx, "saved mail to %s", mailbox_name);
-
-		if (ctx->save_dest_mail &&
-		    mailbox_sync(box, MAILBOX_SYNC_FLAG_FAST) == 0) {
-			range = array_idx(&changes.saved_uids, 0);
-			i_assert(range[0].seq1 == range[0].seq2);
-
-			t = mailbox_transaction_begin(box, 0);
-			ctx->dest_mail = mail_alloc(t, MAIL_FETCH_STREAM_BODY,
-						    NULL);
+		if (ctx->save_dest_mail) {
 			/* copying needs the message body. with maildir we also
 			   need to get the GUID in case the message gets
 			   expunged */
-			if (!mail_set_uid(ctx->dest_mail, range[0].seq1) ||
-			    mail_get_special(ctx->dest_mail, MAIL_FETCH_GUID, &guid) < 0) {
+			ctx->dest_mail = mail_deliver_open_mail(box, &changes,
+				MAIL_FETCH_STREAM_BODY | MAIL_FETCH_GUID, &t);
+			if (mail_get_special(ctx->dest_mail, MAIL_FETCH_GUID, &guid) < 0) {
 				mail_free(&ctx->dest_mail);
 				mailbox_transaction_rollback(&t);
 			}