From: Timo Sirainen Date: Wed, 14 Sep 2016 16:06:29 +0000 (+0300) Subject: pop3-migration: Avoid unnecessarily using stream's hdr_size. X-Git-Tag: 2.2.26~257 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c06590cb3d4d49747322da128f8153c847fd1c84;p=thirdparty%2Fdovecot%2Fcore.git pop3-migration: Avoid unnecessarily using stream's hdr_size. Shouldn't change any behavior, except reduce CPU usage a little bit. --- diff --git a/src/plugins/pop3-migration/pop3-migration-plugin.c b/src/plugins/pop3-migration/pop3-migration-plugin.c index 433ef23af6..3938cda4ef 100644 --- a/src/plugins/pop3-migration/pop3-migration-plugin.c +++ b/src/plugins/pop3-migration/pop3-migration-plugin.c @@ -176,25 +176,21 @@ pop3_header_filter_callback(struct header_filter_istream *input ATTR_UNUSED, } int pop3_migration_get_hdr_sha1(uint32_t mail_seq, struct istream *input, - uoff_t hdr_size, unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN], bool *have_eoh_r) { - struct istream *input2; const unsigned char *data; size_t size; struct sha1_ctxt sha1_ctx; struct pop3_hdr_context hdr_ctx; memset(&hdr_ctx, 0, sizeof(hdr_ctx)); - input2 = i_stream_create_limit(input, hdr_size); /* hide headers that might change or be different in IMAP vs. POP3 */ - input = i_stream_create_header_filter(input2, + input = i_stream_create_header_filter(input, HEADER_FILTER_HIDE_BODY | HEADER_FILTER_EXCLUDE | HEADER_FILTER_NO_CR, hdr_hash_skip_headers, N_ELEMENTS(hdr_hash_skip_headers), pop3_header_filter_callback, &hdr_ctx); - i_stream_unref(&input2); sha1_init(&sha1_ctx); while (i_stream_read_data(input, &data, &size, 0) > 0) { @@ -234,21 +230,18 @@ static int get_hdr_sha1(struct mail *mail, unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN]) { struct istream *input; - struct message_size hdr_size; const char *errstr; enum mail_error error; bool have_eoh; int ret; - if (mail_get_hdr_stream(mail, &hdr_size, &input) < 0) { + if (mail_get_hdr_stream(mail, NULL, &input) < 0) { errstr = mailbox_get_last_error(mail->box, &error); i_error("pop3_migration: Failed to get header for msg %u: %s", mail->seq, errstr); return error == MAIL_ERROR_EXPUNGED ? 0 : -1; } - if (pop3_migration_get_hdr_sha1(mail->seq, input, - hdr_size.physical_size, - sha1_r, &have_eoh) < 0) + if (pop3_migration_get_hdr_sha1(mail->seq, input, sha1_r, &have_eoh) < 0) return -1; if (have_eoh) { struct index_mail *imail = (struct index_mail *)mail; @@ -277,15 +270,13 @@ get_hdr_sha1(struct mail *mail, unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN So we'll try to avoid this by falling back to full FETCH BODY[] (and/or RETR) and we'll parse the header ourself from it. This should work around any similar bugs in all IMAP/POP3 servers. */ - if (mail_get_stream_because(mail, &hdr_size, NULL, "pop3-migration", &input) < 0) { + if (mail_get_stream_because(mail, NULL, NULL, "pop3-migration", &input) < 0) { errstr = mailbox_get_last_error(mail->box, &error); i_error("pop3_migration: Failed to get body for msg %u: %s", mail->seq, errstr); return error == MAIL_ERROR_EXPUNGED ? 0 : -1; } - ret = pop3_migration_get_hdr_sha1(mail->seq, input, - hdr_size.physical_size, - sha1_r, &have_eoh); + ret = pop3_migration_get_hdr_sha1(mail->seq, input, sha1_r, &have_eoh); if (ret == 0) { if (!have_eoh) i_warning("pop3_migration: Truncated email with UID %u stored as truncated", mail->uid); diff --git a/src/plugins/pop3-migration/pop3-migration-plugin.h b/src/plugins/pop3-migration/pop3-migration-plugin.h index 398ffb9c4e..1dc84e070e 100644 --- a/src/plugins/pop3-migration/pop3-migration-plugin.h +++ b/src/plugins/pop3-migration/pop3-migration-plugin.h @@ -7,7 +7,6 @@ void pop3_migration_plugin_init(struct module *module); void pop3_migration_plugin_deinit(void); int pop3_migration_get_hdr_sha1(uint32_t mail_seq, struct istream *input, - uoff_t hdr_size, unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN], bool *have_eoh_r); diff --git a/src/plugins/pop3-migration/test-pop3-migration-plugin.c b/src/plugins/pop3-migration/test-pop3-migration-plugin.c index 0fea11b750..a102a8f359 100644 --- a/src/plugins/pop3-migration/test-pop3-migration-plugin.c +++ b/src/plugins/pop3-migration/test-pop3-migration-plugin.c @@ -31,8 +31,7 @@ static void test_pop3_migration_get_hdr_sha1(void) for (i = 0; i < N_ELEMENTS(tests); i++) { input = i_stream_create_from_data(tests[i].input, strlen(tests[i].input)); - test_assert_idx(pop3_migration_get_hdr_sha1(1, input, strlen(tests[i].input), - digest, &have_eoh) == 0, i); + test_assert_idx(pop3_migration_get_hdr_sha1(1, input, digest, &have_eoh) == 0, i); test_assert_idx(strcasecmp(binary_to_hex(digest, sizeof(digest)), tests[i].sha1) == 0, i); test_assert_idx(tests[i].have_eoh == have_eoh, i); i_stream_unref(&input);