dovecot-2.1: lib-storage: Added mail_get_hdr_stream() and use it...

dovecot at dovecot.org dovecot at dovecot.org
Tue Oct 4 17:08:52 EEST 2011


details:   http://hg.dovecot.org/dovecot-2.1/rev/276a39ebda4d
changeset: 13601:276a39ebda4d
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Oct 04 17:17:12 2011 +0300
description:
lib-storage: Added mail_get_hdr_stream() and use it where possible.
This makes it clearer for backends when it needs a message body instead of
only message header.

diffstat:

 src/doveadm/doveadm-mail-fetch.c              |   2 +-
 src/imap/imap-fetch-body.c                    |  34 +++++++++++---------------
 src/lib-lda/mail-send.c                       |   3 +-
 src/lib-storage/index/cydir/cydir-mail.c      |   6 +++-
 src/lib-storage/index/dbox-common/dbox-mail.c |   3 +-
 src/lib-storage/index/dbox-common/dbox-mail.h |   3 +-
 src/lib-storage/index/imapc/imapc-mail.c      |  22 +++++++++++-----
 src/lib-storage/index/index-mail-headers.c    |   4 +-
 src/lib-storage/index/index-mail.c            |   8 ++++-
 src/lib-storage/index/index-search.c          |   2 +-
 src/lib-storage/index/maildir/maildir-mail.c  |   9 +++---
 src/lib-storage/index/mbox/mbox-mail.c        |   2 +-
 src/lib-storage/index/raw/raw-mail.c          |   3 +-
 src/lib-storage/mail-storage-private.h        |   3 +-
 src/lib-storage/mail-storage.h                |   4 +++
 src/lib-storage/mail.c                        |  12 ++++++++-
 src/lib-storage/test-mail.c                   |   2 +-
 src/plugins/virtual/virtual-mail.c            |  14 ++++++++--
 18 files changed, 86 insertions(+), 50 deletions(-)

diffs (truncated from 386 to 300 lines):

diff -r d3b30b82a225 -r 276a39ebda4d src/doveadm/doveadm-mail-fetch.c
--- a/src/doveadm/doveadm-mail-fetch.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/doveadm/doveadm-mail-fetch.c	Tue Oct 04 17:17:12 2011 +0300
@@ -105,7 +105,7 @@
 	size_t size;
 	int ret = 0;
 
-	if (mail_get_stream(ctx->mail, &hdr_size, NULL, &input) < 0)
+	if (mail_get_hdr_stream(ctx->mail, &hdr_size, &input) < 0)
 		return -1;
 
 	input = i_stream_create_limit(input, hdr_size.physical_size);
diff -r d3b30b82a225 -r 276a39ebda4d src/imap/imap-fetch-body.c
--- a/src/imap/imap-fetch-body.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/imap/imap-fetch-body.c	Tue Oct 04 17:17:12 2011 +0300
@@ -339,36 +339,28 @@
 	struct istream *input;
 	struct message_size hdr_size, body_size;
 
-	if (body->section[0] == '\0') {
+	switch (body->section[0]) {
+	case '\0':
+		/* BODY[] - fetch everything */
 		if (mail_get_stream(mail, NULL, NULL, &input) < 0 ||
 		    mail_get_virtual_size(mail, &body_size.virtual_size) < 0 ||
 		    mail_get_physical_size(mail, &body_size.physical_size) < 0)
 			return -1;
-	} else {
-		if (mail_get_stream(mail, &hdr_size,
-				    body->section[0] == 'H' ? NULL : &body_size,
-				    &input) < 0)
-			return -1;
-	}
-
-	ctx->cur_input = input;
-	i_stream_ref(ctx->cur_input);
-	ctx->update_partial = TRUE;
-
-	switch (body->section[0]) {
-	case '\0':
-		/* BODY[] - fetch everything */
-                fetch_size = &body_size;
+		fetch_size = &body_size;
 		ctx->cur_size_field = MAIL_FETCH_VIRTUAL_SIZE;
 		break;
 	case 'H':
 		/* BODY[HEADER] - fetch only header */
+		if (mail_get_hdr_stream(mail, &hdr_size, &input) < 0)
+			return -1;
                 fetch_size = &hdr_size;
 		ctx->cur_size_field = MAIL_FETCH_MESSAGE_PARTS;
 		break;
 	case 'T':
 		/* BODY[TEXT] - skip header */
-		i_stream_skip(ctx->cur_input, hdr_size.physical_size);
+		if (mail_get_stream(mail, &hdr_size, &body_size, &input) < 0)
+			return -1;
+		i_stream_skip(input, hdr_size.physical_size);
                 fetch_size = &body_size;
 		ctx->cur_size_field = MAIL_FETCH_VIRTUAL_SIZE;
 		break;
@@ -376,6 +368,10 @@
 		i_unreached();
 	}
 
+	ctx->cur_input = input;
+	i_stream_ref(ctx->cur_input);
+	ctx->update_partial = TRUE;
+
 	return fetch_data(ctx, body, fetch_size);
 }
 
@@ -435,7 +431,7 @@
 fetch_body_header_partial(struct imap_fetch_context *ctx, struct mail *mail,
 			  const struct imap_fetch_body_data *body)
 {
-	if (mail_get_stream(mail, NULL, NULL, &ctx->cur_input) < 0)
+	if (mail_get_hdr_stream(mail, NULL, &ctx->cur_input) < 0)
 		return -1;
 
 	i_stream_ref(ctx->cur_input);
@@ -922,7 +918,7 @@
 	struct message_size hdr_size;
 	const char *str;
 
-	if (mail_get_stream(mail, &hdr_size, NULL, &ctx->cur_input) < 0)
+	if (mail_get_hdr_stream(mail, &hdr_size, &ctx->cur_input) < 0)
 		return -1;
 
 	i_stream_ref(ctx->cur_input);
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-lda/mail-send.c
--- a/src/lib-lda/mail-send.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-lda/mail-send.c	Tue Oct 04 17:17:12 2011 +0300
@@ -56,7 +56,6 @@
     struct istream *input;
     struct smtp_client *smtp_client;
     FILE *f;
-    struct message_size hdr_size;
     const char *return_addr, *hdr;
     const unsigned char *data;
     const char *value, *msgid, *orig_msgid, *boundary;
@@ -142,7 +141,7 @@
     /* original message's headers */
     fprintf(f, "--%s\r\nContent-Type: message/rfc822\r\n\r\n", boundary);
 
-    if (mail_get_stream(mail, &hdr_size, NULL, &input) == 0) {
+    if (mail_get_hdr_stream(mail, NULL, &input) == 0) {
 	    /* Note: If you add more headers, they need to be sorted.
 	       We'll drop Content-Type because we're not including the message
 	       body, and having a multipart Content-Type may confuse some
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/cydir/cydir-mail.c
--- a/src/lib-storage/index/cydir/cydir-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/cydir/cydir-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -91,8 +91,10 @@
 }
 
 static int
-cydir_mail_get_stream(struct mail *_mail, struct message_size *hdr_size,
-		      struct message_size *body_size, struct istream **stream_r)
+cydir_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
+		      struct message_size *hdr_size,
+		      struct message_size *body_size,
+		      struct istream **stream_r)
 {
 	struct index_mail *mail = (struct index_mail *)_mail;
 	const char *path;
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/dbox-common/dbox-mail.c
--- a/src/lib-storage/index/dbox-common/dbox-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/dbox-common/dbox-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -234,7 +234,8 @@
 		return dbox_attachment_file_get_stream(file, stream_r);
 }
 
-int dbox_mail_get_stream(struct mail *_mail, struct message_size *hdr_size,
+int dbox_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
+			 struct message_size *hdr_size,
 			 struct message_size *body_size,
 			 struct istream **stream_r)
 {
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/dbox-common/dbox-mail.h
--- a/src/lib-storage/index/dbox-common/dbox-mail.h	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/dbox-common/dbox-mail.h	Tue Oct 04 17:17:12 2011 +0300
@@ -22,7 +22,8 @@
 int dbox_mail_get_save_date(struct mail *_mail, time_t *date_r);
 int dbox_mail_get_special(struct mail *mail, enum mail_fetch_field field,
 			  const char **value_r);
-int dbox_mail_get_stream(struct mail *_mail, struct message_size *hdr_size,
+int dbox_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
+			 struct message_size *hdr_size,
 			 struct message_size *body_size,
 			 struct istream **stream_r);
 
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/imapc/imapc-mail.c
--- a/src/lib-storage/index/imapc/imapc-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/imapc/imapc-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -134,20 +134,27 @@
 }
 
 static int
-imapc_mail_get_stream(struct mail *_mail, struct message_size *hdr_size,
+imapc_mail_get_stream(struct mail *_mail, bool get_body,
+		      struct message_size *hdr_size,
 		      struct message_size *body_size, struct istream **stream_r)
 {
-	struct index_mail *mail = (struct index_mail *)_mail;
-	struct index_mail_data *data = &mail->data;
+	struct imapc_mail *mail = (struct imapc_mail *)_mail;
+	struct index_mail_data *data = &mail->imail.data;
 	enum mail_fetch_field fetch_field;
 
+	if (get_body && !mail->body_fetched &&
+	    mail->imail.data.stream != NULL) {
+		/* we've fetched the header, but we need the body now too */
+		i_stream_unref(&mail->imail.data.stream);
+	}
+
 	if (data->stream == NULL) {
-		if (!mail->data.initialized) {
+		if (!data->initialized) {
 			/* coming here from mail_set_seq() */
 			return mail_set_aborted(_mail);
 		}
-		fetch_field = body_size != NULL ||
-			(mail->wanted_fields & MAIL_FETCH_STREAM_BODY) != 0 ?
+		fetch_field = get_body ||
+			(mail->imail.wanted_fields & MAIL_FETCH_STREAM_BODY) != 0 ?
 			MAIL_FETCH_STREAM_BODY : MAIL_FETCH_STREAM_HEADER;
 		if (imapc_mail_fetch(_mail, fetch_field) < 0)
 			return -1;
@@ -158,7 +165,8 @@
 		}
 	}
 
-	return index_mail_init_stream(mail, hdr_size, body_size, stream_r);
+	return index_mail_init_stream(&mail->imail, hdr_size, body_size,
+				      stream_r);
 }
 
 static bool
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/index-mail-headers.c
--- a/src/lib-storage/index/index-mail-headers.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/index-mail-headers.c	Tue Oct 04 17:17:12 2011 +0300
@@ -412,7 +412,7 @@
 
 	old_offset = data->stream == NULL ? 0 : data->stream->v_offset;
 
-	if (mail_get_stream(&mail->mail.mail, NULL, NULL, &input) < 0)
+	if (mail_get_hdr_stream(&mail->mail.mail, NULL, &input) < 0)
 		return -1;
 
 	index_mail_parse_header_init(mail, headers);
@@ -819,7 +819,7 @@
 	/* not in cache / error */
 	p_free(mail->data_pool, dest);
 
-	if (mail_get_stream(_mail, NULL, NULL, &input) < 0)
+	if (mail_get_hdr_stream(_mail, NULL, &input) < 0)
 		return -1;
 
 	if (mail->data.filter_stream != NULL)
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/index-mail.c
--- a/src/lib-storage/index/index-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/index-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -1320,8 +1320,12 @@
 		/* open the stream only if we didn't get here from
 		   mailbox_save_init() */
 		hdr = mail_index_get_header(_mail->box->view);
-		if (!_mail->saving && _mail->uid < hdr->next_uid)
-			(void)mail_get_stream(_mail, NULL, NULL, &input);
+		if (!_mail->saving && _mail->uid < hdr->next_uid) {
+			if ((data->access_part & READ_BODY) != 0)
+				(void)mail_get_stream(_mail, NULL, NULL, &input);
+			else
+				(void)mail_get_hdr_stream(_mail, NULL, &input);
+		}
 	}
 
 	if ((mail->wanted_fields & MAIL_FETCH_VIRTUAL_SIZE) != 0 &&
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/index-search.c
--- a/src/lib-storage/index/index-search.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/index-search.c	Tue Oct 04 17:17:12 2011 +0300
@@ -632,7 +632,7 @@
 		input = NULL;
 	} else if (have_headers) {
 		/* we need to read the entire header */
-		if (mail_get_stream(ctx->cur_mail, NULL, NULL, &input) < 0)
+		if (mail_get_hdr_stream(ctx->cur_mail, NULL, &input) < 0)
 			failed = TRUE;
 		else {
 			hdr_ctx.parse_headers =
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/maildir/maildir-mail.c
--- a/src/lib-storage/index/maildir/maildir-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/maildir/maildir-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -550,10 +550,11 @@
 	}
 }
 							
-static int maildir_mail_get_stream(struct mail *_mail,
-				   struct message_size *hdr_size,
-				   struct message_size *body_size,
-				   struct istream **stream_r)
+static int
+maildir_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
+			struct message_size *hdr_size,
+			struct message_size *body_size,
+			struct istream **stream_r)
 {
 	struct index_mail *mail = (struct index_mail *)_mail;
 	struct maildir_mailbox *mbox = (struct maildir_mailbox *)_mail->box;
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/mbox/mbox-mail.c
--- a/src/lib-storage/index/mbox/mbox-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/mbox/mbox-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -361,7 +361,7 @@
 	return 0;
 }
 
-static int mbox_mail_get_stream(struct mail *_mail,
+static int mbox_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
 				struct message_size *hdr_size,
 				struct message_size *body_size,
 				struct istream **stream_r)
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/index/raw/raw-mail.c
--- a/src/lib-storage/index/raw/raw-mail.c	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/index/raw/raw-mail.c	Tue Oct 04 17:17:12 2011 +0300
@@ -77,7 +77,8 @@
 }
 
 static int
-raw_mail_get_stream(struct mail *_mail, struct message_size *hdr_size,
+raw_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
+		    struct message_size *hdr_size,
 		    struct message_size *body_size, struct istream **stream_r)
 {
 	struct index_mail *mail = (struct index_mail *)_mail;
diff -r d3b30b82a225 -r 276a39ebda4d src/lib-storage/mail-storage-private.h
--- a/src/lib-storage/mail-storage-private.h	Tue Oct 04 17:14:30 2011 +0300
+++ b/src/lib-storage/mail-storage-private.h	Tue Oct 04 17:17:12 2011 +0300
@@ -300,7 +300,8 @@
 	int (*get_header_stream)(struct mail *mail,
 				 struct mailbox_header_lookup_ctx *headers,


More information about the dovecot-cvs mailing list