changeset 22869:98386299bd70

lib-storage: move snippet generation to mail-save-finish This is necessary because some storage backends (most notably sdbox) do not allow getting the mail stream before the mail is fully written out. (See written_to_disk in sdbox-file.h.) If we could avoid getting the stream to generate a snippet we could leave this where it is.
author Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
date Sun, 18 Feb 2018 18:06:48 -0500
parents 5841256dd8c7
children 6012215fe5dc
files src/lib-storage/index/index-mail.c
diffstat 1 files changed, 19 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/index-mail.c	Mon Feb 19 15:38:13 2018 +0200
+++ b/src/lib-storage/index/index-mail.c	Sun Feb 18 18:06:48 2018 -0500
@@ -71,6 +71,7 @@
 static void index_mail_init_data(struct index_mail *mail);
 static int index_mail_parse_body(struct index_mail *mail,
 				 enum index_cache_field field);
+static int index_mail_write_body_snippet(struct index_mail *mail);
 
 int index_mail_cache_lookup_field(struct index_mail *mail, buffer_t *buf,
 				  unsigned int field_idx)
@@ -860,8 +861,14 @@
 	}
 }
 
-static void index_mail_body_parsed_cache_body_snippet(struct index_mail *mail)
+static void index_mail_save_finish_make_snippet(struct index_mail *mail)
 {
+	if (mail->data.save_body_snippet) {
+		if (index_mail_write_body_snippet(mail) < 0)
+			return;
+		mail->data.save_body_snippet = FALSE;
+	}
+
 	if (mail->data.body_snippet != NULL &&
 	    index_mail_want_cache(mail, MAIL_CACHE_BODY_SNIPPET)) {
 		index_mail_cache_add(mail, MAIL_CACHE_BODY_SNIPPET,
@@ -1092,11 +1099,6 @@
 		mail->data.save_bodystructure_body = FALSE;
 		i_assert(mail->data.parts != NULL);
 	}
-	if (mail->data.save_body_snippet) {
-		if (index_mail_write_body_snippet(mail) < 0)
-			return -1;
-		mail->data.save_body_snippet = FALSE;
-	}
 
 	if (mail->data.no_caching) {
 		/* if we're here because we aborted parsing, don't get any
@@ -1110,7 +1112,6 @@
 	index_mail_body_parsed_cache_flags(mail);
 	index_mail_body_parsed_cache_message_parts(mail);
 	index_mail_body_parsed_cache_bodystructure(mail, field);
-	index_mail_body_parsed_cache_body_snippet(mail);
 	index_mail_cache_sizes(mail);
 	index_mail_cache_dates(mail);
 	return 0;
@@ -1358,6 +1359,15 @@
 		}
 		break;
 	case MAIL_CACHE_BODY_SNIPPET:
+		if (data->body_snippet == NULL) {
+			if (index_mail_write_body_snippet(mail) < 0)
+				return -1;
+
+			if (index_mail_want_cache(mail, MAIL_CACHE_BODY_SNIPPET))
+				index_mail_cache_add(mail, MAIL_CACHE_BODY_SNIPPET,
+						     mail->data.body_snippet,
+						     strlen(mail->data.body_snippet) + 1);
+		}
 		i_assert(data->body_snippet != NULL &&
 			 data->body_snippet[0] != '\0');
 		break;
@@ -2369,6 +2379,8 @@
 {
 	struct index_mail *imail = (struct index_mail *)ctx->dest_mail;
 
+	index_mail_save_finish_make_snippet(imail);
+
 	if (ctx->data.from_envelope != NULL &&
 	    imail->data.from_envelope == NULL) {
 		imail->data.from_envelope =