changeset 21693:c00c65eb641a

lib-mail: istream-attachment-connector now allows msg_size=-1 for "unknown".
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 01 Nov 2016 18:43:57 +0200
parents ce8b35e40712
children 310994917431
files src/lib-mail/istream-attachment-connector.c src/lib-mail/istream-attachment-connector.h src/lib-mail/test-istream-attachment.c
diffstat 3 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-mail/istream-attachment-connector.c	Tue Nov 01 18:42:31 2016 +0200
+++ b/src/lib-mail/istream-attachment-connector.c	Tue Nov 01 18:43:57 2016 +0200
@@ -119,12 +119,18 @@
 	if (conn->base_input_offset != conn->msg_size) {
 		i_assert(conn->base_input_offset < conn->msg_size);
 
-		trailer_size = conn->msg_size - conn->encoded_offset;
-		input = i_stream_create_sized_range(conn->base_input,
-						    conn->base_input_offset,
-						    trailer_size);
-		i_stream_set_name(input, t_strdup_printf(
-			"%s trailer", i_stream_get_name(conn->base_input)));
+		if (conn->msg_size != (uoff_t)-1) {
+			trailer_size = conn->msg_size - conn->encoded_offset;
+			input = i_stream_create_sized_range(conn->base_input,
+							    conn->base_input_offset,
+							    trailer_size);
+			i_stream_set_name(input, t_strdup_printf(
+				"%s trailer", i_stream_get_name(conn->base_input)));
+		} else {
+			input = i_stream_create_range(conn->base_input,
+						      conn->base_input_offset,
+						      (uoff_t)-1);
+		}
 		array_append(&conn->streams, &input, 1);
 	}
 	array_append_zero(&conn->streams);
--- a/src/lib-mail/istream-attachment-connector.h	Tue Nov 01 18:42:31 2016 +0200
+++ b/src/lib-mail/istream-attachment-connector.h	Tue Nov 01 18:43:57 2016 +0200
@@ -2,7 +2,8 @@
 #define ISTREAM_ATTACHMENT_CONNECTOR_H
 
 /* Start building a message stream. The base_input contains the message
-   without attachments. The final stream must be exactly msg_size bytes. */
+   without attachments. The final stream must be exactly msg_size bytes.
+   If the original msg_size isn't known, it can be set to (uoff_t)-1. */
 struct istream_attachment_connector *
 istream_attachment_connector_begin(struct istream *base_input, uoff_t msg_size);
 
--- a/src/lib-mail/test-istream-attachment.c	Tue Nov 01 18:42:31 2016 +0200
+++ b/src/lib-mail/test-istream-attachment.c	Tue Nov 01 18:43:57 2016 +0200
@@ -248,7 +248,7 @@
 	const unsigned char *data;
 	size_t size;
 	struct sha1_ctxt hash;
-	uoff_t msg_size;
+	uoff_t msg_size, orig_msg_size;
 	buffer_t *base_buf;
 	unsigned char hash_file[SHA1_RESULTLEN], hash_attached[SHA1_RESULTLEN];
 	int ret = 0;
@@ -261,7 +261,7 @@
 		i_stream_skip(input, size);
 	}
 	sha1_result(&hash, hash_file);
-	msg_size = input->v_offset;
+	msg_size = orig_msg_size = input->v_offset;
 	i_stream_unref(&input);
 
 	/* read through attachment extractor */
@@ -279,7 +279,7 @@
 	i_stream_unref(&input2);
 
 	/* rebuild the original stream and see if the hash matches */
-	{
+	for (unsigned int i = 0; i < 2; i++) {
 		input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
 		input = test_build_original_istream(input2, msg_size);
 		i_stream_unref(&input2);
@@ -289,18 +289,21 @@
 			sha1_loop(&hash, data, size);
 			i_stream_skip(input, size);
 		}
-		test_assert(input->eof && input->stream_errno == 0);
+		test_assert_idx(input->eof && input->stream_errno == 0, i);
 		sha1_result(&hash, hash_attached);
 		i_stream_unref(&input);
 
 		if (memcmp(hash_file, hash_attached, SHA1_RESULTLEN) != 0)
 			ret = -1;
+
+		/* try again without knowing the message's size */
+		msg_size = (uoff_t)-1;
 	}
 
 	/* try with a wrong message size */
 	for (int i = 0; i < 2; i++) {
 		input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
-		input = test_build_original_istream(input2, msg_size +
+		input = test_build_original_istream(input2, orig_msg_size +
 						    (i == 0 ? 1 : -1));
 		i_stream_unref(&input2);
 		while (i_stream_read_more(input, &data, &size) > 0)